diff options
| author | Aymeric Augustin | 2013-11-16 09:47:14 +0100 | 
|---|---|---|
| committer | Aymeric Augustin | 2013-11-16 10:10:09 +0100 | 
| commit | 71a2c1237de8ed759c3ba415c8bfd91b62adf193 (patch) | |
| tree | 00bc509a00075a0a9266832ff054d9a2e80b9f6f /debug_toolbar | |
| parent | 6334983458abd4380c21275d1229527778cf93a6 (diff) | |
| download | django-debug-toolbar-71a2c1237de8ed759c3ba415c8bfd91b62adf193.tar.bz2 | |
Clean up DebugToolbar class, especially panels handling.
Diffstat (limited to 'debug_toolbar')
| -rw-r--r-- | debug_toolbar/panels/__init__.py | 8 | ||||
| -rw-r--r-- | debug_toolbar/panels/redirects.py | 2 | ||||
| -rw-r--r-- | debug_toolbar/templates/debug_toolbar/base.html | 6 | ||||
| -rw-r--r-- | debug_toolbar/toolbar.py | 40 | ||||
| -rw-r--r-- | debug_toolbar/views.py | 7 | 
5 files changed, 34 insertions, 29 deletions
| 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('<scr'+'ipt src="{{ STATIC_URL }}debug_toolbar  			{% endif %}  			{% for panel in panels %}  				<li class="djDebugPanelButton"> -					<input type="checkbox" data-cookie="{{ panel.dom_id }}" {% if panel.enabled %}checked="checked" title="{% trans "Disable for next and successive requests" %}"{% else %}title="{% trans "Enable for next and successive requests" %}"{% endif %} /> +					<input type="checkbox" data-cookie="djdt{{ panel.panel_id }}" {% if panel.enabled %}checked="checked" title="{% trans "Disable for next and successive requests" %}"{% else %}title="{% trans "Enable for next and successive requests" %}"{% endif %} />  					{% if panel.has_content and panel.enabled %} -						<a href="{{ panel.url|default:"#" }}" title="{{ panel.title }}" class="{{ panel.dom_id }}"> +						<a href="{{ panel.url|default:"#" }}" title="{{ panel.title }}" class="{{ panel.panel_id }}">  					{% else %}  						<div class="contentless{% if not panel.enabled %} disabled{% endif %}">  					{% endif %} @@ -46,7 +46,7 @@ if(!window.jQuery) document.write('<scr'+'ipt src="{{ STATIC_URL }}debug_toolbar  	</div>  	{% for panel in panels %}  		{% if panel.has_content and panel.enabled %} -			<div id="{{ panel.dom_id }}" class="panelContent"> +			<div id="{{ panel.panel_id }}" class="panelContent">  				<div class="djDebugPanelTitle">  					<a href="" class="djDebugClose">{% trans "Close" %}</a>  					<h3>{{ panel.title|safe }}</h3> 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 = "<p>%s</p>" % 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) | 
