Skip to content

Commit

Permalink
[1.5.x] Fixed #19692 -- Completed deprecation of mimetype in favor of…
Browse files Browse the repository at this point in the history
… content_type.

Thanks Tim for the report and initial patch.

Backport of 89cb771 from master.
  • Loading branch information
aaugustin committed Jan 31, 2013
1 parent 256352a commit 11ec025
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 40 deletions.
26 changes: 21 additions & 5 deletions 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
Expand All @@ -6,8 +8,15 @@
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", PendingDeprecationWarning, stacklevel=2)
content_type = mimetype

req_protocol = 'https' if request.is_secure() else 'http'
req_site = get_current_site(request)

Expand All @@ -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", PendingDeprecationWarning, stacklevel=2)
content_type = mimetype

req_protocol = 'https' if request.is_secure() else 'http'
req_site = get_current_site(request)

Expand All @@ -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)
10 changes: 9 additions & 1 deletion django/shortcuts/__init__.py
Expand Up @@ -3,6 +3,7 @@
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
Expand All @@ -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", PendingDeprecationWarning, stacklevel=2)
httpresponse_kwargs['content_type'] = mimetype

return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

def render(request, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions docs/howto/outputting-csv.txt
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions docs/howto/outputting-pdf.txt
Expand Up @@ -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."
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions docs/internals/deprecation.txt
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions docs/ref/contrib/admin/actions.txt
Expand Up @@ -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

Expand Down Expand Up @@ -356,5 +356,3 @@ Conditionally enabling or disabling actions
if 'delete_selected' in actions:
del actions['delete_selected']
return actions


36 changes: 21 additions & 15 deletions docs/ref/template-response.txt
Expand Up @@ -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.
Expand All @@ -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.


Expand Down Expand Up @@ -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.
Expand All @@ -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``
Expand Down
13 changes: 10 additions & 3 deletions docs/topics/http/shortcuts.txt
Expand Up @@ -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``.

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
-------

Expand All @@ -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``
============
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/views/generic_urls.py
Expand Up @@ -47,7 +47,7 @@
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'),
Expand Down
4 changes: 2 additions & 2 deletions tests/regressiontests/views/tests/shortcuts.py
Expand Up @@ -21,8 +21,8 @@ def test_render_to_response_with_request_context(self):
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')
Expand Down
6 changes: 3 additions & 3 deletions tests/regressiontests/views/views.py
Expand Up @@ -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', {
Expand Down Expand Up @@ -263,4 +263,4 @@ def method(self, request):
return technical_500_response(request, *exc_info)

def sensitive_method_view(request):
return Klass().method(request)
return Klass().method(request)

0 comments on commit 11ec025

Please sign in to comment.