Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/rest_framework/response.py: 64%
50 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2The Response class in REST framework is similar to HTTPResponse, except that
3it is initialized with unrendered data, instead of a pre-rendered string.
5The appropriate renderer is called during Django's template response rendering.
6"""
7from http.client import responses
9from django.template.response import SimpleTemplateResponse
11from rest_framework.serializers import Serializer
14class Response(SimpleTemplateResponse):
15 """
16 An HttpResponse that allows its data to be rendered into
17 arbitrary media types.
18 """
20 def __init__(self, data=None, status=None,
21 template_name=None, headers=None,
22 exception=False, content_type=None):
23 """
24 Alters the init arguments slightly.
25 For example, drop 'template_name', and instead use 'data'.
27 Setting 'renderer' and 'media_type' will typically be deferred,
28 For example being set automatically by the `APIView`.
29 """
30 super().__init__(None, status=status)
32 if isinstance(data, Serializer): 32 ↛ 33line 32 didn't jump to line 33
33 msg = (
34 'You passed a Serializer instance as data, but '
35 'probably meant to pass serialized `.data` or '
36 '`.error`. representation.'
37 )
38 raise AssertionError(msg)
40 self.data = data
41 self.template_name = template_name
42 self.exception = exception
43 self.content_type = content_type
45 if headers: 45 ↛ 46line 45 didn't jump to line 46, because the condition on line 45 was never true
46 for name, value in headers.items():
47 self[name] = value
49 @property
50 def rendered_content(self):
51 renderer = getattr(self, 'accepted_renderer', None)
52 accepted_media_type = getattr(self, 'accepted_media_type', None)
53 context = getattr(self, 'renderer_context', None)
55 assert renderer, ".accepted_renderer not set on Response"
56 assert accepted_media_type, ".accepted_media_type not set on Response"
57 assert context is not None, ".renderer_context not set on Response"
58 context['response'] = self
60 media_type = renderer.media_type
61 charset = renderer.charset
62 content_type = self.content_type
64 if content_type is None and charset is not None: 64 ↛ 65line 64 didn't jump to line 65, because the condition on line 64 was never true
65 content_type = "{}; charset={}".format(media_type, charset)
66 elif content_type is None: 66 ↛ 68line 66 didn't jump to line 68, because the condition on line 66 was never false
67 content_type = media_type
68 self['Content-Type'] = content_type
70 ret = renderer.render(self.data, accepted_media_type, context)
71 if isinstance(ret, str): 71 ↛ 72line 71 didn't jump to line 72, because the condition on line 71 was never true
72 assert charset, (
73 'renderer returned unicode, and did not specify '
74 'a charset value.'
75 )
76 return ret.encode(charset)
78 if not ret:
79 del self['Content-Type']
81 return ret
83 @property
84 def status_text(self):
85 """
86 Returns reason text corresponding to our HTTP response status code.
87 Provided for convenience.
88 """
89 return responses.get(self.status_code, '')
91 def __getstate__(self):
92 """
93 Remove attributes from the response that shouldn't be cached.
94 """
95 state = super().__getstate__()
96 for key in (
97 'accepted_renderer', 'renderer_context', 'resolver_match',
98 'client', 'request', 'json', 'wsgi_request'
99 ):
100 if key in state:
101 del state[key]
102 state['_closable_objects'] = []
103 return state