@@ -717,117 +717,3 @@ def __init__(
717
717
kwargs .setdefault ("content_type" , "application/json" )
718
718
data = json .dumps (data , cls = encoder , ** json_dumps_params )
719
719
super ().__init__ (content = data , ** kwargs )
720
-
721
-
722
- class ContentNotRenderedError (Exception ):
723
- pass
724
-
725
-
726
- class RenderableResponse (HttpResponse ):
727
- non_picklable_attrs = HttpResponse .non_picklable_attrs | frozenset (
728
- ["render_func" , "context_data" , "_post_render_callbacks" , "_request" ]
729
- )
730
-
731
- def __init__ (
732
- self ,
733
- render_func ,
734
- context = None ,
735
- content_type = None ,
736
- status = None ,
737
- charset = None ,
738
- headers = None ,
739
- ):
740
- self .render_func = render_func
741
-
742
- # It would seem obvious to call these next two members 'template' and
743
- # 'context', but those names are reserved as part of the test Client
744
- # API. To avoid the name collision, we use different names.
745
- self .context_data = context
746
-
747
- self ._post_render_callbacks = []
748
-
749
- # content argument doesn't make sense here because it will be replaced
750
- # with rendered template so we always pass empty string in order to
751
- # prevent errors and provide shorter signature.
752
- super ().__init__ ("" , content_type , status , charset = charset , headers = headers )
753
-
754
- # _is_rendered tracks whether the template and context has been baked
755
- # into a final response.
756
- # Super __init__ doesn't know any better than to set self.content to
757
- # the empty string we just gave it, which wrongly sets _is_rendered
758
- # True, so we initialize it to False after the call to super __init__.
759
- self ._is_rendered = False
760
-
761
- def __getstate__ (self ):
762
- """
763
- Raise an exception if trying to pickle an unrendered response. Pickle
764
- only rendered data, not the data used to construct the response.
765
- """
766
- if not self ._is_rendered :
767
- raise ContentNotRenderedError (
768
- "The response content must be rendered before it can be pickled."
769
- )
770
- return super ().__getstate__ ()
771
-
772
- @property
773
- def rendered_content (self ):
774
- """Return the freshly rendered content for the template and context
775
- described by the TemplateResponse.
776
-
777
- This *does not* set the final content of the response. To set the
778
- response content, you must either call render(), or set the
779
- content explicitly using the value of this property.
780
- """
781
- return self .render_func (self .context_data )
782
-
783
- def add_post_render_callback (self , callback ):
784
- """Add a new post-rendering callback.
785
-
786
- If the response has already been rendered,
787
- invoke the callback immediately.
788
- """
789
- if self ._is_rendered :
790
- callback (self )
791
- else :
792
- self ._post_render_callbacks .append (callback )
793
-
794
- def render (self ):
795
- """Render (thereby finalizing) the content of the response.
796
-
797
- If the content has already been rendered, this is a no-op.
798
-
799
- Return the baked response instance.
800
- """
801
- retval = self
802
- if not self ._is_rendered :
803
- self .content = self .rendered_content
804
- for post_callback in self ._post_render_callbacks :
805
- newretval = post_callback (retval )
806
- if newretval is not None :
807
- retval = newretval
808
- return retval
809
-
810
- @property
811
- def is_rendered (self ):
812
- return self ._is_rendered
813
-
814
- def __iter__ (self ):
815
- if not self ._is_rendered :
816
- raise ContentNotRenderedError (
817
- "The response content must be rendered before it can be iterated over."
818
- )
819
- return super ().__iter__ ()
820
-
821
- @property
822
- def content (self ):
823
- if not self ._is_rendered :
824
- raise ContentNotRenderedError (
825
- "The response content must be rendered before it can be accessed."
826
- )
827
- return super ().content
828
-
829
- @content .setter
830
- def content (self , value ):
831
- """Set the content for the response."""
832
- HttpResponse .content .fset (self , value )
833
- self ._is_rendered = True
0 commit comments