File: panel.py

package info (click to toggle)
python-django-debug-toolbar 1%3A1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: python: 3,346; makefile: 191; sh: 1
file content (222 lines) | stat: -rw-r--r-- 9,751 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from __future__ import absolute_import, unicode_literals

from collections import OrderedDict
from contextlib import contextmanager
from os.path import normpath
from pprint import pformat

from django import http
from django.conf.urls import url
from django.core import signing
from django.db.models.query import QuerySet, RawQuerySet
from django.template import RequestContext, Template
from django.test.signals import template_rendered
from django.test.utils import instrumented_test_render
from django.utils import six
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.panels import Panel
from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, recording
from debug_toolbar.panels.templates import views

# Monkey-patch to enable the template_rendered signal. The receiver returns
# immediately when the panel is disabled to keep the overhead small.

# Code taken and adapted from Simon Willison and Django Snippets:
# https://www.djangosnippets.org/snippets/766/

if Template._render != instrumented_test_render:
    Template.original_render = Template._render
    Template._render = instrumented_test_render


# Monkey-patch to store items added by template context processors. The
# overhead is sufficiently small to justify enabling it unconditionally.

@contextmanager
def _request_context_bind_template(self, template):
    if self.template is not None:
        raise RuntimeError("Context is already bound to a template")

    self.template = template
    # Set context processors according to the template engine's settings.
    processors = (template.engine.template_context_processors +
                  self._processors)
    self.context_processors = OrderedDict()
    updates = {}
    for processor in processors:
        name = '%s.%s' % (processor.__module__, processor.__name__)
        context = processor(self.request)
        self.context_processors[name] = context
        updates.update(context)
    self.dicts[self._processors_index] = updates

    try:
        yield
    finally:
        self.template = None
        # Unset context processors.
        self.dicts[self._processors_index] = {}


RequestContext.bind_template = _request_context_bind_template


class TemplatesPanel(Panel):
    """
    A panel that lists all templates used during processing of a response.
    """
    def __init__(self, *args, **kwargs):
        super(TemplatesPanel, self).__init__(*args, **kwargs)
        self.templates = []
        # Refs GitHub issue #910
        # Hold a series of seen dictionaries within Contexts. A dictionary is
        # considered seen if it is `in` this list, requiring that the __eq__
        # for the dictionary matches. If *anything* in the dictionary is
        # different it is counted as a new layer.
        self.seen_layers = []
        # Holds all dictionaries which have been prettified for output.
        # This should align with the seen_layers such that an index here is
        # the same as the index there.
        self.pformat_layers = []

    def _store_template_info(self, sender, **kwargs):
        template, context = kwargs['template'], kwargs['context']

        # Skip templates that we are generating through the debug toolbar.
        if (isinstance(template.name, six.string_types) and (
            template.name.startswith('debug_toolbar/') or
            template.name.startswith(
                tuple(self.toolbar.config['SKIP_TEMPLATE_PREFIXES'])))):
            return

        context_list = []
        for context_layer in context.dicts:
            if hasattr(context_layer, 'items') and context_layer:
                # Refs GitHub issue #910
                # If we can find this layer in our pseudo-cache then find the
                # matching prettified version in the associated list.
                key_values = sorted(context_layer.items())
                if key_values in self.seen_layers:
                    index = self.seen_layers.index(key_values)
                    pformatted = self.pformat_layers[index]
                    context_list.append(pformatted)
                else:
                    temp_layer = {}
                    for key, value in context_layer.items():
                        # Replace any request elements - they have a large
                        # unicode representation and the request data is
                        # already made available from the Request panel.
                        if isinstance(value, http.HttpRequest):
                            temp_layer[key] = '<<request>>'
                        # Replace the debugging sql_queries element. The SQL
                        # data is already made available from the SQL panel.
                        elif key == 'sql_queries' and isinstance(value, list):
                            temp_layer[key] = '<<sql_queries>>'
                        # Replace LANGUAGES, which is available in i18n context processor
                        elif key == 'LANGUAGES' and isinstance(value, tuple):
                            temp_layer[key] = '<<languages>>'
                        # QuerySet would trigger the database: user can run the query from SQL Panel
                        elif isinstance(value, (QuerySet, RawQuerySet)):
                            model_name = "%s.%s" % (
                                value.model._meta.app_label, value.model.__name__)
                            temp_layer[key] = '<<%s of %s>>' % (
                                value.__class__.__name__.lower(), model_name)
                        else:
                            try:
                                recording(False)
                                force_text(value)  # this MAY trigger a db query
                            except SQLQueryTriggered:
                                temp_layer[key] = '<<triggers database query>>'
                            except UnicodeEncodeError:
                                temp_layer[key] = '<<unicode encode error>>'
                            except Exception:
                                temp_layer[key] = '<<unhandled exception>>'
                            else:
                                temp_layer[key] = value
                            finally:
                                recording(True)
                    # Refs GitHub issue #910
                    # If we've not seen the layer before then we will add it
                    # so that if we see it again we can skip formatting it.
                    self.seen_layers.append(key_values)
                    # Note: this *ought* to be len(...) - 1 but let's be safe.
                    index = self.seen_layers.index(key_values)
                    try:
                        pformatted = force_text(pformat(temp_layer))
                    except UnicodeEncodeError:
                        pass
                    else:
                        # Note: this *ought* to be len(...) - 1 but let's be safe.
                        self.pformat_layers.insert(index, pformatted)
                        context_list.append(pformatted)

        kwargs['context'] = context_list
        kwargs['context_processors'] = getattr(context, 'context_processors', None)
        self.templates.append(kwargs)

    # Implement the Panel API

    nav_title = _("Templates")

    @property
    def title(self):
        num_templates = len(self.templates)
        return _("Templates (%(num_templates)s rendered)") % {'num_templates': num_templates}

    @property
    def nav_subtitle(self):
        if self.templates:
            return self.templates[0]['template'].name
        return ''

    template = 'debug_toolbar/panels/templates.html'

    @classmethod
    def get_urls(cls):
        return [
            url(r'^template_source/$', views.template_source, name='template_source'),
        ]

    def enable_instrumentation(self):
        template_rendered.connect(self._store_template_info)

    def disable_instrumentation(self):
        template_rendered.disconnect(self._store_template_info)

    def generate_stats(self, request, response):
        template_context = []
        for template_data in self.templates:
            info = {}
            # Clean up some info about templates
            template = template_data.get('template', None)
            if hasattr(template, 'origin') and template.origin and template.origin.name:
                template.origin_name = template.origin.name
                template.origin_hash = signing.dumps(template.origin.name)
            else:
                template.origin_name = _('No origin')
                template.origin_hash = ''
            info['template'] = template
            # Clean up context for better readability
            if self.toolbar.config['SHOW_TEMPLATE_CONTEXT']:
                context_list = template_data.get('context', [])
                info['context'] = '\n'.join(context_list)
            template_context.append(info)

        # Fetch context_processors/template_dirs from any template
        if self.templates:
            context_processors = self.templates[0]['context_processors']
            template = self.templates[0]['template']
            # django templates have the 'engine' attribute, while jinja templates use 'backend'
            engine_backend = getattr(template, 'engine', None) or getattr(template, 'backend')
            template_dirs = engine_backend.dirs
        else:
            context_processors = None
            template_dirs = []

        self.record_stats({
            'templates': template_context,
            'template_dirs': [normpath(x) for x in template_dirs],
            'context_processors': context_processors,
        })