diff options
| author | Aymeric Augustin | 2013-11-11 16:15:30 +0100 |
|---|---|---|
| committer | Aymeric Augustin | 2013-11-11 19:33:48 +0100 |
| commit | ef23e6dc31c66abd0c6784bf0044f53271d34b1e (patch) | |
| tree | 9238fb8b416086ee146cf3dc290eb8627cc62c86 /debug_toolbar | |
| parent | 0b9367398e86a2b50b0e2b12d9ec05e4a25bc7ee (diff) | |
| download | django-debug-toolbar-ef23e6dc31c66abd0c6784bf0044f53271d34b1e.tar.bz2 | |
Simplify monkey-patching of URLs into urlconf.
Diffstat (limited to 'debug_toolbar')
| -rw-r--r-- | debug_toolbar/middleware.py | 26 | ||||
| -rw-r--r-- | debug_toolbar/models.py | 10 | ||||
| -rw-r--r-- | debug_toolbar/urls.py | 16 |
3 files changed, 17 insertions, 35 deletions
diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index daea751..c95cade 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -4,17 +4,13 @@ Debug Toolbar middleware from __future__ import unicode_literals -import imp import threading from django.conf import settings from django.http import HttpResponseRedirect from django.shortcuts import render from django.utils.encoding import force_text -from django.utils.importlib import import_module -from django.utils import six -import debug_toolbar.urls from debug_toolbar.toolbar.loader import DebugToolbar from debug_toolbar.utils.settings import CONFIG @@ -55,8 +51,6 @@ class DebugToolbarMiddleware(object): debug_toolbars = {} def __init__(self): - self._urlconfs = {} - # The method to call to decide to show the toolbar self.show_toolbar = CONFIG['SHOW_TOOLBAR_CALLBACK'] or show_toolbar @@ -66,26 +60,6 @@ class DebugToolbarMiddleware(object): def process_request(self, request): __traceback_hide__ = True # noqa if self.show_toolbar(request): - urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF) - if isinstance(urlconf, six.string_types): - 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 + - list(urlconf.urlpatterns)) - - if hasattr(urlconf, 'handler403'): - new_urlconf.handler403 = urlconf.handler403 - 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: panel.enabled = panel.dom_id() not in request.COOKIES diff --git a/debug_toolbar/models.py b/debug_toolbar/models.py index 397771f..772b52b 100644 --- a/debug_toolbar/models.py +++ b/debug_toolbar/models.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals from django.conf import settings +from django.conf.urls import include, patterns, url +from django.core.urlresolvers import reverse, NoReverseMatch from django.utils.importlib import import_module from debug_toolbar.toolbar.loader import load_panel_classes @@ -36,5 +38,13 @@ if not any(is_toolbar_middleware(middleware) prepend_to_setting('MIDDLEWARE_CLASSES', 'debug_toolbar.middleware.DebugToolbarMiddleware') +try: + reverse('djdt:render_panel') +except NoReverseMatch: + urlconf_module = import_module(settings.ROOT_URLCONF) + urlconf_module.urlpatterns += patterns('', + url(r'^__debug__/', include('debug_toolbar.urls', namespace='djdt', app_name='djdt')), + ) + load_panel_classes() diff --git a/debug_toolbar/urls.py b/debug_toolbar/urls.py index 05ef245..860187a 100644 --- a/debug_toolbar/urls.py +++ b/debug_toolbar/urls.py @@ -1,20 +1,18 @@ """ URLpatterns for the debug toolbar. -These should not be loaded explicitly; the debug toolbar middleware will patch -this into the urlconf for the request. +The debug toolbar middleware will monkey-patch them into the default urlconf +if they aren't explicitly included. """ from __future__ import unicode_literals from django.conf.urls import patterns, url -_PREFIX = '__debug__' - urlpatterns = patterns('debug_toolbar.views', # noqa - url(r'^%s/render_panel/$' % _PREFIX, 'render_panel', name='render_panel'), - url(r'^%s/sql_select/$' % _PREFIX, 'sql_select', name='sql_select'), - url(r'^%s/sql_explain/$' % _PREFIX, 'sql_explain', name='sql_explain'), - url(r'^%s/sql_profile/$' % _PREFIX, 'sql_profile', name='sql_profile'), - url(r'^%s/template_source/$' % _PREFIX, 'template_source', name='template_source'), + url(r'^render_panel/$', 'render_panel', name='render_panel'), + url(r'^sql_select/$', 'sql_select', name='sql_select'), + url(r'^sql_explain/$', 'sql_explain', name='sql_explain'), + url(r'^sql_profile/$', 'sql_profile', name='sql_profile'), + url(r'^template_source/$', 'template_source', name='template_source'), ) |
