Commit 11ec0253 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

[1.5.x] Fixed #19692 -- Completed deprecation of mimetype in favor of content_type.

Thanks Tim for the report and initial patch.

Backport of 89cb771b from master.
parent 256352a7
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
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", PendingDeprecationWarning, 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", PendingDeprecationWarning, 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)
+9 −1
Original line number Diff line number Diff line
@@ -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", PendingDeprecationWarning, stacklevel=2)
        httpresponse_kwargs['content_type'] = mimetype

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

def render(request, *args, **kwargs):
+3 −3
Original line number Diff line number Diff line
@@ -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:

+2 −2
Original line number Diff line number Diff line
@@ -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()
+8 −2
Original line number Diff line number Diff line
@@ -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,
Loading