Skip to content

Commit

Permalink
async redirect panel
Browse files Browse the repository at this point in the history
  • Loading branch information
salty-ivy committed Jul 2, 2024
1 parent b03cc84 commit 1afa22f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 21 additions & 1 deletion debug_toolbar/panels/redirects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from inspect import iscoroutine

from django.template.response import SimpleTemplateResponse
from django.utils.translation import gettext_lazy as _

Expand All @@ -9,13 +11,31 @@ class RedirectsPanel(Panel):
Panel that intercepts redirects and displays a page with debug info.
"""

is_async = False
is_async = True
has_content = False

nav_title = _("Intercept redirects")

async def aprocess_request(self, request):
response = await super().process_request(request)
if 300 <= response.status_code < 400:
redirect_to = response.get("Location")
if redirect_to:
status_line = f"{response.status_code} {response.reason_phrase}"
cookies = response.cookies
context = {"redirect_to": redirect_to, "status_line": status_line}
# Using SimpleTemplateResponse avoids running global context processors.
response = SimpleTemplateResponse(
"debug_toolbar/redirect.html", context
)
response.cookies = cookies
response.render()
return response

def process_request(self, request):
response = super().process_request(request)
if iscoroutine(response):
return self.aprocess_request(request)
if 300 <= response.status_code < 400:
redirect_to = response.get("Location")
if redirect_to:
Expand Down
4 changes: 3 additions & 1 deletion debug_toolbar/panels/staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def _setup(self):

configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)

print("storage class: ", configured_storage_cls)

class DebugStaticFilesStorage(configured_storage_cls):
def url(self, path):
with contextlib.suppress(LookupError):
Expand All @@ -73,7 +75,7 @@ class StaticFilesPanel(panels.Panel):
A panel to display the found staticfiles.
"""

is_async = False
is_async = True
name = "Static files"
template = "debug_toolbar/panels/staticfiles.html"

Expand Down

0 comments on commit 1afa22f

Please sign in to comment.