2011-05-19 21:36:30 +04:00
|
|
|
"""
|
2012-02-07 15:15:30 +04:00
|
|
|
The :mod:`response` module provides :class:`Response` and :class:`ImmediateResponse` classes.
|
|
|
|
|
|
|
|
`Response` is a subclass of `HttpResponse`, and can be similarly instantiated and returned
|
2012-02-07 18:22:14 +04:00
|
|
|
from any view. It is a bit smarter than Django's `HttpResponse`, for it renders automatically
|
|
|
|
its content to a serial format by using a list of :mod:`renderers`.
|
2012-02-07 15:15:30 +04:00
|
|
|
|
2012-02-07 18:22:14 +04:00
|
|
|
To determine the content type to which it must render, default behaviour is to use standard
|
2012-02-20 13:36:03 +04:00
|
|
|
HTTP Accept header content negotiation. But `Response` also supports overriding the content type
|
2012-02-07 18:22:14 +04:00
|
|
|
by specifying an ``_accept=`` parameter in the URL. Also, `Response` will ignore `Accept` headers
|
|
|
|
from Internet Explorer user agents and use a sensible browser `Accept` header instead.
|
2011-05-19 21:36:30 +04:00
|
|
|
"""
|
|
|
|
|
2012-09-04 12:29:59 +04:00
|
|
|
|
|
|
|
import re
|
2012-02-02 20:19:44 +04:00
|
|
|
from django.template.response import SimpleTemplateResponse
|
2011-01-24 02:08:16 +03:00
|
|
|
from django.core.handlers.wsgi import STATUS_CODE_TEXT
|
2012-09-03 18:57:43 +04:00
|
|
|
from djangorestframework.settings import api_settings
|
2012-02-02 20:19:44 +04:00
|
|
|
from djangorestframework.utils.mediatypes import order_by_precedence
|
|
|
|
from djangorestframework import status
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2012-09-04 12:29:59 +04:00
|
|
|
MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )')
|
|
|
|
|
|
|
|
|
2012-02-25 22:45:17 +04:00
|
|
|
class NotAcceptable(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
class Response(SimpleTemplateResponse):
|
2011-05-10 19:01:58 +04:00
|
|
|
"""
|
|
|
|
An HttpResponse that may include content that hasn't yet been serialized.
|
2012-02-07 18:22:14 +04:00
|
|
|
|
2012-02-20 13:36:03 +04:00
|
|
|
Kwargs:
|
2012-08-27 02:16:18 +04:00
|
|
|
- content(object). The raw content, not yet serialized.
|
|
|
|
This must be native Python data that renderers can handle.
|
|
|
|
(e.g.: `dict`, `str`, ...)
|
2012-02-07 18:22:14 +04:00
|
|
|
- renderers(list/tuple). The renderers to use for rendering the response content.
|
2011-05-10 19:01:58 +04:00
|
|
|
"""
|
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
_ACCEPT_QUERY_PARAM = '_accept' # Allow override of Accept header in URL query params
|
|
|
|
_IGNORE_IE_ACCEPT_HEADER = True
|
|
|
|
|
2012-02-25 22:45:17 +04:00
|
|
|
def __init__(self, content=None, status=None, headers=None, view=None, request=None, renderers=None):
|
2012-02-02 20:19:44 +04:00
|
|
|
# First argument taken by `SimpleTemplateResponse.__init__` is template_name,
|
|
|
|
# which we don't need
|
|
|
|
super(Response, self).__init__(None, status=status)
|
2012-02-07 15:15:30 +04:00
|
|
|
|
2012-02-20 13:36:03 +04:00
|
|
|
self.raw_content = content
|
2011-04-11 19:38:00 +04:00
|
|
|
self.has_content_body = content is not None
|
2012-02-22 00:12:14 +04:00
|
|
|
self.headers = headers and headers[:] or []
|
2012-02-25 22:45:17 +04:00
|
|
|
self.view = view
|
|
|
|
self.request = request
|
|
|
|
self.renderers = renderers
|
|
|
|
|
|
|
|
def get_renderers(self):
|
|
|
|
"""
|
|
|
|
Instantiates and returns the list of renderers the response will use.
|
|
|
|
"""
|
2012-09-03 18:57:43 +04:00
|
|
|
if self.renderers is None:
|
|
|
|
renderer_classes = api_settings.DEFAULT_RENDERERS
|
|
|
|
else:
|
|
|
|
renderer_classes = self.renderers
|
|
|
|
|
|
|
|
return [cls(self.view) for cls in renderer_classes]
|
2012-02-02 20:19:44 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def rendered_content(self):
|
|
|
|
"""
|
2012-02-25 22:45:17 +04:00
|
|
|
The final rendered content. Accessing this attribute triggers the
|
|
|
|
complete rendering cycle: selecting suitable renderer, setting
|
|
|
|
response's actual content type, rendering data.
|
2012-02-02 20:19:44 +04:00
|
|
|
"""
|
|
|
|
renderer, media_type = self._determine_renderer()
|
|
|
|
|
|
|
|
# Set the media type of the response
|
|
|
|
self['Content-Type'] = renderer.media_type
|
|
|
|
|
|
|
|
# Render the response content
|
|
|
|
if self.has_content_body:
|
|
|
|
return renderer.render(self.raw_content, media_type)
|
|
|
|
return renderer.render()
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2012-02-25 22:45:17 +04:00
|
|
|
def render(self):
|
|
|
|
try:
|
|
|
|
return super(Response, self).render()
|
|
|
|
except NotAcceptable:
|
|
|
|
response = self._get_406_response()
|
|
|
|
return response.render()
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
@property
|
|
|
|
def status_text(self):
|
2011-05-10 19:01:58 +04:00
|
|
|
"""
|
2012-02-07 15:15:30 +04:00
|
|
|
Returns reason text corresponding to our HTTP response status code.
|
2011-05-10 19:01:58 +04:00
|
|
|
Provided for convenience.
|
|
|
|
"""
|
2012-02-14 12:05:28 +04:00
|
|
|
return STATUS_CODE_TEXT.get(self.status_code, '')
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
def _determine_accept_list(self):
|
2012-02-07 15:15:30 +04:00
|
|
|
"""
|
|
|
|
Returns a list of accepted media types. This list is determined from :
|
2012-02-20 13:36:03 +04:00
|
|
|
|
2012-02-07 15:15:30 +04:00
|
|
|
1. overload with `_ACCEPT_QUERY_PARAM`
|
2012-02-20 13:36:03 +04:00
|
|
|
2. `Accept` header of the request
|
2012-02-07 15:15:30 +04:00
|
|
|
|
|
|
|
If those are useless, a default value is returned instead.
|
|
|
|
"""
|
2012-02-02 20:19:44 +04:00
|
|
|
request = self.request
|
|
|
|
|
|
|
|
if self._ACCEPT_QUERY_PARAM and request.GET.get(self._ACCEPT_QUERY_PARAM, None):
|
|
|
|
# Use _accept parameter override
|
|
|
|
return [request.GET.get(self._ACCEPT_QUERY_PARAM)]
|
|
|
|
elif (self._IGNORE_IE_ACCEPT_HEADER and
|
|
|
|
'HTTP_USER_AGENT' in request.META and
|
|
|
|
MSIE_USER_AGENT_REGEX.match(request.META['HTTP_USER_AGENT'])):
|
|
|
|
# Ignore MSIE's broken accept behavior and do something sensible instead
|
|
|
|
return ['text/html', '*/*']
|
|
|
|
elif 'HTTP_ACCEPT' in request.META:
|
|
|
|
# Use standard HTTP Accept negotiation
|
|
|
|
return [token.strip() for token in request.META['HTTP_ACCEPT'].split(',')]
|
|
|
|
else:
|
|
|
|
# No accept header specified
|
|
|
|
return ['*/*']
|
|
|
|
|
|
|
|
def _determine_renderer(self):
|
|
|
|
"""
|
2012-02-25 22:45:17 +04:00
|
|
|
Determines the appropriate renderer for the output, given the list of
|
|
|
|
accepted media types, and the :attr:`renderers` set on this class.
|
2012-02-02 20:19:44 +04:00
|
|
|
|
|
|
|
Returns a 2-tuple of `(renderer, media_type)`
|
|
|
|
|
2012-02-25 22:45:17 +04:00
|
|
|
See: RFC 2616, Section 14
|
|
|
|
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
2012-02-02 20:19:44 +04:00
|
|
|
"""
|
2012-02-25 22:45:17 +04:00
|
|
|
|
|
|
|
renderers = self.get_renderers()
|
|
|
|
accepts = self._determine_accept_list()
|
|
|
|
|
|
|
|
# Not acceptable response - Ignore accept header.
|
|
|
|
if self.status_code == 406:
|
|
|
|
return (renderers[0], renderers[0].media_type)
|
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
# Check the acceptable media types against each renderer,
|
|
|
|
# attempting more specific media types first
|
|
|
|
# NB. The inner loop here isn't as bad as it first looks :)
|
|
|
|
# Worst case is we're looping over len(accept_list) * len(self.renderers)
|
2012-09-06 16:49:15 +04:00
|
|
|
for media_type_set in order_by_precedence(accepts):
|
2012-02-25 22:45:17 +04:00
|
|
|
for renderer in renderers:
|
2012-09-06 16:49:15 +04:00
|
|
|
for media_type in media_type_set:
|
2012-02-02 20:19:44 +04:00
|
|
|
if renderer.can_handle_response(media_type):
|
|
|
|
return renderer, media_type
|
|
|
|
|
|
|
|
# No acceptable renderers were found
|
2012-02-25 22:45:17 +04:00
|
|
|
raise NotAcceptable
|
2012-02-02 20:19:44 +04:00
|
|
|
|
2012-02-25 22:45:17 +04:00
|
|
|
def _get_406_response(self):
|
|
|
|
renderer = self.renderers[0]
|
|
|
|
return Response(
|
|
|
|
{
|
|
|
|
'detail': 'Could not satisfy the client\'s Accept header',
|
|
|
|
'available_types': [renderer.media_type
|
|
|
|
for renderer in self.renderers]
|
|
|
|
},
|
|
|
|
status=status.HTTP_406_NOT_ACCEPTABLE,
|
|
|
|
view=self.view, request=self.request, renderers=[renderer])
|