Ticket #16326: patch-16326-niran.diff

File patch-16326-niran.diff, 2.4 KB (added by Niran Babalola, 13 years ago)
  • django/template/response.py

    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):  
    66
    77class SimpleTemplateResponse(HttpResponse):
    88
     9    RENDERING_ATTRS = ['template_name', 'context_data',
     10        '_post_render_callbacks']
     11
    912    def __init__(self, template, context=None, mimetype=None, status=None,
    1013            content_type=None):
    1114        # It would seem obvious to call these next two members 'template' and
    class SimpleTemplateResponse(HttpResponse):  
    3740        obj_dict = self.__dict__.copy()
    3841        if not self._is_rendered:
    3942            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]
    4346
    4447        return obj_dict
    4548
    class SimpleTemplateResponse(HttpResponse):  
    122125
    123126
    124127class TemplateResponse(SimpleTemplateResponse):
     128
     129    RENDERING_ATTRS = SimpleTemplateResponse.RENDERING_ATTRS + \
     130        ['_request', '_current_app']
     131
    125132    def __init__(self, request, template, context=None, mimetype=None,
    126133            status=None, content_type=None, current_app=None):
    127134        # self.request gets over-written by django.test.client.Client - and
    class TemplateResponse(SimpleTemplateResponse):  
    134141        super(TemplateResponse, self).__init__(
    135142            template, context, mimetype, status, content_type)
    136143
    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 
    151144    def resolve_context(self, context):
    152145        """Convert context data into a full RequestContext object
    153146        (assuming it isn't already a Context object).
Back to Top