Skip to content

Commit 756f002

Browse files
Refactor type hints and imports in history panel and views for improved clarity
1 parent d7b125b commit 756f002

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

debug_toolbar/panels/history/panel.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import contextlib
22
import json
33

4-
from django.http.request import RawPostDataException
4+
from django.http import HttpResponse, QueryDict
5+
from django.http.request import HttpRequest, RawPostDataException
56
from django.template.loader import render_to_string
67
from django.templatetags.static import static
7-
from django.urls import path
8+
from django.urls import URLPattern, path
89
from django.utils import timezone
910
from django.utils.translation import gettext_lazy as _
1011

@@ -21,42 +22,42 @@ class HistoryPanel(Panel):
2122
nav_title = _("History")
2223
template = "debug_toolbar/panels/history.html"
2324

24-
def get_headers(self, request):
25-
headers = super().get_headers(request)
25+
def get_headers(self, request: HttpRequest) -> dict:
26+
headers: dict = super().get_headers(request)
2627
observe_request = self.toolbar.get_observe_request()
2728
request_id = self.toolbar.request_id
2829
if request_id and observe_request(request):
2930
headers["djdt-request-id"] = request_id
3031
return headers
3132

3233
@property
33-
def enabled(self):
34+
def enabled(self) -> bool:
3435
# Do not show the history panel if the panels are rendered on request
3536
# rather than loaded via ajax.
3637
return super().enabled and not self.toolbar.should_render_panels()
3738

3839
@property
39-
def is_historical(self):
40+
def is_historical(self) -> bool:
4041
"""The HistoryPanel should not be included in the historical panels."""
4142
return False
4243

4344
@classmethod
44-
def get_urls(cls):
45+
def get_urls(cls) -> list[URLPattern]:
4546
return [
4647
path("history_sidebar/", views.history_sidebar, name="history_sidebar"),
4748
path("history_refresh/", views.history_refresh, name="history_refresh"),
4849
]
4950

5051
@property
51-
def nav_subtitle(self):
52+
def nav_subtitle(self) -> str:
5253
return self.get_stats().get("request_url", "")
5354

54-
def generate_stats(self, request, response):
55+
def generate_stats(self, request: HttpRequest, response: HttpResponse) -> None:
5556
try:
5657
if request.method == "GET":
57-
data = request.GET.copy()
58+
data: QueryDict = request.GET.copy()
5859
else:
59-
data = request.POST.copy()
60+
data: QueryDict = request.POST.copy()
6061
# GraphQL tends to not be populated in POST. If the request seems
6162
# empty, check if it's a JSON request.
6263
if (
@@ -82,12 +83,12 @@ def generate_stats(self, request, response):
8283
)
8384

8485
@property
85-
def content(self):
86+
def content(self) -> str:
8687
"""Content of the panel when it's displayed in full screen.
8788
8889
Fetch every store for the toolbar and include it in the template.
8990
"""
90-
toolbar_history = {}
91+
toolbar_history: dict[str, dict] = {}
9192
for request_id in reversed(self.toolbar.store.request_ids()):
9293
toolbar_history[request_id] = {
9394
"history_stats": self.toolbar.store.panel(
@@ -113,7 +114,7 @@ def content(self):
113114
)
114115

115116
@property
116-
def scripts(self):
117-
scripts = super().scripts
117+
def scripts(self) -> list[str]:
118+
scripts: list[str] = super().scripts
118119
scripts.append(static("debug_toolbar/js/history.js"))
119120
return scripts

debug_toolbar/panels/history/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.http import HttpResponseBadRequest, JsonResponse
1+
from django.http import HttpRequest, HttpResponseBadRequest, JsonResponse
22
from django.template.loader import render_to_string
33

44
from debug_toolbar._compat import login_not_required
@@ -11,15 +11,15 @@
1111
@login_not_required
1212
@require_show_toolbar
1313
@render_with_toolbar_language
14-
def history_sidebar(request):
14+
def history_sidebar(request: HttpRequest) -> HttpResponseBadRequest | JsonResponse:
1515
"""Returns the selected debug toolbar history snapshot."""
1616
form = HistoryStoreForm(request.GET)
1717

1818
if form.is_valid():
19-
request_id = form.cleaned_data["request_id"]
20-
toolbar = DebugToolbar.fetch(request_id)
19+
request_id: str = form.cleaned_data["request_id"]
20+
toolbar: DebugToolbar | None = DebugToolbar.fetch(request_id)
2121
exclude_history = form.cleaned_data["exclude_history"]
22-
context = {}
22+
context: dict[str, dict[str, str]] = {}
2323
if toolbar is None:
2424
# When the request_id has been popped already due to
2525
# RESULTS_CACHE_SIZE
@@ -43,12 +43,12 @@ def history_sidebar(request):
4343
@login_not_required
4444
@require_show_toolbar
4545
@render_with_toolbar_language
46-
def history_refresh(request):
46+
def history_refresh(request: HttpRequest) -> HttpResponseBadRequest | JsonResponse:
4747
"""Returns the refreshed list of table rows for the History Panel."""
4848
form = HistoryStoreForm(request.GET)
4949

5050
if form.is_valid():
51-
requests = []
51+
requests: list[dict[str, str]] = []
5252
# Convert to list to handle mutations happening in parallel
5353
for request_id in get_store().request_ids():
5454
toolbar = DebugToolbar.fetch(request_id)

debug_toolbar/views.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
from django.http import JsonResponse
1+
from django.http import HttpRequest, JsonResponse
22
from django.utils.html import escape
33
from django.utils.translation import gettext as _
44

55
from debug_toolbar._compat import login_not_required
66
from debug_toolbar.decorators import render_with_toolbar_language, require_show_toolbar
7-
from debug_toolbar.toolbar import DebugToolbar
7+
from debug_toolbar.panels import Panel
8+
from debug_toolbar.toolbar import DebugToolbar, StoredDebugToolbar
89

910

1011
@login_not_required
1112
@require_show_toolbar
1213
@render_with_toolbar_language
13-
def render_panel(request):
14+
def render_panel(request: HttpRequest) -> JsonResponse:
1415
"""Render the contents of a panel"""
15-
toolbar = DebugToolbar.fetch(request.GET["request_id"], request.GET["panel_id"])
16+
toolbar: StoredDebugToolbar | None = DebugToolbar.fetch(
17+
request.GET["request_id"], request.GET["panel_id"]
18+
)
1619
if toolbar is None:
1720
content = _(
1821
"Data for this panel isn't available anymore. "
@@ -21,7 +24,7 @@ def render_panel(request):
2124
content = f"<p>{escape(content)}</p>"
2225
scripts = []
2326
else:
24-
panel = toolbar.get_panel_by_id(request.GET["panel_id"])
27+
panel: Panel = toolbar.get_panel_by_id(request.GET["panel_id"])
2528
content = panel.content
2629
scripts = panel.scripts
2730
return JsonResponse({"content": content, "scripts": scripts})

0 commit comments

Comments
 (0)