aboutsummaryrefslogtreecommitdiffstats
path: root/debug_toolbar/panels
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/panels
parent57e3169fe3ca4b6bd23faee12911b3177eba8aa4 (diff)
downloaddjango-debug-toolbar-e89992a951b05b20dcd5c59927041d41b23110c9.tar.bz2
Implement redirects interception as a panel.
Fix #122.
Diffstat (limited to 'debug_toolbar/panels')
-rw-r--r--debug_toolbar/panels/redirects.py39
1 files changed, 39 insertions, 0 deletions
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')