From 73292ae949e38451287ce4068f2837cb83f3ec3e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ya=C5=9Far=20Arabac=C4=B1?= <yasar11732@gmail.com>
Date: Fri, 30 Sep 2011 00:47:14 +0300
Subject: [PATCH] Fix #16559 - Deprecate mimetype for HttpResponse
I interchanged locations of mimetype and content_type
kwargs. Using mimetype keyword argument now raises a
DeprecatedWarning. Made required changes around all
codebase so that content_type is used instead of mimetype
---
django/contrib/gis/shortcuts.py | 6 +++---
django/contrib/gis/sitemaps/views.py | 4 ++--
django/contrib/sitemaps/views.py | 4 ++--
django/contrib/syndication/views.py | 2 +-
django/http/__init__.py | 8 ++++++--
django/shortcuts/__init__.py | 2 +-
django/template/response.py | 13 ++++++-------
django/views/csrf.py | 2 +-
django/views/debug.py | 6 +++---
django/views/generic/date_based.py | 12 ++++++------
django/views/generic/list_detail.py | 4 ++--
django/views/generic/simple.py | 2 +-
django/views/static.py | 4 ++--
tests/regressiontests/csrf_tests/tests.py | 2 +-
tests/regressiontests/test_client_regress/views.py | 2 +-
15 files changed, 38 insertions(+), 35 deletions(-)
diff --git a/django/contrib/gis/shortcuts.py b/django/contrib/gis/shortcuts.py
index 9a80f4d..8bdafdf 100644
a
|
b
|
def compress_kml(kml):
|
20 | 20 | def render_to_kml(*args, **kwargs): |
21 | 21 | "Renders the response as KML (using the correct MIME type)." |
22 | 22 | return HttpResponse(loader.render_to_string(*args, **kwargs), |
23 | | mimetype='application/vnd.google-earth.kml+xml') |
| 23 | content_type='application/vnd.google-earth.kml+xml') |
24 | 24 | |
25 | 25 | def render_to_kmz(*args, **kwargs): |
26 | 26 | """ |
… |
… |
def render_to_kmz(*args, **kwargs):
|
28 | 28 | MIME type). |
29 | 29 | """ |
30 | 30 | return HttpResponse(compress_kml(loader.render_to_string(*args, **kwargs)), |
31 | | mimetype='application/vnd.google-earth.kmz') |
| 31 | content_type='application/vnd.google-earth.kmz') |
32 | 32 | |
33 | 33 | |
34 | 34 | def render_to_text(*args, **kwargs): |
35 | 35 | "Renders the response using the MIME type for plain text." |
36 | 36 | return HttpResponse(loader.render_to_string(*args, **kwargs), |
37 | | mimetype='text/plain') |
| 37 | content_type='text/plain') |
diff --git a/django/contrib/gis/sitemaps/views.py b/django/contrib/gis/sitemaps/views.py
index e4ac9f7..eb2454c 100644
a
|
b
|
def index(request, sitemaps):
|
30 | 30 | for page in range(2, pages+1): |
31 | 31 | sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) |
32 | 32 | xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) |
33 | | return HttpResponse(xml, mimetype='application/xml') |
| 33 | return HttpResponse(xml, content_type='application/xml') |
34 | 34 | |
35 | 35 | def sitemap(request, sitemaps, section=None): |
36 | 36 | """ |
… |
… |
def sitemap(request, sitemaps, section=None):
|
58 | 58 | except PageNotAnInteger: |
59 | 59 | raise Http404("No page '%s'" % page) |
60 | 60 | xml = smart_str(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls})) |
61 | | return HttpResponse(xml, mimetype='application/xml') |
| 61 | return HttpResponse(xml, content_type='application/xml') |
62 | 62 | |
63 | 63 | def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS): |
64 | 64 | """ |
diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py
index d82146c..b3aaafd 100644
a
|
b
|
def index(request, sitemaps,
|
21 | 21 | if pages > 1: |
22 | 22 | for page in range(2, pages+1): |
23 | 23 | sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) |
24 | | return TemplateResponse(request, template_name, {'sitemaps': sites}, mimetype=mimetype) |
| 24 | return TemplateResponse(request, template_name, {'sitemaps': sites}, content_type=mimetype) |
25 | 25 | |
26 | 26 | def sitemap(request, sitemaps, section=None, |
27 | 27 | template_name='sitemap.xml', mimetype='application/xml'): |
… |
… |
def sitemap(request, sitemaps, section=None,
|
43 | 43 | raise Http404("Page %s empty" % page) |
44 | 44 | except PageNotAnInteger: |
45 | 45 | raise Http404("No page '%s'" % page) |
46 | | return TemplateResponse(request, template_name, {'urlset': urls}, mimetype=mimetype) |
47 | | No newline at end of file |
| 46 | return TemplateResponse(request, template_name, {'urlset': urls}, content_type=mimetype) |
diff --git a/django/contrib/syndication/views.py b/django/contrib/syndication/views.py
index 8e7c1d1..6937a12 100644
a
|
b
|
class Feed(object):
|
35 | 35 | except ObjectDoesNotExist: |
36 | 36 | raise Http404('Feed object does not exist.') |
37 | 37 | feedgen = self.get_feed(obj, request) |
38 | | response = HttpResponse(mimetype=feedgen.mime_type) |
| 38 | response = HttpResponse(content_type=feedgen.mime_type) |
39 | 39 | feedgen.write(response, 'utf-8') |
40 | 40 | return response |
41 | 41 | |
diff --git a/django/http/__init__.py b/django/http/__init__.py
index c6bf2f1..1f57eb5 100644
a
|
b
|
import datetime
|
2 | 2 | import os |
3 | 3 | import re |
4 | 4 | import time |
| 5 | import warnings |
| 6 | |
5 | 7 | from pprint import pformat |
6 | 8 | from urllib import urlencode, quote |
7 | 9 | from urlparse import urljoin |
… |
… |
class HttpResponse(object):
|
532 | 534 | |
533 | 535 | status_code = 200 |
534 | 536 | |
535 | | def __init__(self, content='', mimetype=None, status=None, |
536 | | content_type=None): |
| 537 | def __init__(self, content='', content_type=None, status=None, |
| 538 | mimetype=None): |
537 | 539 | # _headers is a mapping of the lower-case name to the original case of |
538 | 540 | # the header (required for working with legacy systems) and the header |
539 | 541 | # value. Both the name of the header and its value are ASCII strings. |
… |
… |
class HttpResponse(object):
|
541 | 543 | self._charset = settings.DEFAULT_CHARSET |
542 | 544 | if mimetype: |
543 | 545 | content_type = mimetype # For backwards compatibility |
| 546 | warnings.warn("Using mimetype keyword argument is deprecated, use" |
| 547 | " content_type instead",DeprecationWarning) |
544 | 548 | if not content_type: |
545 | 549 | content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, |
546 | 550 | self._charset) |
diff --git a/django/shortcuts/__init__.py b/django/shortcuts/__init__.py
index 9f97cae..154f224 100644
a
|
b
|
def render_to_response(*args, **kwargs):
|
16 | 16 | Returns a HttpResponse whose content is filled with the result of calling |
17 | 17 | django.template.loader.render_to_string() with the passed arguments. |
18 | 18 | """ |
19 | | httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} |
| 19 | httpresponse_kwargs = {'content_type': kwargs.pop('mimetype', None)} |
20 | 20 | return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) |
21 | 21 | |
22 | 22 | def render(request, *args, **kwargs): |
diff --git a/django/template/response.py b/django/template/response.py
index 990a17b..95aaa54 100644
a
|
b
|
class DiscardedAttributeError(AttributeError):
|
13 | 13 | class SimpleTemplateResponse(HttpResponse): |
14 | 14 | rendering_attrs = ['template_name', 'context_data', '_post_render_callbacks'] |
15 | 15 | |
16 | | def __init__(self, template, context=None, mimetype=None, status=None, |
17 | | content_type=None): |
| 16 | def __init__(self, template, context=None, content_type=None, status=None, |
| 17 | mimetype=None): |
18 | 18 | # It would seem obvious to call these next two members 'template' and |
19 | 19 | # 'context', but those names are reserved as part of the test Client |
20 | 20 | # API. To avoid the name collision, we use tricky-to-debug problems |
… |
… |
class SimpleTemplateResponse(HttpResponse):
|
26 | 26 | # content argument doesn't make sense here because it will be replaced |
27 | 27 | # with rendered template so we always pass empty string in order to |
28 | 28 | # prevent errors and provide shorter signature. |
29 | | super(SimpleTemplateResponse, self).__init__('', mimetype, status, |
30 | | content_type) |
| 29 | super(SimpleTemplateResponse, self).__init__('', content_type, status) |
31 | 30 | |
32 | 31 | # _is_rendered tracks whether the template and context has been baked |
33 | 32 | # into a final response. |
… |
… |
class TemplateResponse(SimpleTemplateResponse):
|
149 | 148 | rendering_attrs = SimpleTemplateResponse.rendering_attrs + \ |
150 | 149 | ['_request', '_current_app'] |
151 | 150 | |
152 | | def __init__(self, request, template, context=None, mimetype=None, |
153 | | status=None, content_type=None, current_app=None): |
| 151 | def __init__(self, request, template, context=None, content_type=None, |
| 152 | status=None, mimetype=None, current_app=None): |
154 | 153 | # self.request gets over-written by django.test.client.Client - and |
155 | 154 | # unlike context_data and template_name the _request should not |
156 | 155 | # be considered part of the public API. |
… |
… |
class TemplateResponse(SimpleTemplateResponse):
|
159 | 158 | # having to avoid needing to create the RequestContext directly |
160 | 159 | self._current_app = current_app |
161 | 160 | super(TemplateResponse, self).__init__( |
162 | | template, context, mimetype, status, content_type) |
| 161 | template, context, content_type, status) |
163 | 162 | |
164 | 163 | def resolve_context(self, context): |
165 | 164 | """Convert context data into a full RequestContext object |
diff --git a/django/views/csrf.py b/django/views/csrf.py
index 80cc21d..36e1e01 100644
a
|
b
|
def csrf_failure(request, reason=""):
|
99 | 99 | 'reason': reason, |
100 | 100 | 'no_referer': reason == REASON_NO_REFERER |
101 | 101 | }) |
102 | | return HttpResponseForbidden(t.render(c), mimetype='text/html') |
| 102 | return HttpResponseForbidden(t.render(c), content_type='text/html') |
diff --git a/django/views/debug.py b/django/views/debug.py
index 8c77d70..04ff122 100644
a
|
b
|
def technical_500_response(request, exc_type, exc_value, tb):
|
60 | 60 | """ |
61 | 61 | reporter = ExceptionReporter(request, exc_type, exc_value, tb) |
62 | 62 | html = reporter.get_traceback_html() |
63 | | return HttpResponseServerError(html, mimetype='text/html') |
| 63 | return HttpResponseServerError(html, content_type='text/html') |
64 | 64 | |
65 | 65 | # Cache for the default exception reporter filter instance. |
66 | 66 | default_exception_reporter_filter = None |
… |
… |
def technical_404_response(request, exception):
|
412 | 412 | 'request': request, |
413 | 413 | 'settings': get_safe_settings(), |
414 | 414 | }) |
415 | | return HttpResponseNotFound(t.render(c), mimetype='text/html') |
| 415 | return HttpResponseNotFound(t.render(c), content_type='text/html') |
416 | 416 | |
417 | 417 | def empty_urlconf(request): |
418 | 418 | "Create an empty URLconf 404 error response." |
… |
… |
def empty_urlconf(request):
|
420 | 420 | c = Context({ |
421 | 421 | 'project_name': settings.SETTINGS_MODULE.split('.')[0] |
422 | 422 | }) |
423 | | return HttpResponse(t.render(c), mimetype='text/html') |
| 423 | return HttpResponse(t.render(c), content_type='text/html') |
424 | 424 | |
425 | 425 | # |
426 | 426 | # Templates are embedded in the file so that we know the error handler will |
diff --git a/django/views/generic/date_based.py b/django/views/generic/date_based.py
index acef71a..a2023b1 100644
a
|
b
|
def archive_index(request, queryset, date_field, num_latest=15,
|
53 | 53 | c[key] = value() |
54 | 54 | else: |
55 | 55 | c[key] = value |
56 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 56 | return HttpResponse(t.render(c), content_type=mimetype) |
57 | 57 | |
58 | 58 | def archive_year(request, year, queryset, date_field, template_name=None, |
59 | 59 | template_loader=loader, extra_context=None, allow_empty=False, |
… |
… |
def archive_year(request, year, queryset, date_field, template_name=None,
|
101 | 101 | c[key] = value() |
102 | 102 | else: |
103 | 103 | c[key] = value |
104 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 104 | return HttpResponse(t.render(c), content_type=mimetype) |
105 | 105 | |
106 | 106 | def archive_month(request, year, month, queryset, date_field, |
107 | 107 | month_format='%b', template_name=None, template_loader=loader, |
… |
… |
def archive_month(request, year, month, queryset, date_field,
|
181 | 181 | c[key] = value() |
182 | 182 | else: |
183 | 183 | c[key] = value |
184 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 184 | return HttpResponse(t.render(c), content_type=mimetype) |
185 | 185 | |
186 | 186 | def archive_week(request, year, week, queryset, date_field, |
187 | 187 | template_name=None, template_loader=loader, |
… |
… |
def archive_week(request, year, week, queryset, date_field,
|
233 | 233 | c[key] = value() |
234 | 234 | else: |
235 | 235 | c[key] = value |
236 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 236 | return HttpResponse(t.render(c), content_type=mimetype) |
237 | 237 | |
238 | 238 | def archive_day(request, year, month, day, queryset, date_field, |
239 | 239 | month_format='%b', day_format='%d', template_name=None, |
… |
… |
def archive_day(request, year, month, day, queryset, date_field,
|
299 | 299 | c[key] = value() |
300 | 300 | else: |
301 | 301 | c[key] = value |
302 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 302 | return HttpResponse(t.render(c), content_type=mimetype) |
303 | 303 | |
304 | 304 | def archive_today(request, **kwargs): |
305 | 305 | """ |
… |
… |
def object_detail(request, year, month, day, queryset, date_field,
|
370 | 370 | c[key] = value() |
371 | 371 | else: |
372 | 372 | c[key] = value |
373 | | response = HttpResponse(t.render(c), mimetype=mimetype) |
| 373 | response = HttpResponse(t.render(c), content_type=mimetype) |
374 | 374 | populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name)) |
375 | 375 | return response |
diff --git a/django/views/generic/list_detail.py b/django/views/generic/list_detail.py
index 22414ae..8f4509c 100644
a
|
b
|
def object_list(request, queryset, paginate_by=None, page=None,
|
105 | 105 | model = queryset.model |
106 | 106 | template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) |
107 | 107 | t = template_loader.get_template(template_name) |
108 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 108 | return HttpResponse(t.render(c), content_type=mimetype) |
109 | 109 | |
110 | 110 | def object_detail(request, queryset, object_id=None, slug=None, |
111 | 111 | slug_field='slug', template_name=None, template_name_field=None, |
… |
… |
def object_detail(request, queryset, object_id=None, slug=None,
|
147 | 147 | c[key] = value() |
148 | 148 | else: |
149 | 149 | c[key] = value |
150 | | response = HttpResponse(t.render(c), mimetype=mimetype) |
| 150 | response = HttpResponse(t.render(c), content_type=mimetype) |
151 | 151 | populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name)) |
152 | 152 | return response |
diff --git a/django/views/generic/simple.py b/django/views/generic/simple.py
index 26df504..a63e184 100644
a
|
b
|
def direct_to_template(request, template, extra_context=None, mimetype=None, **k
|
25 | 25 | dictionary[key] = value |
26 | 26 | c = RequestContext(request, dictionary) |
27 | 27 | t = loader.get_template(template) |
28 | | return HttpResponse(t.render(c), mimetype=mimetype) |
| 28 | return HttpResponse(t.render(c), content_type=mimetype) |
29 | 29 | |
30 | 30 | def redirect_to(request, url, permanent=True, query_string=False, **kwargs): |
31 | 31 | """ |
diff --git a/django/views/static.py b/django/views/static.py
index 387b7da..2e0da33 100644
a
|
b
|
def serve(request, path, document_root=None, show_indexes=False):
|
57 | 57 | mimetype = mimetype or 'application/octet-stream' |
58 | 58 | if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), |
59 | 59 | statobj.st_mtime, statobj.st_size): |
60 | | return HttpResponseNotModified(mimetype=mimetype) |
| 60 | return HttpResponseNotModified(content_type=mimetype) |
61 | 61 | with open(fullpath, 'rb') as f: |
62 | | response = HttpResponse(f.read(), mimetype=mimetype) |
| 62 | response = HttpResponse(f.read(), content_type=mimetype) |
63 | 63 | response["Last-Modified"] = http_date(statobj.st_mtime) |
64 | 64 | if stat.S_ISREG(statobj.st_mode): |
65 | 65 | response["Content-Length"] = statobj.st_size |
diff --git a/tests/regressiontests/csrf_tests/tests.py b/tests/regressiontests/csrf_tests/tests.py
index c5f9f36..0f72bee 100644
a
|
b
|
from django.template import RequestContext, Template
|
13 | 13 | def post_form_response(): |
14 | 14 | resp = HttpResponse(content=u""" |
15 | 15 | <html><body><h1>\u00a1Unicode!<form method="post"><input type="text" /></form></body></html> |
16 | | """, mimetype="text/html") |
| 16 | """, content_type="text/html") |
17 | 17 | return resp |
18 | 18 | |
19 | 19 | def post_form_view(request): |
diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py
index b398293..f16057b 100644
a
|
b
|
def return_json_file(request):
|
84 | 84 | cls=DjangoJSONEncoder, |
85 | 85 | ensure_ascii=False) |
86 | 86 | response = HttpResponse(smart_str(obj_json, encoding=charset), status=200, |
87 | | mimetype='application/json; charset=' + charset) |
| 87 | content_type='application/json; charset=' + charset) |
88 | 88 | response['Content-Disposition'] = 'attachment; filename=testfile.json' |
89 | 89 | return response |
90 | 90 | |