From d07c2d56524cd30bfe95d6457b247fb9ca1c0034 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 12 Feb 2021 08:54:34 -0600 Subject: [PATCH 1/2] Fixed issue with toolbar expecting urls to start with __debug__. When the history panel was added and we expanded the number of urls we were instrumenting, there was a check added to exclude the requests to the toolbar's views. This was done by checking the path of the request to see if it started with __debug__. However, the documentation states that is not a requirement. This change checks the urls to see if the namespace of the resolved url matches the toolbar's namespace. This still isn't perfect as another url path could contain the last namespace of djdt, but this is much less likely than a user defining the toolbar's urls at a different path than /__debug__/ --- debug_toolbar/middleware.py | 2 +- debug_toolbar/toolbar.py | 16 +++++++++++++++- docs/changes.rst | 3 ++- tests/test_integration.py | 19 +++++++++++++++++++ tests/urls_invalid.py | 2 ++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/urls_invalid.py diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 69100b20d..e1cf32d23 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -44,7 +44,7 @@ def __init__(self, get_response): def __call__(self, request): # Decide whether the toolbar is active for this request. show_toolbar = get_show_toolbar() - if not show_toolbar(request) or request.path.startswith("/__debug__/"): + if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request): return self.get_response(request) toolbar = DebugToolbar(request, self.get_response) diff --git a/debug_toolbar/toolbar.py b/debug_toolbar/toolbar.py index 9638ba1f8..fd82d62e2 100644 --- a/debug_toolbar/toolbar.py +++ b/debug_toolbar/toolbar.py @@ -9,7 +9,8 @@ from django.core.exceptions import ImproperlyConfigured from django.template import TemplateSyntaxError from django.template.loader import render_to_string -from django.urls import path +from django.urls import path, resolve +from django.urls.exceptions import Resolver404 from django.utils.module_loading import import_string from debug_toolbar import settings as dt_settings @@ -133,6 +134,19 @@ def get_urls(cls): cls._urlpatterns = urlpatterns return cls._urlpatterns + @classmethod + def is_toolbar_request(cls, request): + """ + Determine if the request is for a DebugToolbar view. + """ + # The primary caller of this function is in the middleware which may + # not have resolver_match set. + try: + resolver_match = request.resolver_match or resolve(request.path) + except Resolver404: + return False + return resolver_match.namespaces and resolver_match.namespaces[-1] == app_name + app_name = "djdt" urlpatterns = DebugToolbar.get_urls() diff --git a/docs/changes.rst b/docs/changes.rst index 6207b6797..8686ae9b2 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -9,7 +9,8 @@ Next version * Added ``PRETTIFY_SQL`` configuration option to support controlling SQL token grouping. By default it's set to True. When set to False, a performance improvement can be seen by the SQL panel. - +* Fixed issue with toolbar expecting urls to start with `/__debug__/` + while the documentation indicates it's not required. 3.2 (2020-12-03) ---------------- diff --git a/tests/test_integration.py b/tests/test_integration.py index 8c28f6c6e..59ea1c25f 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -101,6 +101,25 @@ def test_cache_page(self): self.client.get("/cached_view/") self.assertEqual(len(self.toolbar.get_panel_by_id("CachePanel").calls), 5) + def test_is_toolbar_request(self): + self.request.path = "/__debug__/render_panel/" + self.assertTrue(self.toolbar.is_toolbar_request(self.request)) + + self.request.path = "/invalid/__debug__/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + + self.request.path = "/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + + @override_settings(ROOT_URLCONF="tests.urls_invalid") + def test_is_toolbar_request_without_djdt_urls(self): + """Test cases when the toolbar urls aren't configured.""" + self.request.path = "/__debug__/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + + self.request.path = "/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + @override_settings(DEBUG=True) class DebugToolbarIntegrationTestCase(IntegrationTestCase): diff --git a/tests/urls_invalid.py b/tests/urls_invalid.py new file mode 100644 index 000000000..ccadb6735 --- /dev/null +++ b/tests/urls_invalid.py @@ -0,0 +1,2 @@ +"""Invalid urls.py file for testing""" +urlpatterns = [] From 135e7ca881207f801350dd044d6bd5c77077ddf6 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 12 Feb 2021 09:02:23 -0600 Subject: [PATCH 2/2] Fixed documentation spelling error. --- docs/changes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 8686ae9b2..bef5d1964 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -9,7 +9,7 @@ Next version * Added ``PRETTIFY_SQL`` configuration option to support controlling SQL token grouping. By default it's set to True. When set to False, a performance improvement can be seen by the SQL panel. -* Fixed issue with toolbar expecting urls to start with `/__debug__/` +* Fixed issue with toolbar expecting URL paths to start with `/__debug__/` while the documentation indicates it's not required. 3.2 (2020-12-03)