File: request.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 (56 lines) | stat: -rw-r--r-- 1,948 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import absolute_import, unicode_literals

from django.core.urlresolvers import resolve
from django.http import Http404
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.panels import Panel
from debug_toolbar.utils import get_name_from_obj


class RequestPanel(Panel):
    """
    A panel to display request variables (POST/GET, session, cookies).
    """
    template = 'debug_toolbar/panels/request.html'

    title = _("Request")

    @property
    def nav_subtitle(self):
        """
        Show abbreviated name of view function as subtitle
        """
        view_func = self.get_stats().get('view_func', '')
        return view_func.rsplit('.', 1)[-1]

    def process_response(self, request, response):
        self.record_stats({
            'get': [(k, request.GET.getlist(k)) for k in sorted(request.GET)],
            'post': [(k, request.POST.getlist(k)) for k in sorted(request.POST)],
            'cookies': [(k, request.COOKIES.get(k)) for k in sorted(request.COOKIES)],
        })
        view_info = {
            'view_func': _("<no view>"),
            'view_args': 'None',
            'view_kwargs': 'None',
            'view_urlname': 'None',
        }
        try:
            match = resolve(request.path)
            func, args, kwargs = match
            view_info['view_func'] = get_name_from_obj(func)
            view_info['view_args'] = args
            view_info['view_kwargs'] = kwargs
            view_info['view_urlname'] = getattr(match, 'url_name',
                                                _("<unavailable>"))
        except Http404:
            pass
        self.record_stats(view_info)

        if hasattr(request, 'session'):
            self.record_stats({
                'session': [(k, request.session.get(k))
                            for k in sorted(request.session.keys(), key=force_text)]
            })