2012-11-25 02:19:03 +04:00
|
|
|
import six
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.core.handlers.wsgi import STATUS_CODE_TEXT
|
2012-09-20 20:44:34 +04:00
|
|
|
from django.template.response import SimpleTemplateResponse
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Response(SimpleTemplateResponse):
|
|
|
|
"""
|
|
|
|
An HttpResponse that allows it's data to be rendered into
|
|
|
|
arbitrary media types.
|
|
|
|
"""
|
|
|
|
|
2012-10-05 16:04:34 +04:00
|
|
|
def __init__(self, data=None, status=200,
|
2012-11-06 14:44:19 +04:00
|
|
|
template_name=None, headers=None,
|
|
|
|
exception=False):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Alters the init arguments slightly.
|
|
|
|
For example, drop 'template_name', and instead use 'data'.
|
|
|
|
|
2012-11-14 22:36:29 +04:00
|
|
|
Setting 'renderer' and 'media_type' will typically be deferred,
|
2012-09-20 16:06:27 +04:00
|
|
|
For example being set automatically by the `APIView`.
|
|
|
|
"""
|
|
|
|
super(Response, self).__init__(None, status=status)
|
|
|
|
self.data = data
|
2012-10-05 15:13:44 +04:00
|
|
|
self.template_name = template_name
|
2012-11-06 14:44:19 +04:00
|
|
|
self.exception = exception
|
2012-11-25 02:19:03 +04:00
|
|
|
|
2012-11-13 21:07:38 +04:00
|
|
|
if headers:
|
2012-11-25 02:19:03 +04:00
|
|
|
for name, value in six.iteritems(headers):
|
2012-11-13 21:07:38 +04:00
|
|
|
self[name] = value
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def rendered_content(self):
|
2012-10-10 15:15:18 +04:00
|
|
|
renderer = getattr(self, 'accepted_renderer', None)
|
|
|
|
media_type = getattr(self, 'accepted_media_type', None)
|
|
|
|
context = getattr(self, 'renderer_context', None)
|
2012-10-05 13:23:47 +04:00
|
|
|
|
2012-10-10 15:15:18 +04:00
|
|
|
assert renderer, ".accepted_renderer not set on Response"
|
|
|
|
assert media_type, ".accepted_media_type not set on Response"
|
|
|
|
assert context, ".renderer_context not set on Response"
|
|
|
|
context['response'] = self
|
2012-09-30 18:55:24 +04:00
|
|
|
|
2012-10-05 16:04:34 +04:00
|
|
|
self['Content-Type'] = media_type
|
2012-10-10 15:15:18 +04:00
|
|
|
return renderer.render(self.data, media_type, context)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status_text(self):
|
|
|
|
"""
|
|
|
|
Returns reason text corresponding to our HTTP response status code.
|
|
|
|
Provided for convenience.
|
|
|
|
"""
|
2012-10-10 15:15:18 +04:00
|
|
|
# TODO: Deprecate and use a template tag instead
|
|
|
|
# TODO: Status code text for RFC 6585 status codes
|
2012-09-20 16:06:27 +04:00
|
|
|
return STATUS_CODE_TEXT.get(self.status_code, '')
|
2012-11-04 15:48:41 +04:00
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
"""
|
|
|
|
Remove attributes from the response that shouldn't be cached
|
|
|
|
"""
|
|
|
|
state = super(Response, self).__getstate__()
|
|
|
|
for key in ('accepted_renderer', 'renderer_context', 'data'):
|
|
|
|
if key in state:
|
|
|
|
del state[key]
|
|
|
|
return state
|