From 631bbd18c10f572e31ef30f4dc78df942beeffd4 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 9 Nov 2013 19:05:27 +0100 Subject: Avoid some implicit global lookups. They made it impossible to preserve panel data after the end of a request. --- debug_toolbar/panels/__init__.py | 11 +++++------ debug_toolbar/toolbar/loader.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/debug_toolbar/panels/__init__.py b/debug_toolbar/panels/__init__.py index 8be87e1..a48b1b3 100644 --- a/debug_toolbar/panels/__init__.py +++ b/debug_toolbar/panels/__init__.py @@ -18,7 +18,8 @@ class DebugPanel(object): context = {} # Panel methods - def __init__(self, context={}): + def __init__(self, toolbar, context={}): + self.toolbar = toolbar self.context.update(context) self.slug = slugify(self.name) @@ -44,16 +45,14 @@ class DebugPanel(object): return render_to_string(self.template, context) def record_stats(self, stats): - toolbar = DebugToolbarMiddleware.get_current() - panel_stats = toolbar.stats.get(self.slug) + panel_stats = self.toolbar.stats.get(self.slug) if panel_stats: panel_stats.update(stats) else: - toolbar.stats[self.slug] = stats + self.toolbar.stats[self.slug] = stats def get_stats(self): - toolbar = DebugToolbarMiddleware.get_current() - return toolbar.stats.get(self.slug, {}) + return self.toolbar.stats.get(self.slug, {}) # Standard middleware methods diff --git a/debug_toolbar/toolbar/loader.py b/debug_toolbar/toolbar/loader.py index c8630a9..7b525b6 100644 --- a/debug_toolbar/toolbar/loader.py +++ b/debug_toolbar/toolbar/loader.py @@ -45,7 +45,7 @@ class DebugToolbar(object): """ global panel_classes for panel_class in panel_classes: - panel_instance = panel_class(context=self.template_context) + panel_instance = panel_class(self, context=self.template_context) self._panels[panel_class] = panel_instance -- cgit v1.2.3 From 2816c6fa2a1613e5eeb7967cde0793019ce62feb Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 9 Nov 2013 19:07:38 +0100 Subject: Load the content of panels dynamically. This should drastically reduce the overhead of the browser toolbar when a page has a complex template structure or many SQL queries. This change is backwards-incompatible for third-party panels because it changes the signature of __init__. The JavaScript could probably be improved; I'm outside my comfort zone. --- debug_toolbar/panels/__init__.py | 2 +- debug_toolbar/static/debug_toolbar/js/toolbar.js | 17 +++++++++++++++++ debug_toolbar/templates/debug_toolbar/base.html | 13 +++++++------ debug_toolbar/toolbar/loader.py | 23 +++++++++++++++++++++-- debug_toolbar/urls.py | 1 + debug_toolbar/views.py | 21 ++++++++++++++++++++- 6 files changed, 67 insertions(+), 10 deletions(-) diff --git a/debug_toolbar/panels/__init__.py b/debug_toolbar/panels/__init__.py index a48b1b3..c830bb2 100644 --- a/debug_toolbar/panels/__init__.py +++ b/debug_toolbar/panels/__init__.py @@ -11,7 +11,7 @@ class DebugPanel(object): """ # name = 'Base' # template = 'debug_toolbar/panels/base.html' - has_content = False # If content returns something, set to true in subclass + has_content = False # If content returns something, set to True in subclass # We'll maintain a local context instance so we can expose our template # context variables to panels which need them: diff --git a/debug_toolbar/static/debug_toolbar/js/toolbar.js b/debug_toolbar/static/debug_toolbar/js/toolbar.js index 094c3ac..0b7d1ec 100644 --- a/debug_toolbar/static/debug_toolbar/js/toolbar.js +++ b/debug_toolbar/static/debug_toolbar/js/toolbar.js @@ -28,6 +28,23 @@ $(this).parent().removeClass('active'); } else { $('.panelContent').hide(); // Hide any that are already open + var inner = current.find('.djDebugPanelContent .scroll').first(); + if ($(inner).empty()) { + var ajax_data = { + data: { + toolbar_id: $('#djDebug').data('toolbar-id'), + panel_id: this.className + }, + type: 'GET', + url: $('#djDebug').data('render-panel-url') + }; + $.ajax(ajax_data).done(function(data){ + inner.html(data); + }).fail(function(xhr){ + var message = '
Back

'+xhr.status+': '+xhr.statusText+'

'; + $('#djDebugWindow').html(message).show(); + }); + } current.show(); $('#djDebugToolbar li').removeClass('active'); $(this).parent().addClass('active'); diff --git a/debug_toolbar/templates/debug_toolbar/base.html b/debug_toolbar/templates/debug_toolbar/base.html index e214044..d5a356c 100644 --- a/debug_toolbar/templates/debug_toolbar/base.html +++ b/debug_toolbar/templates/debug_toolbar/base.html @@ -8,7 +8,9 @@ if(!window.jQuery) document.write(' -