Skip to content

Commit 78a8b44

Browse files
committed
Added convenience methods to make it easier to work with panel stats
1 parent 03cd2de commit 78a8b44

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

debug_toolbar/panels/__init__.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
1-
"""Base DebugPanel class"""
1+
from django.template.defaultfilters import slugify
2+
from debug_toolbar.middleware import DebugToolbarMiddleware
3+
24

35
class DebugPanel(object):
46
"""
57
Base class for debug panels.
68
"""
79
# name = Base
810
has_content = False # If content returns something, set to true in subclass
9-
11+
1012
# We'll maintain a local context instance so we can expose our template
1113
# context variables to panels which need them:
1214
context = {}
13-
15+
1416
# Panel methods
1517
def __init__(self, context={}):
1618
self.context.update(context)
17-
19+
self.toolbar = DebugToolbarMiddleware.get_current()
20+
self.slug = slugify(self.name)
21+
1822
def dom_id(self):
1923
return 'djDebug%sPanel' % (self.name.replace(' ', ''))
20-
24+
2125
def nav_title(self):
2226
"""Title showing in toolbar"""
2327
raise NotImplementedError
24-
28+
2529
def nav_subtitle(self):
2630
"""Subtitle showing until title in toolbar"""
2731
return ''
28-
32+
2933
def title(self):
3034
"""Title showing in panel"""
3135
raise NotImplementedError
32-
36+
3337
def url(self):
3438
raise NotImplementedError
35-
39+
3640
def content(self):
3741
raise NotImplementedError
38-
42+
43+
def record_stats(self, stats):
44+
self.toolbar.stats[self.slug].update(stats)
45+
46+
def get_stats(self):
47+
return self.toolbar.stats[self.slug]
48+
3949
# Standard middleware methods
4050
def process_request(self, request):
4151
pass
42-
52+
4353
def process_view(self, request, view_func, view_args, view_kwargs):
4454
pass
45-
55+
4656
def process_response(self, request, response):
4757
pass
48-

debug_toolbar/toolbar/loader.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.utils.safestring import mark_safe
1010

1111
class DebugToolbar(object):
12-
12+
1313
def __init__(self, request):
1414
self.request = request
1515
self._panels = SortedDict()
@@ -38,25 +38,26 @@ def __init__(self, request):
3838
'debug_toolbar.panels.logger.LoggingPanel',
3939
)
4040
self.load_panels()
41+
self.stats = {}
4142

4243
def _get_panels(self):
4344
return self._panels.values()
4445
panels = property(_get_panels)
45-
46+
4647
def get_panel(self, cls):
4748
return self._panels[cls]
48-
49+
4950
def load_panels(self):
5051
"""
5152
Populate debug panels
5253
"""
5354
from django.conf import settings
5455
from django.core import exceptions
55-
56+
5657
# Check if settings has a DEBUG_TOOLBAR_PANELS, otherwise use default
5758
if hasattr(settings, 'DEBUG_TOOLBAR_PANELS'):
5859
self.default_panels = settings.DEBUG_TOOLBAR_PANELS
59-
60+
6061
for panel_path in self.default_panels:
6162
try:
6263
dot = panel_path.rindex('.')
@@ -71,14 +72,14 @@ def load_panels(self):
7172
panel_class = getattr(mod, panel_classname)
7273
except AttributeError:
7374
raise exceptions.ImproperlyConfigured, 'Toolbar Panel module "%s" does not define a "%s" class' % (panel_module, panel_classname)
74-
75+
7576
try:
7677
panel_instance = panel_class(context=self.template_context)
7778
except:
7879
raise # Bubble up problem loading panel
79-
80+
8081
self._panels[panel_class] = panel_instance
81-
82+
8283
def render_toolbar(self):
8384
"""
8485
Renders the overall Toolbar with panels inside.
@@ -91,5 +92,5 @@ def render_toolbar(self):
9192
'js': mark_safe(open(os.path.join(media_path, 'js', 'toolbar.min.js'), 'r').read()),
9293
'css': mark_safe(open(os.path.join(media_path, 'css', 'toolbar.min.css'), 'r').read()),
9394
})
94-
95+
9596
return render_to_string('debug_toolbar/base.html', context)

0 commit comments

Comments
 (0)