Permalink
Browse files

Instantiate only one toolbar object on load.

Start of work on having one toolbar object and its panels handle multiple
requests, getting rid of the need to instantiate a toolbar and all panels per
request.  This will require much more work in panels to make sure everything is
thread-safe.
  • Loading branch information...
1 parent ee18112 commit 7fc06d9acddd41fd5e8b84d52641197abe7c8110 @robhudson committed Apr 21, 2010
Showing with 28 additions and 22 deletions.
  1. +10 −10 debug_toolbar/middleware.py
  2. +18 −12 debug_toolbar/toolbar/loader.py
@@ -32,7 +32,7 @@ class DebugToolbarMiddleware(object):
on outgoing response.
"""
def __init__(self):
- self.debug_toolbars = {}
+ self.debug_toolbar = DebugToolbar()
self.override_url = True
# Set method to use to decide to show toolbar
@@ -66,6 +66,7 @@ def _show_toolbar(self, request):
def process_request(self, request):
if self.show_toolbar(request):
+ self.debug_toolbar.process_request(request)
if self.override_url:
original_urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
debug_toolbar.urls.urlpatterns += patterns('',
@@ -74,19 +75,18 @@ def process_request(self, request):
self.override_url = False
request.urlconf = 'debug_toolbar.urls'
- self.debug_toolbars[request] = DebugToolbar(request)
- for panel in self.debug_toolbars[request].panels:
+ for panel in self.debug_toolbar.panels:
panel.process_request(request)
def process_view(self, request, view_func, view_args, view_kwargs):
- if request in self.debug_toolbars:
- for panel in self.debug_toolbars[request].panels:
+ if request in self.debug_toolbar.requests:
+ for panel in self.debug_toolbar.panels:
panel.process_view(request, view_func, view_args, view_kwargs)
def process_response(self, request, response):
- if request not in self.debug_toolbars:
+ if request not in self.debug_toolbar.requests:
return response
- if self.debug_toolbars[request].config['INTERCEPT_REDIRECTS']:
+ if self.debug_toolbar.config['INTERCEPT_REDIRECTS']:
if isinstance(response, HttpResponseRedirect):
redirect_to = response.get('Location', None)
if redirect_to:
@@ -95,14 +95,14 @@ def process_response(self, request, response):
{'redirect_to': redirect_to}
)
if response.status_code == 200:
- for panel in self.debug_toolbars[request].panels:
+ for panel in self.debug_toolbar.panels:
panel.process_response(request, response)
if response['Content-Type'].split(';')[0] in _HTML_TYPES:
response.content = replace_insensitive(
smart_unicode(response.content),
self.tag,
- smart_unicode(self.debug_toolbars[request].render_toolbar() + self.tag))
+ smart_unicode(self.debug_toolbar.render_toolbar() + self.tag))
if response.get('Content-Length', None):
response['Content-Length'] = len(response.content)
- del self.debug_toolbars[request]
+ del self.debug_toolbar.requests[request]
return response
@@ -6,20 +6,14 @@
class DebugToolbar(object):
- def __init__(self, request):
- self.request = request
- self.panels = []
- base_url = self.request.META.get('SCRIPT_NAME', '')
- self.config = {
- 'INTERCEPT_REDIRECTS': True,
- 'MEDIA_URL': u'%s/__debug__/m/' % base_url
- }
+ config = {}
+ panels = []
+ requests = {}
+ template_context = {}
+
+ def __init__(self):
# Check if settings has a DEBUG_TOOLBAR_CONFIG and updated config
self.config.update(getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}))
- self.template_context = {
- 'BASE_URL': base_url, # for backwards compatibility
- 'DEBUG_TOOLBAR_MEDIA_URL': self.config.get('MEDIA_URL'),
- }
# Override this tuple by copying to settings.py as `DEBUG_TOOLBAR_PANELS`
self.default_panels = (
'debug_toolbar.panels.version.VersionDebugPanel',
@@ -35,6 +29,18 @@ def __init__(self, request):
)
self.load_panels()
+ def process_request(self, request):
+ self.requests[request] = []
+ base_url = request.META.get('SCRIPT_NAME', '')
+ self.config.update({
+ 'INTERCEPT_REDIRECTS': True,
+ 'MEDIA_URL': u'%s/__debug__/m/' % (base_url,)
+ })
+ self.template_context.update({
+ 'BASE_URL': base_url, # for backwards compatibility
+ 'DEBUG_TOOLBAR_MEDIA_URL': self.config.get('MEDIA_URL'),
+ })
+
def load_panels(self):
"""
Populate debug panels

0 comments on commit 7fc06d9

Please sign in to comment.