Skip to content

Commit

Permalink
Merge pull request #1453 from tim-schilling/fix-url-expectation
Browse files Browse the repository at this point in the history
Fixed issue with toolbar expecting urls to start with __debug__.
  • Loading branch information
matthiask committed Mar 6, 2021
2 parents 06bf4ba + 135e7ca commit b17d7bc
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
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
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
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 URL paths 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
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
@@ -0,0 +1,2 @@
"""Invalid urls.py file for testing"""
urlpatterns = []

0 comments on commit b17d7bc

Please sign in to comment.