Skip to content

Commit f597083

Browse files
committed
Add function docstrings
1 parent b03cc84 commit f597083

File tree

3 files changed

+96
-38
lines changed

3 files changed

+96
-38
lines changed

debug_toolbar/middleware.py

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,38 +79,21 @@ def __init__(self, get_response):
7979
# __call__ to avoid swapping out dunder methods.
8080
markcoroutinefunction(self)
8181

82-
def __call__(self, request):
83-
if self.async_mode:
84-
return self.__acall__(request)
85-
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-
91-
toolbar = DebugToolbar(request, self.get_response)
92-
93-
# Activate instrumentation ie. monkey-patch.
94-
for panel in toolbar.enabled_panels:
95-
panel.enable_instrumentation()
96-
try:
97-
# Run panels like Django middleware.
98-
response = toolbar.process_request(request)
99-
finally:
100-
clear_stack_trace_caches()
101-
# Deactivate instrumentation ie. monkey-unpatch. This must run
102-
# regardless of the response. Keep 'return' clauses below.
103-
for panel in reversed(toolbar.enabled_panels):
104-
panel.disable_instrumentation()
82+
async def aget_response(self, request):
83+
return await self.get_response(request)
10584

106-
return self._postprocess(request, response, toolbar)
85+
async def aprocess_request(self, toolbar, request):
86+
return await toolbar.process_request(request)
10787

108-
async def __acall__(self, request):
88+
def __call__(self, request):
10989
# Decide whether the toolbar is active for this request.
11090
show_toolbar = get_show_toolbar()
11191
if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request):
112-
response = await self.get_response(request)
113-
return response
92+
return (
93+
self.aget_response(request)
94+
if self.async_mode
95+
else self.get_response(request)
96+
)
11497

11598
toolbar = DebugToolbar(request, self.get_response)
11699

@@ -119,20 +102,18 @@ async def __acall__(self, request):
119102
panel.enable_instrumentation()
120103
try:
121104
# Run panels like Django middleware.
122-
response = await toolbar.process_request(request)
105+
response = (
106+
self.aprocess_request(toolbar, request)
107+
if self.async_mode
108+
else toolbar.process_request(request)
109+
)
123110
finally:
124111
clear_stack_trace_caches()
125112
# Deactivate instrumentation ie. monkey-unpatch. This must run
126113
# regardless of the response. Keep 'return' clauses below.
127114
for panel in reversed(toolbar.enabled_panels):
128115
panel.disable_instrumentation()
129116

130-
return self._postprocess(request, response, toolbar)
131-
132-
def _postprocess(self, request, response, toolbar):
133-
"""
134-
Post-process the response.
135-
"""
136117
# Generate the stats for all requests when the toolbar is being shown,
137118
# but not necessarily inserted.
138119
for panel in reversed(toolbar.enabled_panels):
@@ -168,6 +149,69 @@ def _postprocess(self, request, response, toolbar):
168149
response["Content-Length"] = len(response.content)
169150
return response
170151

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+
171215
@staticmethod
172216
def get_headers(request, panels):
173217
headers = {}

tests/panels/test_async_panel_compatibility.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88

99
def set_custom_toolbar_config():
10+
"""
11+
Set custom toolbar configuration.
12+
13+
Returns a new configuration with DISABLE_PANELS set to an empty dictionary.
14+
This allows all panels to be enabled by default.
15+
"""
1016
new_settings = dt_settings.get_config().copy()
1117
new_settings["DISABLE_PANELS"] = {}
1218
return new_settings

tests/test_middleware_compatibility.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@ def setUp(self):
1212
self.async_factory = AsyncRequestFactory()
1313

1414
def test_sync_mode(self):
15+
"""
16+
test middlware switches to sync (__call__) based on get_response type
17+
"""
18+
1519
request = self.factory.get("/")
16-
middleware = DebugToolbarMiddleware(lambda x: HttpResponse(""))
20+
middleware = DebugToolbarMiddleware(
21+
lambda x: HttpResponse("<html><body>Django debug toolbar</body></html>")
22+
)
1723

18-
# test middlware switches to sync (__call__) based on get_response type
1924
self.assertFalse(asyncio.iscoroutinefunction(middleware))
2025

2126
response = middleware(request)
2227
self.assertEqual(response.status_code, 200)
2328

2429
async def test_async_mode(self):
30+
"""
31+
test middlware switches to async (__acall__) based on get_response type
32+
and returns a coroutine
33+
"""
34+
2535
async def get_response(request):
2636
return HttpResponse("<html><body>Django debug toolbar</body></html>")
2737

2838
middleware = DebugToolbarMiddleware(get_response)
2939
request = self.async_factory.get("/")
3040

31-
# test middlware switches to async (__acall__) based on get_response type
32-
# and returns a coroutine
3341
self.assertTrue(asyncio.iscoroutinefunction(middleware))
3442

3543
response = await middleware(request)

0 commit comments

Comments
 (0)