commit fdf353340b90f40758d46e1402ed7eadf73726ca
Author: Niran Babalola <niran@niran.org>
Date: Wed Jun 22 19:15:02 2011 -0500
Fixed #16326 -- Allow TemplateReponses to be re-pickled
diff --git a/django/template/response.py b/django/template/response.py
index 73645a7..9d1ddd6 100644
a
|
b
|
class ContentNotRenderedError(Exception):
|
6 | 6 | |
7 | 7 | class SimpleTemplateResponse(HttpResponse): |
8 | 8 | |
| 9 | RENDERING_ATTRS = ['template_name', 'context_data', |
| 10 | '_post_render_callbacks'] |
| 11 | |
9 | 12 | def __init__(self, template, context=None, mimetype=None, status=None, |
10 | 13 | content_type=None): |
11 | 14 | # It would seem obvious to call these next two members 'template' and |
… |
… |
class SimpleTemplateResponse(HttpResponse):
|
37 | 40 | obj_dict = self.__dict__.copy() |
38 | 41 | if not self._is_rendered: |
39 | 42 | raise ContentNotRenderedError('The response content must be rendered before it can be pickled.') |
40 | | del obj_dict['template_name'] |
41 | | del obj_dict['context_data'] |
42 | | del obj_dict['_post_render_callbacks'] |
| 43 | for attr in self.RENDERING_ATTRS: |
| 44 | if attr in obj_dict: |
| 45 | del obj_dict[attr] |
43 | 46 | |
44 | 47 | return obj_dict |
45 | 48 | |
… |
… |
class SimpleTemplateResponse(HttpResponse):
|
122 | 125 | |
123 | 126 | |
124 | 127 | class TemplateResponse(SimpleTemplateResponse): |
| 128 | |
| 129 | RENDERING_ATTRS = SimpleTemplateResponse.RENDERING_ATTRS + \ |
| 130 | ['_request', '_current_app'] |
| 131 | |
125 | 132 | def __init__(self, request, template, context=None, mimetype=None, |
126 | 133 | status=None, content_type=None, current_app=None): |
127 | 134 | # self.request gets over-written by django.test.client.Client - and |
… |
… |
class TemplateResponse(SimpleTemplateResponse):
|
134 | 141 | super(TemplateResponse, self).__init__( |
135 | 142 | template, context, mimetype, status, content_type) |
136 | 143 | |
137 | | def __getstate__(self): |
138 | | """Pickling support function. |
139 | | |
140 | | Ensures that the object can't be pickled before it has been |
141 | | rendered, and that the pickled state only includes rendered |
142 | | data, not the data used to construct the response. |
143 | | """ |
144 | | obj_dict = super(TemplateResponse, self).__getstate__() |
145 | | |
146 | | del obj_dict['_request'] |
147 | | del obj_dict['_current_app'] |
148 | | |
149 | | return obj_dict |
150 | | |
151 | 144 | def resolve_context(self, context): |
152 | 145 | """Convert context data into a full RequestContext object |
153 | 146 | (assuming it isn't already a Context object). |