From 4df6736225961d6021768d356adda98dc4eeb258 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 14 Mar 2013 16:21:30 -0400 Subject: [PATCH] Call the Django response middleware from Tornado views. But discard any changes the Django response middleware may have made to anything other than the content. This allows us to, for example, output our nice database query logging for get_updates requests. (imported from commit e1d2fd38ceb4d73ff50bdfaad7c72ddb24d0fe16) --- zephyr/management/commands/runtornado.py | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/zephyr/management/commands/runtornado.py b/zephyr/management/commands/runtornado.py index d7d3f565f3..0eb1945ea0 100644 --- a/zephyr/management/commands/runtornado.py +++ b/zephyr/management/commands/runtornado.py @@ -9,6 +9,7 @@ import logging import time from tornado import ioloop from zephyr.lib.debug import interactive_debug_listen +from zephyr.lib.response import json_response # A hack to keep track of how much time we spend working, versus sleeping in # the event loop. @@ -229,6 +230,10 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler): urlresolvers.set_urlconf(urlconf) resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) + ### ADDED BY HUMBUG + request._resolver = resolver + ### END ADDED BY HUMBUG + callback, callback_args, callback_kwargs = resolver.resolve( request.path_info) @@ -314,6 +319,15 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler): # Reset urlconf on the way out for isolation urlresolvers.set_urlconf(None) + ### HUMBUG CHANGE: The remainder of this function was moved + ### into its own function, just below, so we can call it from + ### finish(). + response = self.apply_response_middleware(request, response, resolver) + + return response + + ### Copied from get_response (above in this file) + def apply_response_middleware(self, request, response, resolver): try: # Apply response middleware, regardless of the response for middleware_method in self._response_middleware: @@ -343,4 +357,13 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler): if response['result'] == 'error': self.set_status(400) - return superself.finish(response) + # Call the Django response middleware on our object so that + # e.g. our own logging code can run; but don't actually use + # the headers from that since sending those to Tornado seems + # tricky; instead just send the (already json-rendered) + # content on to Tornado + django_response = json_response(res_type=response['result'], + data=response, status=self.get_status()) + django_response = self.apply_response_middleware(request, django_response, + request._resolver) + return superself.finish(django_response.content)