2010-12-07 21:57:01 +08:00
|
|
|
from django.http import HttpResponse
|
|
|
|
|
2015-04-25 03:35:30 +08:00
|
|
|
from .loader import get_template, select_template
|
|
|
|
|
2011-07-29 17:40:50 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
class ContentNotRenderedError(Exception):
|
|
|
|
pass
|
|
|
|
|
2011-07-29 17:40:50 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
class SimpleTemplateResponse(HttpResponse):
|
2011-07-29 17:40:50 +08:00
|
|
|
rendering_attrs = ['template_name', 'context_data', '_post_render_callbacks']
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-11-17 01:54:12 +08:00
|
|
|
def __init__(self, template, context=None, content_type=None, status=None,
|
2020-09-15 18:43:37 +08:00
|
|
|
charset=None, using=None, headers=None):
|
2010-12-07 21:57:01 +08:00
|
|
|
# It would seem obvious to call these next two members 'template' and
|
2011-07-29 17:40:50 +08:00
|
|
|
# 'context', but those names are reserved as part of the test Client
|
2015-01-10 05:59:00 +08:00
|
|
|
# API. To avoid the name collision, we use different names.
|
2010-12-07 21:57:01 +08:00
|
|
|
self.template_name = template
|
|
|
|
self.context_data = context
|
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
self.using = using
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
self._post_render_callbacks = []
|
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
# _request stores the current request object in subclasses that know
|
|
|
|
# about requests, like TemplateResponse. It's defined in the base class
|
|
|
|
# to minimize code duplication.
|
|
|
|
# It's called self._request because self.request gets overwritten by
|
|
|
|
# django.test.client.Client. Unlike template_name and context_data,
|
|
|
|
# _request should not be considered part of the public API.
|
|
|
|
self._request = None
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
# content argument doesn't make sense here because it will be replaced
|
|
|
|
# with rendered template so we always pass empty string in order to
|
|
|
|
# prevent errors and provide shorter signature.
|
2020-09-15 18:43:37 +08:00
|
|
|
super().__init__('', content_type, status, charset=charset, headers=headers)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2011-09-15 15:26:35 +08:00
|
|
|
# _is_rendered tracks whether the template and context has been baked
|
|
|
|
# into a final response.
|
|
|
|
# Super __init__ doesn't know any better than to set self.content to
|
|
|
|
# the empty string we just gave it, which wrongly sets _is_rendered
|
|
|
|
# True, so we initialize it to False after the call to super __init__.
|
|
|
|
self._is_rendered = False
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
def __getstate__(self):
|
2016-11-23 22:45:21 +08:00
|
|
|
"""
|
|
|
|
Raise an exception if trying to pickle an unrendered response. Pickle
|
|
|
|
only rendered data, not the data used to construct the response.
|
2011-01-24 22:24:35 +08:00
|
|
|
"""
|
2014-11-01 02:26:27 +08:00
|
|
|
obj_dict = self.__dict__.copy()
|
2011-01-24 22:24:35 +08:00
|
|
|
if not self._is_rendered:
|
2011-07-29 17:40:50 +08:00
|
|
|
raise ContentNotRenderedError('The response content must be '
|
|
|
|
'rendered before it can be pickled.')
|
|
|
|
for attr in self.rendering_attrs:
|
|
|
|
if attr in obj_dict:
|
|
|
|
del obj_dict[attr]
|
2011-01-24 22:24:35 +08:00
|
|
|
|
|
|
|
return obj_dict
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
def resolve_template(self, template):
|
2017-01-25 04:36:36 +08:00
|
|
|
"""Accept a template object, path-to-template, or list of paths."""
|
2010-12-07 21:57:01 +08:00
|
|
|
if isinstance(template, (list, tuple)):
|
2015-04-25 03:35:30 +08:00
|
|
|
return select_template(template, using=self.using)
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(template, str):
|
2015-04-25 03:35:30 +08:00
|
|
|
return get_template(template, using=self.using)
|
2010-12-07 21:57:01 +08:00
|
|
|
else:
|
|
|
|
return template
|
|
|
|
|
|
|
|
def resolve_context(self, context):
|
2015-01-10 05:59:00 +08:00
|
|
|
return context
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
@property
|
|
|
|
def rendered_content(self):
|
2017-01-25 04:36:36 +08:00
|
|
|
"""Return the freshly rendered content for the template and context
|
2010-12-07 21:57:01 +08:00
|
|
|
described by the TemplateResponse.
|
|
|
|
|
|
|
|
This *does not* set the final content of the response. To set the
|
|
|
|
response content, you must either call render(), or set the
|
|
|
|
content explicitly using the value of this property.
|
|
|
|
"""
|
2015-09-06 00:06:34 +08:00
|
|
|
template = self.resolve_template(self.template_name)
|
|
|
|
context = self.resolve_context(self.context_data)
|
2019-04-24 19:09:29 +08:00
|
|
|
return template.render(context, self._request)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
def add_post_render_callback(self, callback):
|
2017-01-25 04:36:36 +08:00
|
|
|
"""Add a new post-rendering callback.
|
2011-01-24 22:24:35 +08:00
|
|
|
|
2011-07-29 17:40:50 +08:00
|
|
|
If the response has already been rendered,
|
|
|
|
invoke the callback immediately.
|
2011-01-24 22:24:35 +08:00
|
|
|
"""
|
|
|
|
if self._is_rendered:
|
|
|
|
callback(self)
|
|
|
|
else:
|
|
|
|
self._post_render_callbacks.append(callback)
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
def render(self):
|
2017-01-25 04:36:36 +08:00
|
|
|
"""Render (thereby finalizing) the content of the response.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
If the content has already been rendered, this is a no-op.
|
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
Return the baked response instance.
|
2010-12-07 21:57:01 +08:00
|
|
|
"""
|
2011-05-25 05:28:43 +08:00
|
|
|
retval = self
|
2010-12-07 21:57:01 +08:00
|
|
|
if not self._is_rendered:
|
2012-08-22 23:48:22 +08:00
|
|
|
self.content = self.rendered_content
|
2011-01-24 22:24:35 +08:00
|
|
|
for post_callback in self._post_render_callbacks:
|
2011-05-25 05:28:43 +08:00
|
|
|
newretval = post_callback(retval)
|
|
|
|
if newretval is not None:
|
|
|
|
retval = newretval
|
|
|
|
return retval
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2011-07-29 17:40:50 +08:00
|
|
|
@property
|
|
|
|
def is_rendered(self):
|
|
|
|
return self._is_rendered
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
if not self._is_rendered:
|
2016-03-29 06:33:29 +08:00
|
|
|
raise ContentNotRenderedError(
|
|
|
|
'The response content must be rendered before it can be iterated over.'
|
|
|
|
)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().__iter__()
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2012-08-22 23:48:22 +08:00
|
|
|
@property
|
|
|
|
def content(self):
|
2010-12-07 21:57:01 +08:00
|
|
|
if not self._is_rendered:
|
2016-03-29 06:33:29 +08:00
|
|
|
raise ContentNotRenderedError(
|
|
|
|
'The response content must be rendered before it can be accessed.'
|
|
|
|
)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().content
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2012-08-22 23:48:22 +08:00
|
|
|
@content.setter
|
|
|
|
def content(self, value):
|
2017-01-25 04:36:36 +08:00
|
|
|
"""Set the content for the response."""
|
2012-08-22 23:48:22 +08:00
|
|
|
HttpResponse.content.fset(self, value)
|
2010-12-07 21:57:01 +08:00
|
|
|
self._is_rendered = True
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateResponse(SimpleTemplateResponse):
|
2015-09-04 03:52:04 +08:00
|
|
|
rendering_attrs = SimpleTemplateResponse.rendering_attrs + ['_request']
|
2011-07-29 17:40:50 +08:00
|
|
|
|
2012-07-01 03:25:16 +08:00
|
|
|
def __init__(self, request, template, context=None, content_type=None,
|
2020-09-15 18:43:37 +08:00
|
|
|
status=None, charset=None, using=None, headers=None):
|
|
|
|
super().__init__(template, context, content_type, status, charset, using, headers=headers)
|
2015-01-10 05:59:00 +08:00
|
|
|
self._request = request
|