diff options
Diffstat (limited to 'debug_toolbar/middleware.py')
| -rw-r--r-- | debug_toolbar/middleware.py | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index de4ae9c..f49a29f 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -1,13 +1,15 @@ """ Debug Toolbar middleware """ +import imp import thread from django.conf import settings +from django.conf.urls.defaults import include, patterns from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.utils.encoding import smart_unicode -from django.conf.urls.defaults import include, patterns +from django.utils.importlib import import_module import debug_toolbar.urls from debug_toolbar.toolbar.loader import DebugToolbar @@ -32,13 +34,13 @@ class DebugToolbarMiddleware(object): on outgoing response. """ debug_toolbars = {} - + @classmethod def get_current(cls): return cls.debug_toolbars.get(thread.get_ident()) def __init__(self): - self.override_url = True + self._urlconfs = {} # Set method to use to decide to show toolbar self.show_toolbar = self._show_toolbar # default @@ -57,32 +59,39 @@ class DebugToolbarMiddleware(object): self.tag = u'</' + tag + u'>' def _show_toolbar(self, request): + if getattr(settings, 'TEST', False): + return False + x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', None) if x_forwarded_for: remote_addr = x_forwarded_for.split(',')[0].strip() else: remote_addr = request.META.get('REMOTE_ADDR', None) - if not remote_addr in settings.INTERNAL_IPS \ - or (request.is_ajax() and \ - not debug_toolbar.urls._PREFIX in request.path) \ - or not (settings.DEBUG or getattr(settings, 'TEST', False)): - return False - return True + + # if not internal ip, and not DEBUG + return remote_addr in settings.INTERNAL_IPS and bool(settings.DEBUG) def process_request(self, request): __traceback_hide__ = True if self.show_toolbar(request): - if self.override_url: - original_urlconf = __import__(getattr(request, 'urlconf', settings.ROOT_URLCONF), {}, {}, ['*']) - debug_toolbar.urls.urlpatterns += patterns('', - ('', include(original_urlconf)), - ) - if hasattr(original_urlconf, 'handler404'): - debug_toolbar.urls.handler404 = original_urlconf.handler404 - if hasattr(original_urlconf, 'handler500'): - debug_toolbar.urls.handler500 = original_urlconf.handler500 - self.override_url = False - request.urlconf = 'debug_toolbar.urls' + + urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF) + if isinstance(urlconf, basestring): + urlconf = import_module(getattr(request, 'urlconf', settings.ROOT_URLCONF)) + + if urlconf not in self._urlconfs: + new_urlconf = imp.new_module('urlconf') + new_urlconf.urlpatterns = debug_toolbar.urls.urlpatterns + \ + urlconf.urlpatterns + + if hasattr(urlconf, 'handler404'): + new_urlconf.handler404 = urlconf.handler404 + if hasattr(urlconf, 'handler500'): + new_urlconf.handler500 = urlconf.handler500 + + self._urlconfs[urlconf] = new_urlconf + + request.urlconf = self._urlconfs[urlconf] toolbar = DebugToolbar(request) for panel in toolbar.panels: @@ -119,7 +128,7 @@ class DebugToolbarMiddleware(object): for panel in toolbar.panels: panel.process_response(request, response) response.content = replace_insensitive( - smart_unicode(response.content), + smart_unicode(response.content), self.tag, smart_unicode(toolbar.render_toolbar() + self.tag)) if response.get('Content-Length', None): |
