From e89992a951b05b20dcd5c59927041d41b23110c9 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Fri, 15 Nov 2013 20:45:32 +0100 Subject: Implement redirects interception as a panel. Fix #122. --- debug_toolbar/middleware.py | 14 -------- debug_toolbar/panels/redirects.py | 39 ++++++++++++++++++++++ .../templates/debug_toolbar/redirect.html | 6 ++-- debug_toolbar/utils/settings.py | 1 + 4 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 debug_toolbar/panels/redirects.py (limited to 'debug_toolbar') 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 @@
-{% trans 'Location' %}: {{ redirect_to }}
+
- {% 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 DEBUG_TOOLBAR_CONFIG dictionary's key INTERCEPT_REDIRECTS to False." %}
+ {% 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." %}