Skip to content

Commit fd786be

Browse files
committed
Work in preperation of an autorefresh
adding view that will return the toolbar for a given store id Adding header containing toolbar url Refactoring history javascript to allow switching state by store id
1 parent 3ea0900 commit fd786be

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

debug_toolbar/panels/history/panel.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.http.request import RawPostDataException
55
from django.template.loader import render_to_string
66
from django.templatetags.static import static
7-
from django.urls import path
7+
from django.urls import path, reverse
88
from django.utils import timezone
99
from django.utils.translation import gettext_lazy as _
1010

@@ -21,6 +21,15 @@ class HistoryPanel(Panel):
2121
nav_title = _("History")
2222
template = "debug_toolbar/panels/history.html"
2323

24+
def process_request(self, request):
25+
response = super().process_request(request)
26+
if not self.toolbar.should_render_panels():
27+
self.toolbar.store()
28+
response["DJ-TOOLBAR-URL"] = (
29+
reverse("djdt:render_base") + f"?store_id={self.toolbar.store_id}"
30+
)
31+
return response
32+
2433
@property
2534
def enabled(self):
2635
# Do not show the history panel if the panels are rendered on request

debug_toolbar/static/debug_toolbar/js/history.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ function difference(setA, setB) {
1010
return _difference;
1111
}
1212

13+
function replaceToolbarState(newStoreId, data) {
14+
djDebug.setAttribute("data-store-id", newStoreId);
15+
// Check if response is empty, it could be due to an expired store_id.
16+
if (Object.keys(data).length === 0) {
17+
const container = document.getElementById("djdtHistoryRequests");
18+
container.querySelector(
19+
'button[data-store-id="' + newStoreId + '"]'
20+
).innerHTML = "Switch [EXPIRED]";
21+
} else {
22+
Object.keys(data).forEach(function (panelId) {
23+
const panel = document.getElementById(panelId);
24+
if (panel) {
25+
panel.outerHTML = data[panelId].content;
26+
document.getElementById("djdt-" + panelId).outerHTML =
27+
data[panelId].button;
28+
}
29+
});
30+
}
31+
}
32+
1333
function switchHistory(newStoreId) {
1434
const formTarget = djDebug.querySelector(
1535
".switchHistory[data-store-id='" + newStoreId + "']"
@@ -23,23 +43,7 @@ function switchHistory(newStoreId) {
2343
formTarget.closest("tr").classList.add("djdt-highlighted");
2444

2545
ajaxForm(formTarget).then(function (data) {
26-
djDebug.setAttribute("data-store-id", newStoreId);
27-
// Check if response is empty, it could be due to an expired store_id.
28-
if (Object.keys(data).length === 0) {
29-
const container = document.getElementById("djdtHistoryRequests");
30-
container.querySelector(
31-
'button[data-store-id="' + newStoreId + '"]'
32-
).innerHTML = "Switch [EXPIRED]";
33-
} else {
34-
Object.keys(data).forEach(function (panelId) {
35-
const panel = document.getElementById(panelId);
36-
if (panel) {
37-
panel.outerHTML = data[panelId].content;
38-
document.getElementById("djdt-" + panelId).outerHTML =
39-
data[panelId].button;
40-
}
41-
});
42-
}
46+
replaceToolbarState(newStoreId, data);
4347
});
4448
}
4549

debug_toolbar/toolbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def get_urls(cls):
132132
# Load URLs in a temporary variable for thread safety.
133133
# Global URLs
134134
urlpatterns = [
135-
path("render_panel/", views.render_panel, name="render_panel")
135+
path("render_panel/", views.render_panel, name="render_panel"),
136+
path("render_base/", views.render_base, name="render_base"),
136137
]
137138
# Per-panel URLs
138139
for panel_class in cls.get_panel_classes():

debug_toolbar/views.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.http import JsonResponse
1+
from django.http import HttpResponse, JsonResponse
22
from django.utils.html import escape
33
from django.utils.translation import gettext as _
44

@@ -22,3 +22,17 @@ def render_panel(request):
2222
content = panel.content
2323
scripts = panel.scripts
2424
return JsonResponse({"content": content, "scripts": scripts})
25+
26+
27+
@require_show_toolbar
28+
def render_base(request):
29+
"""Render the contents of a panel"""
30+
toolbar = DebugToolbar.fetch(request.GET["store_id"])
31+
if toolbar is None:
32+
content = _(
33+
"Data for this toolbar isn't available anymore. "
34+
"Please reload the page and retry."
35+
)
36+
content = "<p>%s</p>" % escape(content)
37+
content = toolbar.render_toolbar()
38+
return HttpResponse(content)

0 commit comments

Comments
 (0)