2010-12-07 21:57:01 +08:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import loader, Context, RequestContext
|
|
|
|
|
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
|
|
|
|
2012-07-01 03:25:16 +08:00
|
|
|
def __init__(self, template, context=None, content_type=None, status=None,
|
|
|
|
mimetype=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
|
|
|
|
# API. To avoid the name collision, we use tricky-to-debug problems
|
2010-12-07 21:57:01 +08:00
|
|
|
self.template_name = template
|
|
|
|
self.context_data = context
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
self._post_render_callbacks = []
|
|
|
|
|
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.
|
2012-07-01 03:25:16 +08:00
|
|
|
super(SimpleTemplateResponse, self).__init__('', content_type, status,
|
|
|
|
mimetype)
|
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):
|
|
|
|
"""Pickling support function.
|
|
|
|
|
|
|
|
Ensures that the object can't be pickled before it has been
|
|
|
|
rendered, and that the pickled state only includes rendered
|
|
|
|
data, not the data used to construct the response.
|
|
|
|
"""
|
|
|
|
obj_dict = self.__dict__.copy()
|
|
|
|
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):
|
|
|
|
"Accepts a template object, path-to-template or list of paths"
|
|
|
|
if isinstance(template, (list, tuple)):
|
|
|
|
return loader.select_template(template)
|
|
|
|
elif isinstance(template, basestring):
|
|
|
|
return loader.get_template(template)
|
|
|
|
else:
|
|
|
|
return template
|
|
|
|
|
|
|
|
def resolve_context(self, context):
|
2011-07-29 17:40:50 +08:00
|
|
|
"""Converts context data into a full Context object
|
2010-12-07 21:57:01 +08:00
|
|
|
(assuming it isn't already a Context object).
|
|
|
|
"""
|
|
|
|
if isinstance(context, Context):
|
|
|
|
return context
|
|
|
|
else:
|
|
|
|
return Context(context)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rendered_content(self):
|
|
|
|
"""Returns the freshly rendered content for the template and context
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
template = self.resolve_template(self.template_name)
|
|
|
|
context = self.resolve_context(self.context_data)
|
|
|
|
content = template.render(context)
|
|
|
|
return content
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
def add_post_render_callback(self, callback):
|
2011-07-29 17:40:50 +08:00
|
|
|
"""Adds 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):
|
2011-07-29 17:40:50 +08:00
|
|
|
"""Renders (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.
|
|
|
|
|
|
|
|
Returns the baked response instance.
|
|
|
|
"""
|
2011-05-25 05:28:43 +08:00
|
|
|
retval = self
|
2010-12-07 21:57:01 +08:00
|
|
|
if not self._is_rendered:
|
|
|
|
self._set_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:
|
2011-07-29 17:40:50 +08:00
|
|
|
raise ContentNotRenderedError('The response content must be '
|
|
|
|
'rendered before it can be iterated over.')
|
2010-12-07 21:57:01 +08:00
|
|
|
return super(SimpleTemplateResponse, self).__iter__()
|
|
|
|
|
|
|
|
def _get_content(self):
|
|
|
|
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 accessed.')
|
2010-12-07 21:57:01 +08:00
|
|
|
return super(SimpleTemplateResponse, self)._get_content()
|
|
|
|
|
|
|
|
def _set_content(self, value):
|
2011-07-29 17:40:50 +08:00
|
|
|
"""Sets the content for the response
|
|
|
|
"""
|
2010-12-07 21:57:01 +08:00
|
|
|
super(SimpleTemplateResponse, self)._set_content(value)
|
|
|
|
self._is_rendered = True
|
|
|
|
|
|
|
|
content = property(_get_content, _set_content)
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateResponse(SimpleTemplateResponse):
|
2011-07-29 17:40:50 +08:00
|
|
|
rendering_attrs = SimpleTemplateResponse.rendering_attrs + \
|
|
|
|
['_request', '_current_app']
|
|
|
|
|
2012-07-01 03:25:16 +08:00
|
|
|
def __init__(self, request, template, context=None, content_type=None,
|
|
|
|
status=None, mimetype=None, current_app=None):
|
2010-12-07 21:57:01 +08:00
|
|
|
# self.request gets over-written by django.test.client.Client - and
|
|
|
|
# unlike context_data and template_name the _request should not
|
|
|
|
# be considered part of the public API.
|
|
|
|
self._request = request
|
2011-01-06 06:41:43 +08:00
|
|
|
# As a convenience we'll allow callers to provide current_app without
|
|
|
|
# having to avoid needing to create the RequestContext directly
|
|
|
|
self._current_app = current_app
|
2010-12-07 21:57:01 +08:00
|
|
|
super(TemplateResponse, self).__init__(
|
2012-07-01 03:25:16 +08:00
|
|
|
template, context, content_type, status, mimetype)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def resolve_context(self, context):
|
|
|
|
"""Convert context data into a full RequestContext object
|
|
|
|
(assuming it isn't already a Context object).
|
|
|
|
"""
|
|
|
|
if isinstance(context, Context):
|
|
|
|
return context
|
2011-07-29 17:40:50 +08:00
|
|
|
return RequestContext(self._request, context, current_app=self._current_app)
|