2013-04-25 15:47:34 +04:00
|
|
|
"""
|
2013-05-28 18:09:23 +04:00
|
|
|
The Response class in REST framework is similar to HTTPResponse, except that
|
2013-04-25 15:47:34 +04:00
|
|
|
it is initialized with unrendered data, instead of a pre-rendered string.
|
|
|
|
|
|
|
|
The appropriate renderer is called during Django's template response rendering.
|
|
|
|
"""
|
2019-04-30 18:53:44 +03:00
|
|
|
from http.client import responses
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.template.response import SimpleTemplateResponse
|
2013-01-03 14:41:07 +04:00
|
|
|
|
2015-08-07 20:12:37 +03:00
|
|
|
from rest_framework.serializers import Serializer
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
class Response(SimpleTemplateResponse):
|
|
|
|
"""
|
2013-06-02 08:12:14 +04:00
|
|
|
An HttpResponse that allows its data to be rendered into
|
2012-09-20 16:06:27 +04:00
|
|
|
arbitrary media types.
|
|
|
|
"""
|
|
|
|
|
2014-08-01 12:20:10 +04:00
|
|
|
def __init__(self, data=None, status=None,
|
2012-11-06 14:44:19 +04:00
|
|
|
template_name=None, headers=None,
|
2013-05-21 00:18:17 +04:00
|
|
|
exception=False, content_type=None):
|
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`.
|
|
|
|
"""
|
2019-04-30 18:53:44 +03:00
|
|
|
super().__init__(None, status=status)
|
2015-08-07 20:12:37 +03:00
|
|
|
|
|
|
|
if isinstance(data, Serializer):
|
|
|
|
msg = (
|
|
|
|
'You passed a Serializer instance as data, but '
|
|
|
|
'probably meant to pass serialized `.data` or '
|
|
|
|
'`.error`. representation.'
|
|
|
|
)
|
|
|
|
raise AssertionError(msg)
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
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
|
2013-05-21 00:18:17 +04:00
|
|
|
self.content_type = content_type
|
2012-11-25 02:19:03 +04:00
|
|
|
|
2012-11-13 21:07:38 +04:00
|
|
|
if headers:
|
2019-04-30 18:53:44 +03:00
|
|
|
for name, value in headers.items():
|
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)
|
2016-07-27 13:49:01 +03:00
|
|
|
accepted_media_type = getattr(self, 'accepted_media_type', None)
|
2012-10-10 15:15:18 +04:00
|
|
|
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"
|
2016-07-27 13:49:01 +03:00
|
|
|
assert accepted_media_type, ".accepted_media_type not set on Response"
|
2016-09-17 05:13:34 +03:00
|
|
|
assert context is not None, ".renderer_context not set on Response"
|
2012-10-10 15:15:18 +04:00
|
|
|
context['response'] = self
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2016-07-11 14:04:11 +03:00
|
|
|
media_type = renderer.media_type
|
2013-05-21 00:18:17 +04:00
|
|
|
charset = renderer.charset
|
|
|
|
content_type = self.content_type
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-21 00:18:17 +04:00
|
|
|
if content_type is None and charset is not None:
|
2019-04-30 18:53:44 +03:00
|
|
|
content_type = "{}; charset={}".format(media_type, charset)
|
2013-05-21 00:18:17 +04:00
|
|
|
elif content_type is None:
|
2013-05-18 20:21:43 +04:00
|
|
|
content_type = media_type
|
|
|
|
self['Content-Type'] = content_type
|
2013-05-21 00:00:56 +04:00
|
|
|
|
2016-07-27 13:49:01 +03:00
|
|
|
ret = renderer.render(self.data, accepted_media_type, context)
|
2019-04-30 18:53:44 +03:00
|
|
|
if isinstance(ret, str):
|
2014-08-19 16:28:07 +04:00
|
|
|
assert charset, (
|
|
|
|
'renderer returned unicode, and did not specify '
|
|
|
|
'a charset value.'
|
|
|
|
)
|
2013-05-21 00:18:17 +04:00
|
|
|
return bytes(ret.encode(charset))
|
2013-12-03 20:55:11 +04:00
|
|
|
|
|
|
|
if not ret:
|
|
|
|
del self['Content-Type']
|
|
|
|
|
2013-05-21 00:00:56 +04:00
|
|
|
return ret
|
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.
|
|
|
|
"""
|
2015-03-25 20:27:47 +03:00
|
|
|
return responses.get(self.status_code, '')
|
2012-11-04 15:48:41 +04:00
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
"""
|
2015-01-23 15:56:55 +03:00
|
|
|
Remove attributes from the response that shouldn't be cached.
|
2012-11-04 15:48:41 +04:00
|
|
|
"""
|
2019-04-30 18:53:44 +03:00
|
|
|
state = super().__getstate__()
|
2015-01-23 15:56:55 +03:00
|
|
|
for key in (
|
|
|
|
'accepted_renderer', 'renderer_context', 'resolver_match',
|
2015-08-28 13:55:23 +03:00
|
|
|
'client', 'request', 'json', 'wsgi_request'
|
2015-01-23 15:56:55 +03:00
|
|
|
):
|
2012-11-04 15:48:41 +04:00
|
|
|
if key in state:
|
|
|
|
del state[key]
|
2015-01-31 11:27:17 +03:00
|
|
|
state['_closable_objects'] = []
|
2013-05-18 20:21:43 +04:00
|
|
|
return state
|