aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAymeric Augustin2013-11-15 20:45:32 +0100
committerAymeric Augustin2013-11-15 20:45:32 +0100
commite89992a951b05b20dcd5c59927041d41b23110c9 (patch)
treefd51cafbcc151bb44c7d363810031bae70067468
parent57e3169fe3ca4b6bd23faee12911b3177eba8aa4 (diff)
downloaddjango-debug-toolbar-e89992a951b05b20dcd5c59927041d41b23110c9.tar.bz2
Implement redirects interception as a panel.
Fix #122.
-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
-rw-r--r--docs/configuration.rst18
-rw-r--r--docs/panels.rst15
-rw-r--r--example/settings.py1
7 files changed, 68 insertions, 26 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',
)
diff --git a/docs/configuration.rst b/docs/configuration.rst
index df203cb..05500d9 100644
--- a/docs/configuration.rst
+++ b/docs/configuration.rst
@@ -22,6 +22,7 @@ setting. The default value is::
'debug_toolbar.panels.cache.CacheDebugPanel',
'debug_toolbar.panels.signals.SignalDebugPanel',
'debug_toolbar.panels.logger.LoggingPanel',
+ 'debug_toolbar.panels.redirects.InterceptRedirectsPanel',
)
This setting allows you to:
@@ -54,15 +55,6 @@ Toolbar options
right thing depending on whether the WSGI container runs multiple processes.
This setting allows you to force a different behavior if needed.
-* ``INTERCEPT_REDIRECTS``
-
- Default: ``False``
-
- If set to ``True``, the debug toolbar will show an intermediate page upon
- redirect so you can view any debug information prior to redirecting. This
- page will provide a link to the redirect destination you can follow when
- ready. If set to ``False``, redirects will proceed as normal.
-
* ``RESULTS_CACHE_SIZE``
Default: ``10``
@@ -139,6 +131,14 @@ Panel options
If set to ``True`` then code in Django itself won't be shown in
stacktraces.
+* ``INTERCEPT_REDIRECTS``
+
+ Default: ``False``
+
+ Panel: redirects
+
+ If set to ``True``, the redirect panel will be active by default.
+
* ``SHOW_TEMPLATE_CONTEXT``
Default: ``True``
diff --git a/docs/panels.rst b/docs/panels.rst
index b5b7b2e..5ea4e44 100644
--- a/docs/panels.rst
+++ b/docs/panels.rst
@@ -84,6 +84,21 @@ Path: ``debug_toolbar.panels.logger.LoggingPanel``
Logging output via Python's built-in :mod:`logging`, or via the `logbook <http://logbook.pocoo.org>`_ module.
+Redirects
+~~~~~~~~~
+
+Path: ``debug_toolbar.panels.redirects.InterceptRedirectsPanel``
+
+When this panel is enabled, the debug toolbar will show an intermediate page
+upon redirect so you can view any debug information prior to redirecting. This
+page will provide a link to the redirect destination you can follow when
+ready.
+
+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 ``INTERCEPT_REDIRECTS`` configuration option.
+
+
Non-default built-in panels
---------------------------
diff --git a/example/settings.py b/example/settings.py
index 1eba309..0934c0e 100644
--- a/example/settings.py
+++ b/example/settings.py
@@ -64,4 +64,5 @@ DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.cache.CacheDebugPanel',
'debug_toolbar.panels.signals.SignalDebugPanel',
'debug_toolbar.panels.logger.LoggingPanel',
+ 'debug_toolbar.panels.redirects.InterceptRedirectsPanel',
)