From 89cb771be7b53c40642872cdbedb15943bdf8e34 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 31 Jan 2013 13:39:29 +0100 Subject: [PATCH] Fixed #19692 -- Completed deprecation of mimetype in favor of content_type. Thanks Tim for the report and initial patch. --- django/contrib/sitemaps/views.py | 26 +++++++++++--- django/shortcuts/__init__.py | 10 +++++- docs/howto/outputting-csv.txt | 6 ++-- docs/howto/outputting-pdf.txt | 4 +-- docs/internals/deprecation.txt | 10 ++++-- docs/ref/contrib/admin/actions.txt | 4 +-- docs/ref/template-response.txt | 36 +++++++++++-------- docs/topics/http/shortcuts.txt | 13 +++++-- tests/regressiontests/views/generic_urls.py | 2 +- .../regressiontests/views/tests/shortcuts.py | 4 +-- tests/regressiontests/views/views.py | 6 ++-- 11 files changed, 81 insertions(+), 40 deletions(-) diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py index cfe3aa66a9..c8d2f4dfa0 100644 --- a/django/contrib/sitemaps/views.py +++ b/django/contrib/sitemaps/views.py @@ -1,3 +1,5 @@ +import warnings + from django.contrib.sites.models import get_current_site from django.core import urlresolvers from django.core.paginator import EmptyPage, PageNotAnInteger @@ -6,8 +8,15 @@ from django.template.response import TemplateResponse from django.utils import six def index(request, sitemaps, - template_name='sitemap_index.xml', mimetype='application/xml', - sitemap_url_name='django.contrib.sitemaps.views.sitemap'): + template_name='sitemap_index.xml', content_type='application/xml', + sitemap_url_name='django.contrib.sitemaps.views.sitemap', + mimetype=None): + + if mimetype: + warnings.warn("The mimetype keyword argument is deprecated, use " + "content_type instead", DeprecationWarning, stacklevel=2) + content_type = mimetype + req_protocol = 'https' if request.is_secure() else 'http' req_site = get_current_site(request) @@ -24,10 +33,17 @@ def index(request, sitemaps, sites.append('%s?p=%s' % (absolute_url, page)) return TemplateResponse(request, template_name, {'sitemaps': sites}, - content_type=mimetype) + content_type=content_type) def sitemap(request, sitemaps, section=None, - template_name='sitemap.xml', mimetype='application/xml'): + template_name='sitemap.xml', content_type='application/xml', + mimetype=None): + + if mimetype: + warnings.warn("The mimetype keyword argument is deprecated, use " + "content_type instead", DeprecationWarning, stacklevel=2) + content_type = mimetype + req_protocol = 'https' if request.is_secure() else 'http' req_site = get_current_site(request) @@ -51,4 +67,4 @@ def sitemap(request, sitemaps, section=None, except PageNotAnInteger: raise Http404("No page '%s'" % page) return TemplateResponse(request, template_name, {'urlset': urls}, - content_type=mimetype) + content_type=content_type) diff --git a/django/shortcuts/__init__.py b/django/shortcuts/__init__.py index 9f896347a4..21bd7a06d2 100644 --- a/django/shortcuts/__init__.py +++ b/django/shortcuts/__init__.py @@ -3,6 +3,7 @@ This module collects helper functions and classes that "span" multiple levels of MVC. In other words, these functions/classes introduce controlled coupling for convenience's sake. """ +import warnings from django.template import loader, RequestContext from django.http import HttpResponse, Http404 @@ -17,7 +18,14 @@ def render_to_response(*args, **kwargs): Returns a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments. """ - httpresponse_kwargs = {'content_type': kwargs.pop('mimetype', None)} + httpresponse_kwargs = {'content_type': kwargs.pop('content_type', None)} + + mimetype = kwargs.pop('mimetype', None) + if mimetype: + warnings.warn("The mimetype keyword argument is deprecated, use " + "content_type instead", DeprecationWarning, stacklevel=2) + httpresponse_kwargs['content_type'] = mimetype + return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) def render(request, *args, **kwargs): diff --git a/docs/howto/outputting-csv.txt b/docs/howto/outputting-csv.txt index bcc6f3827b..1f9efb5a4b 100644 --- a/docs/howto/outputting-csv.txt +++ b/docs/howto/outputting-csv.txt @@ -20,7 +20,7 @@ Here's an example:: def some_view(request): # Create the HttpResponse object with the appropriate CSV header. - response = HttpResponse(mimetype='text/csv') + response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="somefilename.csv"' writer = csv.writer(response) @@ -92,7 +92,7 @@ Here's an example, which generates the same CSV file as above:: def some_view(request): # Create the HttpResponse object with the appropriate CSV header. - response = HttpResponse(mimetype='text/csv') + response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="somefilename.csv"' # The data is hard-coded here, but you could load it from a database or @@ -111,7 +111,7 @@ Here's an example, which generates the same CSV file as above:: The only difference between this example and the previous example is that this one uses template loading instead of the CSV module. The rest of the code -- -such as the ``mimetype='text/csv'`` -- is the same. +such as the ``content_type='text/csv'`` -- is the same. Then, create the template ``my_template_name.txt``, with this template code: diff --git a/docs/howto/outputting-pdf.txt b/docs/howto/outputting-pdf.txt index 9d87b97710..d15f94f7f4 100644 --- a/docs/howto/outputting-pdf.txt +++ b/docs/howto/outputting-pdf.txt @@ -51,7 +51,7 @@ Here's a "Hello World" example:: def some_view(request): # Create the HttpResponse object with the appropriate PDF headers. - response = HttpResponse(mimetype='application/pdf') + response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' # Create the PDF object, using the response object as its "file." @@ -120,7 +120,7 @@ Here's the above "Hello World" example rewritten to use :mod:`io`:: def some_view(request): # Create the HttpResponse object with the appropriate PDF headers. - response = HttpResponse(mimetype='application/pdf') + response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' buffer = BytesIO() diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 63d65d1e4a..da0d1e212c 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -290,8 +290,14 @@ these changes. specified as a plain string instead of a tuple will be removed and raise an exception. -* The ``mimetype`` argument to :class:`~django.http.HttpResponse` ``__init__`` - will be removed (``content_type`` should be used instead). +* The ``mimetype`` argument to the ``__init__`` methods of + :class:`~django.http.HttpResponse`, + :class:`~django.template.response.SimpleTemplateResponse`, and + :class:`~django.template.response.TemplateResponse`, will be removed. + ``content_type`` should be used instead. This also applies to the + :func:`~django.shortcuts.render_to_response` shortcut and + the sitemamp views, :func:`~django.contrib.sitemaps.views.index` and + :func:`~django.contrib.sitemaps.views.sitemap`. * When :class:`~django.http.HttpResponse` is instantiated with an iterator, or when :attr:`~django.http.HttpResponse.content` is set to an iterator, diff --git a/docs/ref/contrib/admin/actions.txt b/docs/ref/contrib/admin/actions.txt index d7eef623d5..0a302ecd1d 100644 --- a/docs/ref/contrib/admin/actions.txt +++ b/docs/ref/contrib/admin/actions.txt @@ -223,7 +223,7 @@ objects as JSON:: from django.core import serializers def export_as_json(modeladmin, request, queryset): - response = HttpResponse(mimetype="text/javascript") + response = HttpResponse(content_type="application/json") serializers.serialize("json", queryset, stream=response) return response @@ -356,5 +356,3 @@ Conditionally enabling or disabling actions if 'delete_selected' in actions: del actions['delete_selected'] return actions - - diff --git a/docs/ref/template-response.txt b/docs/ref/template-response.txt index 3f5e772737..844b5fa46b 100644 --- a/docs/ref/template-response.txt +++ b/docs/ref/template-response.txt @@ -56,11 +56,11 @@ Attributes Methods ------- -.. method:: SimpleTemplateResponse.__init__(template, context=None, mimetype=None, status=None, content_type=None) +.. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None) Instantiates a :class:`~django.template.response.SimpleTemplateResponse` object - with the given template, context, MIME type and HTTP status. + with the given template, context, content type, and HTTP status. ``template`` The full name of a template, or a sequence of template names. @@ -75,12 +75,15 @@ Methods The HTTP Status code for the response. ``content_type`` - An alias for ``mimetype``. Historically, this parameter was only called - ``mimetype``, but since this is actually the value included in the HTTP - ``Content-Type`` header, it can also include the character set encoding, - which makes it more than just a MIME type specification. If ``mimetype`` - is specified (not ``None``), that value is used. Otherwise, - ``content_type`` is used. If neither is given, + + .. versionchanged:: 1.5 + + Historically, this parameter was only called ``mimetype`` (now + deprecated), but since this is actually the value included in the HTTP + ``Content-Type`` header, it can also include the character set + encoding, which makes it more than just a MIME type specification. If + ``mimetype`` is specified (not ``None``), that value is used. + Otherwise, ``content_type`` is used. If neither is given, :setting:`DEFAULT_CONTENT_TYPE` is used. @@ -144,7 +147,7 @@ TemplateResponse objects Methods ------- -.. method:: TemplateResponse.__init__(request, template, context=None, mimetype=None, status=None, content_type=None, current_app=None) +.. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, current_app=None) Instantiates an ``TemplateResponse`` object with the given template, context, MIME type and HTTP status. @@ -165,12 +168,15 @@ Methods The HTTP Status code for the response. ``content_type`` - An alias for ``mimetype``. Historically, this parameter was only called - ``mimetype``, but since this is actually the value included in the HTTP - ``Content-Type`` header, it can also include the character set encoding, - which makes it more than just a MIME type specification. If ``mimetype`` - is specified (not ``None``), that value is used. Otherwise, - ``content_type`` is used. If neither is given, + + .. versionchanged:: 1.5 + + Historically, this parameter was only called ``mimetype`` (now + deprecated), but since this is actually the value included in the HTTP + ``Content-Type`` header, it can also include the character set + encoding, which makes it more than just a MIME type specification. If + ``mimetype`` is specified (not ``None``), that value is used. + Otherwise, ``content_type`` is used. If neither is given, :setting:`DEFAULT_CONTENT_TYPE` is used. ``current_app`` diff --git a/docs/topics/http/shortcuts.txt b/docs/topics/http/shortcuts.txt index b1b4700b73..68860f123f 100644 --- a/docs/topics/http/shortcuts.txt +++ b/docs/topics/http/shortcuts.txt @@ -50,6 +50,9 @@ Optional arguments The MIME type to use for the resulting document. Defaults to the value of the :setting:`DEFAULT_CONTENT_TYPE` setting. + .. versionchanged:: 1.5 + This parameter used to be called ``mimetype``. + ``status`` The status code for the response. Defaults to ``200``. @@ -87,7 +90,7 @@ This example is equivalent to:: ``render_to_response`` ====================== -.. function:: render_to_response(template_name[, dictionary][, context_instance][, mimetype]) +.. function:: render_to_response(template_name[, dictionary][, context_instance][, content_type]) Renders a given template with a given context dictionary and returns an :class:`~django.http.HttpResponse` object with that rendered text. @@ -121,10 +124,14 @@ Optional arguments my_data_dictionary, context_instance=RequestContext(request)) -``mimetype`` +``content_type`` The MIME type to use for the resulting document. Defaults to the value of the :setting:`DEFAULT_CONTENT_TYPE` setting. + .. versionchanged:: 1.5 + This parameter used to be called ``mimetype``. + + Example ------- @@ -148,7 +155,7 @@ This example is equivalent to:: t = loader.get_template('myapp/template.html') c = Context({'foo': 'bar'}) return HttpResponse(t.render(c), - mimetype="application/xhtml+xml") + content_type="application/xhtml+xml") ``redirect`` ============ diff --git a/tests/regressiontests/views/generic_urls.py b/tests/regressiontests/views/generic_urls.py index 0f214d12d5..d50436279a 100644 --- a/tests/regressiontests/views/generic_urls.py +++ b/tests/regressiontests/views/generic_urls.py @@ -47,7 +47,7 @@ urlpatterns += patterns('', urlpatterns += patterns('regressiontests.views.views', (r'^shortcuts/render_to_response/$', 'render_to_response_view'), (r'^shortcuts/render_to_response/request_context/$', 'render_to_response_view_with_request_context'), - (r'^shortcuts/render_to_response/mimetype/$', 'render_to_response_view_with_mimetype'), + (r'^shortcuts/render_to_response/content_type/$', 'render_to_response_view_with_content_type'), (r'^shortcuts/render/$', 'render_view'), (r'^shortcuts/render/base_context/$', 'render_view_with_base_context'), (r'^shortcuts/render/content_type/$', 'render_view_with_content_type'), diff --git a/tests/regressiontests/views/tests/shortcuts.py b/tests/regressiontests/views/tests/shortcuts.py index 62bd82f4a7..3a5df6a9cb 100644 --- a/tests/regressiontests/views/tests/shortcuts.py +++ b/tests/regressiontests/views/tests/shortcuts.py @@ -21,8 +21,8 @@ class ShortcutTests(TestCase): self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') - def test_render_to_response_with_mimetype(self): - response = self.client.get('/shortcuts/render_to_response/mimetype/') + def test_render_to_response_with_content_type(self): + response = self.client.get('/shortcuts/render_to_response/content_type/') self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b'FOO.BAR..\n') self.assertEqual(response['Content-Type'], 'application/x-rendertest') diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py index 748f07637f..50ad98ac2d 100644 --- a/tests/regressiontests/views/views.py +++ b/tests/regressiontests/views/views.py @@ -68,11 +68,11 @@ def render_to_response_view_with_request_context(request): 'bar': 'BAR', }, context_instance=RequestContext(request)) -def render_to_response_view_with_mimetype(request): +def render_to_response_view_with_content_type(request): return render_to_response('debug/render_test.html', { 'foo': 'FOO', 'bar': 'BAR', - }, mimetype='application/x-rendertest') + }, content_type='application/x-rendertest') def render_view(request): return render(request, 'debug/render_test.html', { @@ -263,4 +263,4 @@ class Klass(object): return technical_500_response(request, *exc_info) def sensitive_method_view(request): - return Klass().method(request) \ No newline at end of file + return Klass().method(request)