Skip to content

Commit 4baa51e

Browse files
acdharobhudson
authored andcommitted
Template panel context cleanup.
All panels get a copy of the template context when created and use an updated copy when rendering so they can have full access to context vars and avoid making changes to the shared context. Signed-off-by: Rob Hudson <[email protected]>
1 parent fba93b8 commit 4baa51e

File tree

12 files changed

+67
-32
lines changed

12 files changed

+67
-32
lines changed

debug_toolbar/panels/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ class DebugPanel(object):
77
# name = Base
88
has_content = False # If content returns something, set to true in subclass
99

10+
# We'll maintain a local context instance so we can expose our template
11+
# context variables to panels which need them:
12+
context = {}
13+
1014
# Panel methods
11-
def __init__(self):
12-
pass
15+
def __init__(self, context={}):
16+
self.context.update(context)
1317

1418
def dom_id(self):
1519
return 'djDebug%sPanel' % (self.name.replace(' ', ''))

debug_toolbar/panels/cache.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class CacheDebugPanel(DebugPanel):
7676
name = 'Cache'
7777
has_content = True
7878

79-
def __init__(self):
79+
def __init__(self, *args, **kwargs):
80+
super(self.__class__, self).__init__(*args, **kwargs)
8081
# This is hackish but to prevent threading issues is somewhat needed
8182
if isinstance(cache.cache, CacheStatTracker):
8283
cache.cache.reset()
@@ -95,9 +96,10 @@ def url(http://github.com/self):
9596
return ''
9697

9798
def content(self):
98-
context = dict(
99-
cache_calls = len(self.cache.calls),
100-
cache_time = self.cache.total_time,
101-
cache = self.cache,
102-
)
99+
context = self.context.copy()
100+
context.update({
101+
'cache_calls': len(self.cache.calls),
102+
'cache_time': self.cache.total_time,
103+
'cache': self.cache,
104+
})
103105
return render_to_string('debug_toolbar/panels/cache.html', context)

debug_toolbar/panels/headers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def process_request(self, request):
4747
)
4848

4949
def content(self):
50-
context = {
50+
context = self.context.copy()
51+
context.update({
5152
'headers': self.headers
52-
}
53+
})
5354
return render_to_string('debug_toolbar/panels/headers.html', context)

debug_toolbar/panels/logger.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ def content(self):
7575
'file': record.pathname,
7676
'line': record.lineno,
7777
})
78-
return render_to_string('debug_toolbar/panels/logger.html', {'records': records})
78+
79+
context = self.context.copy()
80+
context.update({'records': records})
81+
82+
return render_to_string('debug_toolbar/panels/logger.html', context)
83+

debug_toolbar/panels/request_vars.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ def process_view(self, request, view_func, view_args, view_kwargs):
2727
self.view_kwargs = view_kwargs
2828

2929
def content(self):
30-
context = {
30+
context = self.context.copy()
31+
context.update({
3132
'get': [(k, self.request.GET.getlist(k)) for k in self.request.GET],
3233
'post': [(k, self.request.POST.getlist(k)) for k in self.request.POST],
3334
'cookies': [(k, self.request.COOKIES.get(k)) for k in self.request.COOKIES],
3435
'view_func': '%s.%s' % (self.view_func.__module__, self.view_func.__name__),
3536
'view_args': self.view_args,
3637
'view_kwargs': self.view_kwargs
37-
}
38+
})
3839
if hasattr(self.request, 'session'):
39-
context['session'] = [(k, self.request.session.get(k)) for k in self.request.session.iterkeys()]
40+
context.update({
41+
'session': [(k, self.request.session.get(k)) for k in self.request.session.iterkeys()]
42+
})
4043

4144
return render_to_string('debug_toolbar/panels/request_vars.html', context)

debug_toolbar/panels/settings_vars.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def url(http://github.com/self):
2222
return ''
2323

2424
def content(self):
25-
context = {
25+
context = self.context.copy()
26+
context.update({
2627
'settings': get_safe_settings(),
27-
}
28+
})
2829
return render_to_string('debug_toolbar/panels/settings_vars.html', context)

debug_toolbar/panels/signals.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ def content(self):
8080
text = "function %s" % receiver.__name__
8181
receivers.append(text)
8282
signals.append((name, signal, receivers))
83-
return render_to_string('debug_toolbar/panels/signals.html', {'signals': signals})
83+
84+
context = self.context.copy()
85+
context.update({'signals': signals})
86+
87+
return render_to_string('debug_toolbar/panels/signals.html', context)

debug_toolbar/panels/sql.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ class SQLDebugPanel(DebugPanel):
137137
name = 'SQL'
138138
has_content = True
139139

140-
def __init__(self):
140+
def __init__(self, *args, **kwargs):
141+
super(self.__class__, self).__init__(*args, **kwargs)
141142
self._offset = len(connection.queries)
142143
self._sql_time = 0
143144
self._queries = []
@@ -173,11 +174,13 @@ def content(self):
173174
query['start_offset'] = width_ratio_tally
174175
width_ratio_tally += query['width_ratio']
175176

176-
context = {
177+
context = self.context.copy()
178+
context.update({
177179
'queries': self._queries,
178180
'sql_time': self._sql_time,
179181
'is_mysql': settings.DATABASE_ENGINE == 'mysql',
180-
}
182+
})
183+
181184
return render_to_string('debug_toolbar/panels/sql.html', context)
182185

183186
def ms_from_timedelta(td):

debug_toolbar/panels/template.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class TemplateDebugPanel(DebugPanel):
3636
name = 'Template'
3737
has_content = True
3838

39-
def __init__(self):
39+
def __init__(self, *args, **kwargs):
40+
super(self.__class__, self).__init__(*args, **kwargs)
4041
self.templates = []
4142
template_rendered.connect(self._store_template_info)
4243

@@ -103,9 +104,12 @@ def content(self):
103104
pass
104105
info['context'] = '\n'.join(context_list)
105106
template_context.append(info)
106-
context = {
107+
108+
context = self.context.copy()
109+
context.update({
107110
'templates': template_context,
108111
'template_dirs': [normpath(x) for x in settings.TEMPLATE_DIRS],
109112
'context_processors': context_processors,
110-
}
113+
})
114+
111115
return render_to_string('debug_toolbar/panels/templates.html', context)

debug_toolbar/panels/timer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ def content(self):
8484
# ('Page faults', '%d no i/o, %d requiring i/o' % (minflt, majflt)),
8585
# ('Disk operations', '%d in, %d out, %d swapout' % (blkin, blkout, swap)),
8686
)
87-
context = {
87+
88+
context = self.context.copy()
89+
context.update({
8890
'rows': rows,
89-
}
91+
})
92+
9093
return render_to_string('debug_toolbar/panels/timer.html', context)

debug_toolbar/panels/version.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ def content(self):
5050
version = '.'.join(str(o) for o in version)
5151
versions[name] = version
5252

53-
return render_to_string('debug_toolbar/panels/versions.html', {
53+
context = self.context.copy()
54+
context.update({
5455
'versions': versions,
5556
'paths': sys.path,
5657
})
58+
59+
return render_to_string('debug_toolbar/panels/versions.html', context)

debug_toolbar/toolbar/loader.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def __init__(self, request):
1111
self.config = {
1212
'INTERCEPT_REDIRECTS': True,
1313
}
14+
self.template_context = {
15+
'BASE_URL': self.request.META.get('SCRIPT_NAME', ''),
16+
}
1417
# Override this tuple by copying to settings.py as `DEBUG_TOOLBAR_PANELS`
1518
self.default_panels = (
1619
'debug_toolbar.panels.version.VersionDebugPanel',
@@ -56,9 +59,8 @@ def load_panels(self):
5659
raise exceptions.ImproperlyConfigured, 'Toolbar Panel module "%s" does not define a "%s" class' % (panel_module, panel_classname)
5760

5861
try:
59-
panel_instance = panel_class()
62+
panel_instance = panel_class(context=self.template_context)
6063
except:
61-
print panel_class
6264
raise # Bubble up problem loading panel
6365

6466
self.panels.append(panel_instance)
@@ -67,7 +69,7 @@ def render_toolbar(self):
6769
"""
6870
Renders the overall Toolbar with panels inside.
6971
"""
70-
return render_to_string('debug_toolbar/base.html', {
71-
'panels': self.panels,
72-
'BASE_URL': self.request.META.get('SCRIPT_NAME', ''),
73-
})
72+
context = self.template_context.copy()
73+
context.update({ 'panels': self.panels, })
74+
75+
return render_to_string('debug_toolbar/base.html', context)

0 commit comments

Comments
 (0)