Ticket #16004: 16004.fix.alternative.diff
File 16004.fix.alternative.diff, 5.5 KB (added by , 13 years ago) |
---|
-
django/template/response.py
diff -r 128c90a7b280 django/template/response.py
a b 92 92 93 93 Returns the baked response instance. 94 94 """ 95 retval = self 95 96 if not self._is_rendered: 96 97 self._set_content(self.rendered_content) 97 98 for post_callback in self._post_render_callbacks: 98 post_callback(self) 99 return self 99 newretval = post_callback(retval) 100 if newretval is not None: 101 retval = newretval 102 return retval 100 103 101 104 is_rendered = property(lambda self: self._is_rendered) 102 105 -
django/utils/decorators.py
diff -r 128c90a7b280 django/utils/decorators.py
a b 95 95 if result is not None: 96 96 return result 97 97 raise 98 if hasattr(middleware, 'process_response'): 99 result = middleware.process_response(request, response) 100 if result is not None: 101 return result 98 if hasattr(response, 'render') and callable(response.render): 99 if hasattr(middleware, 'process_template_response'): 100 response = middleware.process_template_response(request, response) 101 # Defer running of process_response, so that we don't 102 # force the template to be rendered just yet. 103 if hasattr(middleware, 'process_response'): 104 callback = lambda response: middleware.process_response(request, response) 105 response.add_post_render_callback(callback) 106 else: 107 if hasattr(middleware, 'process_response'): 108 return middleware.process_response(request, response) 102 109 return response 103 110 return _wrapped_view 104 111 return _decorator -
docs/ref/template-response.txt
diff -r 128c90a7b280 docs/ref/template-response.txt
a b 119 119 rendered :class:`~django.template.response.SimpleTemplateResponse` 120 120 instance. 121 121 122 If the callback returns a value that is not `None`, this will be 123 used as the response instead of the original response object (and 124 will be passed to the next post rendering callback etc.) 125 122 126 .. method:: SimpleTemplateResponse.render(): 123 127 124 128 Sets :attr:`response.content` to the result obtained by -
tests/regressiontests/utils/decorators.py
diff -r 128c90a7b280 tests/regressiontests/utils/decorators.py
a b 1 1 from django.http import HttpResponse 2 2 from django.middleware.doc import XViewMiddleware 3 from django.template import Template, Context 4 from django.template.response import TemplateResponse 3 5 from django.test import TestCase, RequestFactory 4 6 from django.utils.decorators import decorator_from_middleware 5 7 … … 19 21 class_xview = xview_dec(ClassXView()) 20 22 21 23 24 class FullMiddleware(object): 25 def process_request(self, request): 26 request.process_request_reached = True 27 28 def process_view(sef, request, view_func, view_args, view_kwargs): 29 request.process_view_reached = True 30 31 def process_template_response(self, request, response): 32 request.process_template_response_reached = True 33 return response 34 35 def process_response(self, request, response): 36 # This should never receive unrendered content. 37 request.process_response_content = response.content 38 request.process_response_reached = True 39 return response 40 41 full_dec = decorator_from_middleware(FullMiddleware) 42 43 44 def template_response_view(request): 45 t = Template("Hello world") 46 return TemplateResponse(request, t, {}) 47 48 decorated_template_response_view = full_dec(template_response_view) 49 50 def normal_view(request): 51 t = Template("Hello world") 52 return HttpResponse(t.render(Context({}))) 53 54 decorated_view = full_dec(normal_view) 55 22 56 class DecoratorFromMiddlewareTests(TestCase): 23 57 """ 24 58 Tests for view decorators created using … … 37 71 Test a middleware that implements process_view, operating on a callable class. 38 72 """ 39 73 class_xview(self.rf.get('/')) 74 75 def test_full_middleware_dec_normal(self): 76 # Combined tests for all methods are easier 77 request = self.rf.get('/') 78 response = decorated_view(request) 79 self.assertTrue(request.process_request_reached) 80 self.assertTrue(request.process_view_reached) 81 self.assertTrue(request.process_response_reached) 82 self.assertEqual(request.process_response_content, "Hello world") 83 84 def test_full_middleware_dec_templateresponse(self): 85 # Combined tests for all methods are easier 86 request = self.rf.get('/') 87 response = decorated_template_response_view(request) 88 self.assertFalse(response._is_rendered) 89 response.render() 90 self.assertTrue(request.process_request_reached) 91 self.assertTrue(request.process_view_reached) 92 self.assertTrue(request.process_template_response_reached) 93 self.assertTrue(request.process_response_reached) 94 self.assertEqual(request.process_response_content, "Hello world")