aboutsummaryrefslogtreecommitdiffstats
path: root/debug_toolbar
diff options
context:
space:
mode:
authorAymeric Augustin2013-11-15 20:45:32 +0100
committerAymeric Augustin2013-11-15 20:45:32 +0100
commite89992a951b05b20dcd5c59927041d41b23110c9 (patch)
treefd51cafbcc151bb44c7d363810031bae70067468 /debug_toolbar
parent57e3169fe3ca4b6bd23faee12911b3177eba8aa4 (diff)
downloaddjango-debug-toolbar-e89992a951b05b20dcd5c59927041d41b23110c9.tar.bz2
Implement redirects interception as a panel.
Fix #122.
Diffstat (limited to 'debug_toolbar')
-rw-r--r--debug_toolbar/middleware.py14
-rw-r--r--debug_toolbar/panels/redirects.py39
-rw-r--r--debug_toolbar/templates/debug_toolbar/redirect.html6
-rw-r--r--debug_toolbar/utils/settings.py1
4 files changed, 43 insertions, 17 deletions
diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py
index edcf17f..c1d052c 100644
--- a/debug_toolbar/middleware.py
+++ b/debug_toolbar/middleware.py
@@ -7,8 +7,6 @@ from __future__ import unicode_literals
import threading
from django.conf import settings
-from django.http import HttpResponseRedirect
-from django.shortcuts import render
from django.utils.encoding import force_text
from debug_toolbar.toolbar import DebugToolbar
@@ -92,18 +90,6 @@ class DebugToolbarMiddleware(object):
toolbar = self.__class__.debug_toolbars.pop(threading.current_thread().ident, None)
if not toolbar or getattr(response, 'streaming', False):
return response
- if isinstance(response, HttpResponseRedirect):
- if not toolbar.config['INTERCEPT_REDIRECTS']:
- return response
- redirect_to = response.get('Location', None)
- if redirect_to:
- cookies = response.cookies
- response = render(
- request,
- 'debug_toolbar/redirect.html',
- {'redirect_to': redirect_to}
- )
- response.cookies = cookies
for panel in reversed(toolbar.enabled_panels):
new_response = panel.process_response(request, response)
if new_response:
diff --git a/debug_toolbar/panels/redirects.py b/debug_toolbar/panels/redirects.py
new file mode 100644
index 0000000..244f7ed
--- /dev/null
+++ b/debug_toolbar/panels/redirects.py
@@ -0,0 +1,39 @@
+from __future__ import unicode_literals
+
+from django.core.handlers.wsgi import STATUS_CODE_TEXT
+from django.http import HttpResponseRedirect
+from django.shortcuts import render
+from django.utils.translation import ugettext as _
+
+from debug_toolbar.panels import DebugPanel
+
+
+class InterceptRedirectsPanel(DebugPanel):
+ """
+ Panel that intercepts redirects and displays a page with debug info.
+ """
+ name = 'Redirects'
+
+ has_content = False
+
+ def enabled(self):
+ default = 'on' if self.toolbar.config['INTERCEPT_REDIRECTS'] else 'off'
+ return self.toolbar.request.COOKIES.get(self.dom_id(), default) == 'on'
+
+ def process_response(self, request, response):
+ if isinstance(response, HttpResponseRedirect):
+ redirect_to = response.get('Location', None)
+ if redirect_to:
+ try:
+ status_text = STATUS_CODE_TEXT[response.status_code]
+ except KeyError:
+ status_text = 'UNKNOWN STATUS CODE'
+ status_line = '%s %s' % (response.status_code, status_text.title())
+ cookies = response.cookies
+ context = {'redirect_to': redirect_to, 'status_line': status_line}
+ response = render(request, 'debug_toolbar/redirect.html', context)
+ response.cookies = cookies
+ return response
+
+ def nav_title(self):
+ return _('Intercept redirects')
diff --git a/debug_toolbar/templates/debug_toolbar/redirect.html b/debug_toolbar/templates/debug_toolbar/redirect.html
index ca33446..f2dde2d 100644
--- a/debug_toolbar/templates/debug_toolbar/redirect.html
+++ b/debug_toolbar/templates/debug_toolbar/redirect.html
@@ -4,10 +4,10 @@
<head>
</head>
<body>
-<h1>HttpResponseRedirect</h1>
-<p>{% trans 'Location' %}: <a id="redirect_to" href="{{ redirect_to }}">{{ redirect_to }}</a></p>
+<h1>{{ status_line }}</h1>
+<h2>{% trans "Location" %}: <a id="redirect_to" href="{{ redirect_to }}">{{ redirect_to }}</a></h2>
<p class="notice">
- {% trans "The Django Debug Toolbar has intercepted a redirect to the above URL for debug viewing purposes. You can click the above link to continue with the redirect as normal. If you'd like to disable this feature, set the <code>DEBUG_TOOLBAR_CONFIG</code> dictionary's key <code>INTERCEPT_REDIRECTS</code> to <code>False</code>." %}
+ {% trans "The Django Debug Toolbar has intercepted a redirect to the above URL for debug viewing purposes. You can click the above link to continue with the redirect as normal." %}
</p>
<script type="text/javascript">
document.getElementById('redirect_to').focus();
diff --git a/debug_toolbar/utils/settings.py b/debug_toolbar/utils/settings.py
index 323e7cf..237f2c8 100644
--- a/debug_toolbar/utils/settings.py
+++ b/debug_toolbar/utils/settings.py
@@ -48,6 +48,7 @@ PANELS_DEFAULTS = (
'debug_toolbar.panels.cache.CacheDebugPanel',
'debug_toolbar.panels.signals.SignalDebugPanel',
'debug_toolbar.panels.logger.LoggingPanel',
+ 'debug_toolbar.panels.redirects.InterceptRedirectsPanel',
)