From 71a2c1237de8ed759c3ba415c8bfd91b62adf193 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 16 Nov 2013 09:47:14 +0100 Subject: Clean up DebugToolbar class, especially panels handling. --- debug_toolbar/panels/__init__.py | 8 +++-- debug_toolbar/panels/redirects.py | 2 +- debug_toolbar/templates/debug_toolbar/base.html | 6 ++-- debug_toolbar/toolbar.py | 40 ++++++++++++++----------- debug_toolbar/views.py | 7 ++--- 5 files changed, 34 insertions(+), 29 deletions(-) (limited to 'debug_toolbar') diff --git a/debug_toolbar/panels/__init__.py b/debug_toolbar/panels/__init__.py index d64aac2..37d7eb7 100644 --- a/debug_toolbar/panels/__init__.py +++ b/debug_toolbar/panels/__init__.py @@ -31,11 +31,13 @@ class DebugPanel(object): context.update(self.get_stats()) return render_to_string(self.template, context) - def dom_id(self): - return 'djDebug%sPanel' % (self.name.replace(' ', '')) + @property + def panel_id(self): + return self.__class__.__name__ + @property def enabled(self): - return self.toolbar.request.COOKIES.get(self.dom_id(), 'on') == 'on' + return self.toolbar.request.COOKIES.get('djdt' + self.panel_id, 'on') == 'on' # URLs for panel-specific views diff --git a/debug_toolbar/panels/redirects.py b/debug_toolbar/panels/redirects.py index 244f7ed..22add70 100644 --- a/debug_toolbar/panels/redirects.py +++ b/debug_toolbar/panels/redirects.py @@ -18,7 +18,7 @@ class InterceptRedirectsPanel(DebugPanel): def enabled(self): default = 'on' if self.toolbar.config['INTERCEPT_REDIRECTS'] else 'off' - return self.toolbar.request.COOKIES.get(self.dom_id(), default) == 'on' + return self.toolbar.request.COOKIES.get('djdt' + self.panel_id, default) == 'on' def process_response(self, request, response): if isinstance(response, HttpResponseRedirect): diff --git a/debug_toolbar/templates/debug_toolbar/base.html b/debug_toolbar/templates/debug_toolbar/base.html index 19b32cf..424dc57 100644 --- a/debug_toolbar/templates/debug_toolbar/base.html +++ b/debug_toolbar/templates/debug_toolbar/base.html @@ -20,9 +20,9 @@ if(!window.jQuery) document.write(' - + {% if panel.has_content and panel.enabled %} - + {% else %}
{% endif %} @@ -46,7 +46,7 @@ if(!window.jQuery) document.write(' +
{% trans "Close" %}

{{ panel.title|safe }}

diff --git a/debug_toolbar/toolbar.py b/debug_toolbar/toolbar.py index acb86d9..8b48a9f 100644 --- a/debug_toolbar/toolbar.py +++ b/debug_toolbar/toolbar.py @@ -29,28 +29,34 @@ class DebugToolbar(object): 'STATIC_URL': settings.STATIC_URL, 'TOOLBAR_ROOT_TAG_ATTRS': self.config['ROOT_TAG_ATTRS'], } - - self.load_panels() self.stats = {} + for panel_class in self.get_panel_classes(): + panel_instance = panel_class(self, context=self.template_context) + self._panels[panel_instance.panel_id] = panel_instance - def _get_panels(self): - return list(self._panels.values()) - panels = property(_get_panels) + # Manage panels - def _get_enabled_panels(self): - return [panel for panel in self._panels.values() if panel.enabled()] - enabled_panels = property(_get_enabled_panels) + @property + def panels(self): + """ + Get a list of all available panels. + """ + return list(self._panels.values()) - def get_panel(self, cls): - return self._panels[cls] + @property + def enabled_panels(self): + """ + Get a list of panels enabled for the current request. + """ + return [panel for panel in self._panels.values() if panel.enabled] - def load_panels(self): + def get_panel_by_id(self, panel_id): """ - Populate debug panels + Get the panel with the given id, which is the class name by default. """ - for panel_class in self.get_panel_classes(): - panel_instance = panel_class(self, context=self.template_context) - self._panels[panel_class] = panel_instance + return self._panels[panel_id] + + # Handle rendering the toolbar in HTML def render_toolbar(self): """ @@ -86,8 +92,8 @@ class DebugToolbar(object): def fetch(cls, storage_id): return cls._storage.get(storage_id) - # Manually implement class-level caching of the list of panels because - # it's more obvious than going through an abstraction. + # Manually implement class-level caching of panel classes and url patterns + # because it's more obvious than going through an abstraction. _panel_classes = None diff --git a/debug_toolbar/views.py b/debug_toolbar/views.py index 07e38d9..eb67e8b 100644 --- a/debug_toolbar/views.py +++ b/debug_toolbar/views.py @@ -15,9 +15,6 @@ def render_panel(request): "Please reload the page and retry.") content = "

%s

" % escape(content) else: - panel_id = request.GET['panel_id'] - for panel in toolbar.panels: - if panel.dom_id() == panel_id: - content = panel.content() - break + panel = toolbar.get_panel_by_id(request.GET['panel_id']) + content = panel.content() return HttpResponse(content) -- cgit v1.2.3