1
1
from __future__ import absolute_import , unicode_literals
2
2
3
+ try :
4
+ from collections import OrderedDict
5
+ except ImportError :
6
+ from django .utils .datastructures import SortedDict as OrderedDict
3
7
from os .path import normpath
4
8
from pprint import pformat
5
9
8
12
from django .conf import settings
9
13
from django .conf .urls import patterns , url
10
14
from django .db .models .query import QuerySet , RawQuerySet
15
+ from django .template import Context , RequestContext , Template
11
16
from django .template .context import get_standard_processors
12
17
from django .test .signals import template_rendered
18
+ from django .test .utils import instrumented_test_render
13
19
from django .utils .encoding import force_text
14
20
from django .utils import six
15
21
from django .utils .translation import ugettext_lazy as _
16
22
17
23
from debug_toolbar .panels import Panel
18
24
from debug_toolbar .panels .sql .tracking import recording , SQLQueryTriggered
19
25
20
- # Code taken and adapted from Simon Willison and Django Snippets:
21
- # http://www.djangosnippets.org/snippets/766/
22
26
23
27
# Monkey-patch to enable the template_rendered signal. The receiver returns
24
28
# immediately when the panel is disabled to keep the overhead small.
25
29
26
- from django . test . utils import instrumented_test_render
27
- from django . template import Template
30
+ # Code taken and adapted from Simon Willison and Django Snippets:
31
+ # http://www.djangosnippets.org/snippets/766/
28
32
29
33
if Template ._render != instrumented_test_render :
30
34
Template .original_render = Template ._render
31
35
Template ._render = instrumented_test_render
32
36
33
37
38
+ # Monkey-patch to store items added by template context processors. The
39
+ # overhead is sufficiently small to justify enabling it unconditionally.
40
+
41
+ def _request_context__init__ (
42
+ self , request , dict_ = None , processors = None , current_app = None ,
43
+ use_l10n = None , use_tz = None ):
44
+ Context .__init__ (
45
+ self , dict_ , current_app = current_app ,
46
+ use_l10n = use_l10n , use_tz = use_tz )
47
+ if processors is None :
48
+ processors = ()
49
+ else :
50
+ processors = tuple (processors )
51
+ self .context_processors = OrderedDict ()
52
+ updates = dict ()
53
+ for processor in get_standard_processors () + processors :
54
+ name = '%s.%s' % (processor .__module__ , processor .__name__ )
55
+ context = processor (request )
56
+ self .context_processors [name ] = context
57
+ updates .update (context )
58
+ self .update (updates )
59
+
60
+ RequestContext .__init__ = _request_context__init__
61
+
62
+
63
+ # Monkey-patch versions of Django where Template doesn't store origin.
64
+ # See https://code.djangoproject.com/ticket/16096.
65
+
34
66
if django .VERSION [:2 ] < (1 , 7 ):
35
- # Monkey-patch versions of Django where Template doesn't store origin.
36
- # See https://code.djangoproject.com/ticket/16096.
37
67
38
68
old_template_init = Template .__init__
39
69
@@ -107,6 +137,7 @@ def _store_template_info(self, sender, **kwargs):
107
137
pass
108
138
109
139
kwargs ['context' ] = [force_text (item ) for item in context_list ]
140
+ kwargs ['context_processors' ] = getattr (context , 'context_processors' , None )
110
141
self .templates .append (kwargs )
111
142
112
143
# Implement the Panel API
@@ -127,12 +158,6 @@ def get_urls(cls):
127
158
)
128
159
129
160
def process_response (self , request , response ):
130
- context_processors = dict (
131
- [
132
- ("%s.%s" % (k .__module__ , k .__name__ ),
133
- pformat (k (request ))) for k in get_standard_processors ()
134
- ]
135
- )
136
161
template_context = []
137
162
for template_data in self .templates :
138
163
info = {}
@@ -151,6 +176,12 @@ def process_response(self, request, response):
151
176
info ['context' ] = '\n ' .join (context_list )
152
177
template_context .append (info )
153
178
179
+ # Fetch context_processors from any template
180
+ if self .templates :
181
+ context_processors = self .templates [0 ]['context_processors' ]
182
+ else :
183
+ context_processors = None
184
+
154
185
self .record_stats ({
155
186
'templates' : template_context ,
156
187
'template_dirs' : [normpath (x ) for x in settings .TEMPLATE_DIRS ],
0 commit comments