File: redirects.py

package info (click to toggle)
python-django-debug-toolbar 1%3A1.2.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 1,628 kB
  • sloc: python: 2,789; makefile: 180; sh: 1
file content (33 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import absolute_import, unicode_literals

from django.core.handlers.wsgi import STATUS_CODE_TEXT
from django.shortcuts import render_to_response
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.panels import Panel


class RedirectsPanel(Panel):
    """
    Panel that intercepts redirects and displays a page with debug info.
    """

    has_content = False

    nav_title = _("Intercept redirects")

    def process_response(self, request, response):
        if 300 <= int(response.status_code) < 400:
            redirect_to = response.get('Location', None)
            if redirect_to:
                try:        # Django >= 1.6
                    reason_phrase = response.reason_phrase
                except AttributeError:
                    reason_phrase = STATUS_CODE_TEXT.get(response.status_code,
                                                         'UNKNOWN STATUS CODE')
                status_line = '%s %s' % (response.status_code, reason_phrase)
                cookies = response.cookies
                context = {'redirect_to': redirect_to, 'status_line': status_line}
                response = render_to_response('debug_toolbar/redirect.html', context)
                response.cookies = cookies
        return response