Skip to content

Commit

Permalink
Fixed issue with toolbar expecting urls to start with __debug__.
Browse files Browse the repository at this point in the history
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__/
  • Loading branch information
tim-schilling committed Feb 12, 2021
1 parent 3dc5437 commit d07c2d5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
----------------
Expand Down
19 changes: 19 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/urls_invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Invalid urls.py file for testing"""
urlpatterns = []

0 comments on commit d07c2d5

Please sign in to comment.