@@ -79,21 +79,36 @@ def __init__(self, get_response):
7979 # __call__ to avoid swapping out dunder methods.
8080 markcoroutinefunction (self )
8181
82- async def aget_response (self , request ):
83- return await self .get_response (request )
82+ def __call__ (self , request ):
83+ # Decide whether the toolbar is active for this request.
84+ if self .async_mode :
85+ return self .__acall__ (request )
86+ # Decide whether the toolbar is active for this request.
87+ show_toolbar = get_show_toolbar ()
88+ if not show_toolbar (request ) or DebugToolbar .is_toolbar_request (request ):
89+ return self .get_response (request )
90+ toolbar = DebugToolbar (request , self .get_response )
91+ # Activate instrumentation ie. monkey-patch.
92+ for panel in toolbar .enabled_panels :
93+ panel .enable_instrumentation ()
94+ try :
95+ # Run panels like Django middleware.
96+ response = toolbar .process_request (request )
97+ finally :
98+ clear_stack_trace_caches ()
99+ # Deactivate instrumentation ie. monkey-unpatch. This must run
100+ # regardless of the response. Keep 'return' clauses below.
101+ for panel in reversed (toolbar .enabled_panels ):
102+ panel .disable_instrumentation ()
84103
85- async def aprocess_request (self , toolbar , request ):
86- return await toolbar .process_request (request )
104+ return self ._postprocess (request , response , toolbar )
87105
88- def __call__ (self , request ):
106+ async def __acall__ (self , request ):
89107 # Decide whether the toolbar is active for this request.
90108 show_toolbar = get_show_toolbar ()
91109 if not show_toolbar (request ) or DebugToolbar .is_toolbar_request (request ):
92- return (
93- self .aget_response (request )
94- if self .async_mode
95- else self .get_response (request )
96- )
110+ response = await self .get_response (request )
111+ return response
97112
98113 toolbar = DebugToolbar (request , self .get_response )
99114
@@ -102,18 +117,20 @@ def __call__(self, request):
102117 panel .enable_instrumentation ()
103118 try :
104119 # Run panels like Django middleware.
105- response = (
106- self .aprocess_request (toolbar , request )
107- if self .async_mode
108- else toolbar .process_request (request )
109- )
120+ response = await toolbar .process_request (request )
110121 finally :
111122 clear_stack_trace_caches ()
112123 # Deactivate instrumentation ie. monkey-unpatch. This must run
113124 # regardless of the response. Keep 'return' clauses below.
114125 for panel in reversed (toolbar .enabled_panels ):
115126 panel .disable_instrumentation ()
116127
128+ return self ._postprocess (request , response , toolbar )
129+
130+ def _postprocess (self , request , response , toolbar ):
131+ """
132+ Post-process the response.
133+ """
117134 # Generate the stats for all requests when the toolbar is being shown,
118135 # but not necessarily inserted.
119136 for panel in reversed (toolbar .enabled_panels ):
@@ -149,69 +166,6 @@ def __call__(self, request):
149166 response ["Content-Length" ] = len (response .content )
150167 return response
151168
152- # async def __acall__(self, request):
153- # # Decide whether the toolbar is active for this request.
154- # show_toolbar = get_show_toolbar()
155- # if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request):
156- # response = await self.get_response(request)
157- # return response
158-
159- # toolbar = DebugToolbar(request, self.get_response)
160-
161- # # Activate instrumentation ie. monkey-patch.
162- # for panel in toolbar.enabled_panels:
163- # panel.enable_instrumentation()
164- # try:
165- # # Run panels like Django middleware.
166- # response = await toolbar.process_request(request)
167- # finally:
168- # clear_stack_trace_caches()
169- # # Deactivate instrumentation ie. monkey-unpatch. This must run
170- # # regardless of the response. Keep 'return' clauses below.
171- # for panel in reversed(toolbar.enabled_panels):
172- # panel.disable_instrumentation()
173-
174- # return self._postprocess(request, response, toolbar)
175-
176- # def _postprocess(self, request, response, toolbar):
177- # """
178- # Post-process the response.
179- # """
180- # # Generate the stats for all requests when the toolbar is being shown,
181- # # but not necessarily inserted.
182- # for panel in reversed(toolbar.enabled_panels):
183- # panel.generate_stats(request, response)
184- # panel.generate_server_timing(request, response)
185-
186- # # Always render the toolbar for the history panel, even if it is not
187- # # included in the response.
188- # rendered = toolbar.render_toolbar()
189-
190- # for header, value in self.get_headers(request, toolbar.enabled_panels).items():
191- # response.headers[header] = value
192-
193- # # Check for responses where the toolbar can't be inserted.
194- # content_encoding = response.get("Content-Encoding", "")
195- # content_type = response.get("Content-Type", "").split(";")[0]
196- # if (
197- # getattr(response, "streaming", False)
198- # or content_encoding != ""
199- # or content_type not in _HTML_TYPES
200- # ):
201- # return response
202-
203- # # Insert the toolbar in the response.
204- # content = response.content.decode(response.charset)
205- # insert_before = dt_settings.get_config()["INSERT_BEFORE"]
206- # pattern = re.escape(insert_before)
207- # bits = re.split(pattern, content, flags=re.IGNORECASE)
208- # if len(bits) > 1:
209- # bits[-2] += rendered
210- # response.content = insert_before.join(bits)
211- # if "Content-Length" in response:
212- # response["Content-Length"] = len(response.content)
213- # return response
214-
215169 @staticmethod
216170 def get_headers (request , panels ):
217171 headers = {}
0 commit comments