<s> from django . http import HttpResponse <EOL> from django . template import loader , Context , RequestContext <EOL> from django . utils import six <EOL> class ContentNotRenderedError ( Exception ) : <EOL> pass <EOL> class SimpleTemplateResponse ( HttpResponse ) : <EOL> rendering_attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , template , context = None , content_type = None , status = None , <EOL> mimetype = None ) : <EOL> self . template_name = template <EOL> self . context_data = context <EOL> self . _post_render_callbacks = [ ] <EOL> super ( SimpleTemplateResponse , self ) . __init__ ( '<STR_LIT>' , content_type , status , <EOL> mimetype ) <EOL> self . _is_rendered = False <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> obj_dict = super ( SimpleTemplateResponse , self ) . __getstate__ ( ) <EOL> if not self . _is_rendered : <EOL> raise ContentNotRenderedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for attr in self . rendering_attrs : <EOL> if attr in obj_dict : <EOL> del obj_dict [ attr ] <EOL> return obj_dict <EOL> def resolve_template ( self , template ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( template , ( list , tuple ) ) : <EOL> return loader . select_template ( template ) <EOL> elif isinstance ( template , six . string_types ) : <EOL> return loader . get_template ( template ) <EOL> else : <EOL> return template <EOL> def resolve_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( context , Context ) : <EOL> return context <EOL> else : <EOL> return Context ( context ) <EOL> @ property <EOL> def rendered_content ( self ) : <EOL> """<STR_LIT>""" <EOL> template = self . resolve_template ( self . template_name ) <EOL> context = self . resolve_context ( self . context_data ) <EOL> content = template . render ( context ) <EOL> return content <EOL> def add_post_render_callback ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_rendered : <EOL> callback ( self ) <EOL> else : <EOL> self . _post_render_callbacks . append ( callback ) <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> retval = self <EOL> if not self . _is_rendered : <EOL> self . content = self . rendered_content <EOL> for post_callback in self . _post_render_callbacks : <EOL> newretval = post_callback ( retval ) <EOL> if newretval is not None : <EOL> retval = newretval <EOL> return retval <EOL> @ property <EOL> def is_rendered ( self ) : <EOL> return self . _is_rendered <EOL> def __iter__ ( self ) : <EOL> if not self . _is_rendered : <EOL> raise ContentNotRenderedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return super ( SimpleTemplateResponse , self ) . __iter__ ( ) <EOL> @ property <EOL> def content ( self ) : <EOL> if not self . _is_rendered : <EOL> raise ContentNotRenderedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return super ( SimpleTemplateResponse , self ) . content <EOL> @ content . setter <EOL> def content ( self , value ) : <EOL> """<STR_LIT>""" <EOL> HttpResponse . content . fset ( self , value ) <EOL> self . _is_rendered = True <EOL> class TemplateResponse ( SimpleTemplateResponse ) : <EOL> rendering_attrs = SimpleTemplateResponse . rendering_attrs + [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , request , template , context = None , content_type = None , <EOL> status = None , mimetype = None , current_app = None ) : <EOL> self . _request = request <EOL> self . _current_app = current_app <EOL> super ( TemplateResponse , self ) . __init__ ( <EOL> template , context , content_type , status , mimetype ) <EOL> def resolve_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( context , Context ) : <EOL> return context <EOL> return RequestContext ( self . _request , context , current_app = self . _current_app ) </s>
