Skip to content

Commit 14a5e0c

Browse files
committed
Make Panel.panel_id a classmember.
This avoids needing to create an instance of a panel to get its panel ID.
1 parent e2f695b commit 14a5e0c

16 files changed

+102
-61
lines changed

debug_toolbar/panels/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.template.loader import render_to_string
2+
from django.utils.functional import classproperty
23

34
from debug_toolbar import settings as dt_settings
45
from debug_toolbar.utils import get_name_from_obj
@@ -16,9 +17,9 @@ def __init__(self, toolbar, get_response):
1617

1718
# Private panel properties
1819

19-
@property
20-
def panel_id(self):
21-
return self.__class__.__name__
20+
@classproperty
21+
def panel_id(cls):
22+
return cls.__name__
2223

2324
@property
2425
def enabled(self) -> bool:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from debug_toolbar.panels.history.panel import HistoryPanel
22

3-
__all__ = ["HistoryPanel"]
3+
__all__ = [HistoryPanel.panel_id]

debug_toolbar/panels/sql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from debug_toolbar.panels.sql.panel import SQLPanel
22

3-
__all__ = ["SQLPanel"]
3+
__all__ = [SQLPanel.panel_id]

debug_toolbar/panels/sql/forms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ def clean_alias(self):
4646
return value
4747

4848
def clean(self):
49+
from debug_toolbar.panels.sql import SQLPanel
50+
4951
cleaned_data = super().clean()
5052
toolbar = DebugToolbar.fetch(
51-
self.cleaned_data["request_id"], panel_id="SQLPanel"
53+
self.cleaned_data["request_id"], panel_id=SQLPanel.panel_id
5254
)
5355
if toolbar is None:
5456
raise ValidationError(_("Data for this panel isn't available anymore."))
5557

56-
panel = toolbar.get_panel_by_id("SQLPanel")
58+
panel = toolbar.get_panel_by_id(SQLPanel.panel_id)
5759
# Find the query for this form submission
5860
query = None
5961
for q in panel.get_stats()["queries"]:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from debug_toolbar.panels.templates.panel import TemplatesPanel
22

3-
__all__ = ["TemplatesPanel"]
3+
__all__ = [TemplatesPanel.panel_id]

docs/changes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Serializable (don't include in main)
2222
this id and avoid passing SQL to be executed.
2323
* Move the formatting logic of SQL queries to just before rendering in
2424
``SQLPanel.content``.
25-
25+
* Make ``Panel.panel_id`` a class member.
2626

2727
Pending
2828
-------

tests/panels/test_cache.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from django.core import cache
22

3+
from debug_toolbar.panels.cache import CachePanel
4+
35
from ..base import BaseTestCase
46

57

68
class CachePanelTestCase(BaseTestCase):
7-
panel_id = "CachePanel"
9+
panel_id = CachePanel.panel_id
810

911
def test_recording(self):
1012
self.assertEqual(len(self.panel.calls), 0)

tests/panels/test_history.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.test import RequestFactory, override_settings
55
from django.urls import resolve, reverse
66

7+
from debug_toolbar.panels.history import HistoryPanel
78
from debug_toolbar.store import get_store
89
from debug_toolbar.toolbar import DebugToolbar
910

@@ -14,7 +15,7 @@
1415

1516

1617
class HistoryPanelTestCase(BaseTestCase):
17-
panel_id = "HistoryPanel"
18+
panel_id = HistoryPanel.panel_id
1819

1920
def test_disabled(self):
2021
config = {"DISABLE_PANELS": {"debug_toolbar.panels.history.HistoryPanel"}}
@@ -93,7 +94,7 @@ def test_history_panel_integration_content(self):
9394
request_ids = list(store.request_ids())
9495
self.assertEqual(len(request_ids), 1)
9596
toolbar = DebugToolbar.fetch(request_ids[0])
96-
content = toolbar.get_panel_by_id("HistoryPanel").content
97+
content = toolbar.get_panel_by_id(HistoryPanel.panel_id).content
9798
self.assertIn("bar", content)
9899
self.assertIn('name="exclude_history" value="True"', content)
99100

@@ -131,7 +132,7 @@ def test_history_sidebar_includes_history(self):
131132
"""Validate the history sidebar view."""
132133
self.client.get("/json_view/")
133134
panel_keys = copy.copy(self.PANEL_KEYS)
134-
panel_keys.add("HistoryPanel")
135+
panel_keys.add(HistoryPanel.panel_id)
135136
request_id = list(get_store().request_ids())[0]
136137
data = {"request_id": request_id}
137138
response = self.client.get(reverse("djdt:history_sidebar"), data=data)

tests/panels/test_profiling.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from django.http import HttpResponse
44
from django.test.utils import override_settings
55

6+
from debug_toolbar.panels.profiling import ProfilingPanel
7+
68
from ..base import BaseTestCase, IntegrationTestCase
79
from ..views import listcomp_view, regular_view
810

@@ -11,7 +13,7 @@
1113
DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.profiling.ProfilingPanel"]
1214
)
1315
class ProfilingPanelTestCase(BaseTestCase):
14-
panel_id = "ProfilingPanel"
16+
panel_id = ProfilingPanel.panel_id
1517

1618
def test_regular_view(self):
1719
self._get_response = lambda request: regular_view(request, "profiling")

tests/panels/test_redirects.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from django.conf import settings
44
from django.http import HttpResponse
55

6+
from debug_toolbar.panels.redirects import RedirectsPanel
7+
68
from ..base import BaseTestCase
79

810

911
class RedirectsPanelTestCase(BaseTestCase):
10-
panel_id = "RedirectsPanel"
12+
panel_id = RedirectsPanel.panel_id
1113

1214
def test_regular_response(self):
1315
not_redirect = HttpResponse()

0 commit comments

Comments
 (0)