Fixed #16519 -- Deprecated mimetype kwarg of HttpResponse __init__

This keyword was already deprecated in the code (supported for
backwards compatibility only), but never formally deprecated.
Thanks Paul McMillan for the report and yasar11732 for the initial
patch.
This commit is contained in:
Claude Paroz 2012-06-30 21:25:16 +02:00
parent deed192dda
commit da200c5e35
4 changed files with 25 additions and 21 deletions

View File

@ -524,14 +524,16 @@ class HttpResponse(object):
status_code = 200 status_code = 200
def __init__(self, content='', mimetype=None, status=None, def __init__(self, content='', content_type=None, status=None,
content_type=None): mimetype=None):
# _headers is a mapping of the lower-case name to the original case of # _headers is a mapping of the lower-case name to the original case of
# the header (required for working with legacy systems) and the header # the header (required for working with legacy systems) and the header
# value. Both the name of the header and its value are ASCII strings. # value. Both the name of the header and its value are ASCII strings.
self._headers = {} self._headers = {}
self._charset = settings.DEFAULT_CHARSET self._charset = settings.DEFAULT_CHARSET
if mimetype: # For backwards compatibility. if mimetype:
warnings.warn("Using mimetype keyword argument is deprecated, use"
" content_type instead", PendingDeprecationWarning)
content_type = mimetype content_type = mimetype
if not content_type: if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,

View File

@ -9,8 +9,8 @@ class ContentNotRenderedError(Exception):
class SimpleTemplateResponse(HttpResponse): class SimpleTemplateResponse(HttpResponse):
rendering_attrs = ['template_name', 'context_data', '_post_render_callbacks'] rendering_attrs = ['template_name', 'context_data', '_post_render_callbacks']
def __init__(self, template, context=None, mimetype=None, status=None, def __init__(self, template, context=None, content_type=None, status=None,
content_type=None): mimetype=None):
# It would seem obvious to call these next two members 'template' and # It would seem obvious to call these next two members 'template' and
# 'context', but those names are reserved as part of the test Client # 'context', but those names are reserved as part of the test Client
# API. To avoid the name collision, we use tricky-to-debug problems # API. To avoid the name collision, we use tricky-to-debug problems
@ -22,8 +22,8 @@ class SimpleTemplateResponse(HttpResponse):
# content argument doesn't make sense here because it will be replaced # content argument doesn't make sense here because it will be replaced
# with rendered template so we always pass empty string in order to # with rendered template so we always pass empty string in order to
# prevent errors and provide shorter signature. # prevent errors and provide shorter signature.
super(SimpleTemplateResponse, self).__init__('', mimetype, status, super(SimpleTemplateResponse, self).__init__('', content_type, status,
content_type) mimetype)
# _is_rendered tracks whether the template and context has been baked # _is_rendered tracks whether the template and context has been baked
# into a final response. # into a final response.
@ -137,8 +137,8 @@ class TemplateResponse(SimpleTemplateResponse):
rendering_attrs = SimpleTemplateResponse.rendering_attrs + \ rendering_attrs = SimpleTemplateResponse.rendering_attrs + \
['_request', '_current_app'] ['_request', '_current_app']
def __init__(self, request, template, context=None, mimetype=None, def __init__(self, request, template, context=None, content_type=None,
status=None, content_type=None, current_app=None): status=None, mimetype=None, current_app=None):
# self.request gets over-written by django.test.client.Client - and # self.request gets over-written by django.test.client.Client - and
# unlike context_data and template_name the _request should not # unlike context_data and template_name the _request should not
# be considered part of the public API. # be considered part of the public API.
@ -147,7 +147,7 @@ class TemplateResponse(SimpleTemplateResponse):
# having to avoid needing to create the RequestContext directly # having to avoid needing to create the RequestContext directly
self._current_app = current_app self._current_app = current_app
super(TemplateResponse, self).__init__( super(TemplateResponse, self).__init__(
template, context, mimetype, status, content_type) template, context, content_type, status, mimetype)
def resolve_context(self, context): def resolve_context(self, context):
"""Convert context data into a full RequestContext object """Convert context data into a full RequestContext object

View File

@ -280,6 +280,9 @@ these changes.
specified as a plain string instead of a tuple will be removed and raise an specified as a plain string instead of a tuple will be removed and raise an
exception. exception.
* The ``mimetype`` argument to :class:`~django.http.HttpResponse` ``__init__``
will be removed (``content_type`` should be used instead).
2.0 2.0
--- ---

View File

@ -606,11 +606,10 @@ Attributes
Methods Methods
------- -------
.. method:: HttpResponse.__init__(content='', mimetype=None, status=200, content_type=DEFAULT_CONTENT_TYPE) .. method:: HttpResponse.__init__(content='', content_type=None, status=200)
Instantiates an ``HttpResponse`` object with the given page content (a Instantiates an ``HttpResponse`` object with the given page content and
string) and MIME type. The :setting:`DEFAULT_CONTENT_TYPE` is content type.
``'text/html'``.
``content`` should be an iterator or a string. If it's an ``content`` should be an iterator or a string. If it's an
iterator, it should return strings, and those strings will be iterator, it should return strings, and those strings will be
@ -618,15 +617,15 @@ Methods
an iterator or a string, it will be converted to a string when an iterator or a string, it will be converted to a string when
accessed. accessed.
``content_type`` is the MIME type optionally completed by a character set
encoding and is used to fill the HTTP ``Content-Type`` header. If not
specified, it is formed by the :setting:`DEFAULT_CONTENT_TYPE` and
:setting:`DEFAULT_CHARSET` settings, by default: "`text/html; charset=utf-8`".
Historically, this parameter was called ``mimetype`` (now deprecated).
``status`` is the `HTTP Status code`_ for the response. ``status`` is the `HTTP Status code`_ for the response.
``content_type`` is 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, the
:setting:`DEFAULT_CONTENT_TYPE` setting is used.
.. method:: HttpResponse.__setitem__(header, value) .. method:: HttpResponse.__setitem__(header, value)