From ab9fb3efd6bd5761f110e24fd8845d859b109e85 Mon Sep 17 00:00:00 2001 From: blingblin-g Date: Thu, 13 Mar 2025 01:08:42 +0900 Subject: [PATCH 1/4] Add interception_response hook --- debug_toolbar/panels/redirects.py | 35 ++++++++++++++++++------------- docs/changes.rst | 1 + tests/panels/test_redirects.py | 13 ++++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/debug_toolbar/panels/redirects.py b/debug_toolbar/panels/redirects.py index 27cce4c17..debf123d1 100644 --- a/debug_toolbar/panels/redirects.py +++ b/debug_toolbar/panels/redirects.py @@ -22,20 +22,8 @@ def _process_response(self, response): Common response processing logic. """ 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, - "toolbar": self.toolbar, - } - # Using SimpleTemplateResponse avoids running global context processors. - response = SimpleTemplateResponse( - "debug_toolbar/redirect.html", context - ) - response.cookies = cookies + if redirect_to := response.get("Location"): + response = self.get_interception_response(response, redirect_to) response.render() return response @@ -53,3 +41,22 @@ def process_request(self, request): if iscoroutine(response): return self.aprocess_request(request, response) return self._process_response(response) + + def get_interception_response(self, response, redirect_to): + """ + Hook method to allow subclasses to customize the interception response. + """ + status_line = f"{response.status_code} {response.reason_phrase}" + cookies = response.cookies + original_response = response + context = { + "redirect_to": redirect_to, + "status_line": status_line, + "toolbar": self.toolbar, + "original_response": original_response, + } + # Default implementation: return a SimpleTemplateResponse + response = SimpleTemplateResponse("debug_toolbar/redirect.html", context) + response.cookies = cookies + response.original_response = original_response + return response diff --git a/docs/changes.rst b/docs/changes.rst index f11d4889e..753c8e36f 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -16,6 +16,7 @@ Pending of each target. * Avoided reinitializing the staticfiles storage during instrumentation. * Fix for exception-unhandled "forked" Promise chain in rebound window.fetch +* Add hook to RedirectsPanel for subclass customization 5.0.1 (2025-01-13) ------------------ diff --git a/tests/panels/test_redirects.py b/tests/panels/test_redirects.py index 2abed9fd0..7d6d5ac06 100644 --- a/tests/panels/test_redirects.py +++ b/tests/panels/test_redirects.py @@ -85,3 +85,16 @@ async def get_response(request): response = await self.panel.process_request(self.request) self.assertIsInstance(response, HttpResponse) self.assertTrue(response is await_response) + + def test_original_response_preserved(self): + redirect = HttpResponse(status=302) + redirect["Location"] = "http://somewhere/else/" + self._get_response = lambda request: redirect + response = self.panel.process_request(self.request) + self.assertFalse(response is redirect) + self.assertTrue(hasattr(response, "original_response")) + self.assertTrue(response.original_response is redirect) + self.assertIsNone(response.get("Location")) + self.assertEqual( + response.original_response.get("Location"), "http://somewhere/else/" + ) From b74f704360c332ff37a387520756d433d0a7e02b Mon Sep 17 00:00:00 2001 From: blingblin-g Date: Thu, 20 Mar 2025 23:18:52 +0900 Subject: [PATCH 2/4] Fix comments for SimpleTemplateResponse --- debug_toolbar/panels/redirects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/panels/redirects.py b/debug_toolbar/panels/redirects.py index debf123d1..8055c67ad 100644 --- a/debug_toolbar/panels/redirects.py +++ b/debug_toolbar/panels/redirects.py @@ -55,7 +55,7 @@ def get_interception_response(self, response, redirect_to): "toolbar": self.toolbar, "original_response": original_response, } - # Default implementation: return a SimpleTemplateResponse + # Using SimpleTemplateResponse avoids running global context processors. response = SimpleTemplateResponse("debug_toolbar/redirect.html", context) response.cookies = cookies response.original_response = original_response From 66ff940bd3c23c7b200c66d5ed28d1b87135a18d Mon Sep 17 00:00:00 2001 From: blingblin-g Date: Fri, 21 Mar 2025 00:08:45 +0900 Subject: [PATCH 3/4] Docs for Custom RedirectPanel --- docs/panels.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/panels.rst b/docs/panels.rst index be481fb6e..a116bff1e 100644 --- a/docs/panels.rst +++ b/docs/panels.rst @@ -122,6 +122,11 @@ Since this behavior is annoying when you aren't debugging a redirect, this panel is included but inactive by default. You can activate it by default with the ``DISABLE_PANELS`` configuration option. +To further customize the behavior, you can subclass the ``RedirectsPanel`` +and override the ``get_interception_response`` method to manipulate the +response directly. To use a custom ``RedirectsPanel``, you need to replace +the original one in ``DEBUG_TOOLBAR_PANELS`` in your ``settings.py``. + .. _profiling-panel: Profiling From db924732b8ae6fed15d431815ccdb2595d608765 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Thu, 20 Mar 2025 22:27:09 +0100 Subject: [PATCH 4/4] Moved the changelog entry to the correct place. --- docs/changes.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 6efa38f70..5be81b2cb 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -4,6 +4,8 @@ Change log Pending ------- +* Added hook to RedirectsPanel for subclass customization. + 5.1.0 (2025-03-20) ------------------ * Added Django 5.2 to the tox matrix. @@ -22,7 +24,6 @@ Pending * Fixed the pygments code highlighting when using dark mode. * Fix for exception-unhandled "forked" Promise chain in rebound window.fetch * Create a CSP nonce property on the toolbar ``Toolbar().csp_nonce``. -* Add hook to RedirectsPanel for subclass customization 5.0.1 (2025-01-13) ------------------