Skip to content
Prev Previous commit
Next Next commit
make redirect panel async capable by using aprocess_request patterm
  • Loading branch information
salty-ivy authored and tim-schilling committed Aug 6, 2024
commit 8d533112fa704cd8a0b8ceae87b25317931b9c3c
20 changes: 17 additions & 3 deletions 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,15 @@ 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")

def process_request(self, request):
response = super().process_request(request)
def _process_response(self, response):
"""
Common response processing logic.
"""
if 300 <= response.status_code < 400:
redirect_to = response.get("Location")
if redirect_to:
Expand All @@ -33,3 +37,13 @@ def process_request(self, request):
response.cookies = cookies
response.render()
return response

async def aprocess_request(self, request):
response = await super().process_request(request)
return self._process_response(response)

def process_request(self, request):
response = super().process_request(request)
if iscoroutine(response):
return self.aprocess_request(request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flow will call super().process_request twice. We probably want to avoid that. I have some ideas, but none that I know are great. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since aprocess_request is exclusive to RedirectsPanel

  1. we can pass response from process_request in aprocess_request as an argument.
  2. or else we change the check from coroutine to checking request being an instance ASGIRequest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference is option 1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 2. won't work either : )

return self._process_response(response)