Skip to content

Commit

Permalink
Make Panel.panel_id a classmember.
Browse files Browse the repository at this point in the history
This avoids needing to create an instance of a panel to get its
panel ID.
  • Loading branch information
tim-schilling committed Aug 21, 2023
1 parent e2f695b commit 14a5e0c
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 61 deletions.
7 changes: 4 additions & 3 deletions debug_toolbar/panels/__init__.py
@@ -1,4 +1,5 @@
from django.template.loader import render_to_string
from django.utils.functional import classproperty

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

# Private panel properties

@property
def panel_id(self):
return self.__class__.__name__
@classproperty
def panel_id(cls):
return cls.__name__

@property
def enabled(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/panels/history/__init__.py
@@ -1,3 +1,3 @@
from debug_toolbar.panels.history.panel import HistoryPanel

__all__ = ["HistoryPanel"]
__all__ = [HistoryPanel.panel_id]
2 changes: 1 addition & 1 deletion debug_toolbar/panels/sql/__init__.py
@@ -1,3 +1,3 @@
from debug_toolbar.panels.sql.panel import SQLPanel

__all__ = ["SQLPanel"]
__all__ = [SQLPanel.panel_id]
6 changes: 4 additions & 2 deletions debug_toolbar/panels/sql/forms.py
Expand Up @@ -46,14 +46,16 @@ def clean_alias(self):
return value

def clean(self):
from debug_toolbar.panels.sql import SQLPanel

cleaned_data = super().clean()
toolbar = DebugToolbar.fetch(
self.cleaned_data["request_id"], panel_id="SQLPanel"
self.cleaned_data["request_id"], panel_id=SQLPanel.panel_id
)
if toolbar is None:
raise ValidationError(_("Data for this panel isn't available anymore."))

panel = toolbar.get_panel_by_id("SQLPanel")
panel = toolbar.get_panel_by_id(SQLPanel.panel_id)
# Find the query for this form submission
query = None
for q in panel.get_stats()["queries"]:
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/panels/templates/__init__.py
@@ -1,3 +1,3 @@
from debug_toolbar.panels.templates.panel import TemplatesPanel

__all__ = ["TemplatesPanel"]
__all__ = [TemplatesPanel.panel_id]
2 changes: 1 addition & 1 deletion docs/changes.rst
Expand Up @@ -22,7 +22,7 @@ Serializable (don't include in main)
this id and avoid passing SQL to be executed.
* Move the formatting logic of SQL queries to just before rendering in
``SQLPanel.content``.

* Make ``Panel.panel_id`` a class member.

Pending
-------
Expand Down
4 changes: 3 additions & 1 deletion tests/panels/test_cache.py
@@ -1,10 +1,12 @@
from django.core import cache

from debug_toolbar.panels.cache import CachePanel

from ..base import BaseTestCase


class CachePanelTestCase(BaseTestCase):
panel_id = "CachePanel"
panel_id = CachePanel.panel_id

def test_recording(self):
self.assertEqual(len(self.panel.calls), 0)
Expand Down
7 changes: 4 additions & 3 deletions tests/panels/test_history.py
Expand Up @@ -4,6 +4,7 @@
from django.test import RequestFactory, override_settings
from django.urls import resolve, reverse

from debug_toolbar.panels.history import HistoryPanel
from debug_toolbar.store import get_store
from debug_toolbar.toolbar import DebugToolbar

Expand All @@ -14,7 +15,7 @@


class HistoryPanelTestCase(BaseTestCase):
panel_id = "HistoryPanel"
panel_id = HistoryPanel.panel_id

def test_disabled(self):
config = {"DISABLE_PANELS": {"debug_toolbar.panels.history.HistoryPanel"}}
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_history_panel_integration_content(self):
request_ids = list(store.request_ids())
self.assertEqual(len(request_ids), 1)
toolbar = DebugToolbar.fetch(request_ids[0])
content = toolbar.get_panel_by_id("HistoryPanel").content
content = toolbar.get_panel_by_id(HistoryPanel.panel_id).content
self.assertIn("bar", content)
self.assertIn('name="exclude_history" value="True"', content)

Expand Down Expand Up @@ -131,7 +132,7 @@ def test_history_sidebar_includes_history(self):
"""Validate the history sidebar view."""
self.client.get("/json_view/")
panel_keys = copy.copy(self.PANEL_KEYS)
panel_keys.add("HistoryPanel")
panel_keys.add(HistoryPanel.panel_id)
request_id = list(get_store().request_ids())[0]
data = {"request_id": request_id}
response = self.client.get(reverse("djdt:history_sidebar"), data=data)
Expand Down
4 changes: 3 additions & 1 deletion tests/panels/test_profiling.py
Expand Up @@ -3,6 +3,8 @@
from django.http import HttpResponse
from django.test.utils import override_settings

from debug_toolbar.panels.profiling import ProfilingPanel

from ..base import BaseTestCase, IntegrationTestCase
from ..views import listcomp_view, regular_view

Expand All @@ -11,7 +13,7 @@
DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.profiling.ProfilingPanel"]
)
class ProfilingPanelTestCase(BaseTestCase):
panel_id = "ProfilingPanel"
panel_id = ProfilingPanel.panel_id

def test_regular_view(self):
self._get_response = lambda request: regular_view(request, "profiling")
Expand Down
4 changes: 3 additions & 1 deletion tests/panels/test_redirects.py
Expand Up @@ -3,11 +3,13 @@
from django.conf import settings
from django.http import HttpResponse

from debug_toolbar.panels.redirects import RedirectsPanel

from ..base import BaseTestCase


class RedirectsPanelTestCase(BaseTestCase):
panel_id = "RedirectsPanel"
panel_id = RedirectsPanel.panel_id

def test_regular_response(self):
not_redirect = HttpResponse()
Expand Down
4 changes: 3 additions & 1 deletion tests/panels/test_request.py
@@ -1,10 +1,12 @@
from django.http import QueryDict

from debug_toolbar.panels.request import RequestPanel

from ..base import BaseTestCase


class RequestPanelTestCase(BaseTestCase):
panel_id = "RequestPanel"
panel_id = RequestPanel.panel_id

def test_non_ascii_session(self):
self.request.session = {"où": "où"}
Expand Down
4 changes: 2 additions & 2 deletions tests/panels/test_sql.py
Expand Up @@ -34,7 +34,7 @@ def sql_call(*, use_iterator=False):


class SQLPanelTestCase(BaseTestCase):
panel_id = "SQLPanel"
panel_id = SQLPanel.panel_id

def test_disabled(self):
config = {"DISABLE_PANELS": {"debug_toolbar.panels.sql.SQLPanel"}}
Expand Down Expand Up @@ -699,7 +699,7 @@ def test_similar_and_duplicate_grouping(self):


class SQLPanelMultiDBTestCase(BaseMultiDBTestCase):
panel_id = "SQLPanel"
panel_id = SQLPanel.panel_id

def test_aliases(self):
self.assertFalse(self.panel._queries)
Expand Down
4 changes: 3 additions & 1 deletion tests/panels/test_staticfiles.py
Expand Up @@ -6,13 +6,15 @@
from django.contrib.staticfiles import finders
from django.test.utils import override_settings

from debug_toolbar.panels.staticfiles import StaticFilesPanel

from ..base import BaseTestCase

PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static")


class StaticFilesPanelTestCase(BaseTestCase):
panel_id = "StaticFilesPanel"
panel_id = StaticFilesPanel.panel_id

def test_default_case(self):
response = self.panel.process_request(self.request)
Expand Down
7 changes: 5 additions & 2 deletions tests/panels/test_template.py
Expand Up @@ -3,17 +3,20 @@
from django.template import Context, RequestContext, Template
from django.test import override_settings

from debug_toolbar.panels.sql import SQLPanel
from debug_toolbar.panels.templates import TemplatesPanel

from ..base import BaseTestCase, IntegrationTestCase
from ..forms import TemplateReprForm
from ..models import NonAsciiRepr


class TemplatesPanelTestCase(BaseTestCase):
panel_id = "TemplatesPanel"
panel_id = TemplatesPanel.panel_id

def setUp(self):
super().setUp()
self.sql_panel = self.toolbar.get_panel_by_id("SQLPanel")
self.sql_panel = self.toolbar.get_panel_by_id(SQLPanel.panel_id)
self.sql_panel.enable_instrumentation()

def tearDown(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/panels/test_versions.py
@@ -1,5 +1,7 @@
from collections import namedtuple

from debug_toolbar.panels.versions import VersionsPanel

from ..base import BaseTestCase

version_info_t = namedtuple(
Expand All @@ -8,7 +10,7 @@


class VersionsPanelTestCase(BaseTestCase):
panel_id = "VersionsPanel"
panel_id = VersionsPanel.panel_id

def test_app_version_from_get_version_fn(self):
class FakeApp:
Expand Down

0 comments on commit 14a5e0c

Please sign in to comment.