Skip to content

Commit cda2553

Browse files
authored
Fixes #2160 -- Corrected links to static files (#2162)
The JSON serialization used by the new serializable mechanism compressed the `StaticFile` dataclass down to a single string without an `.url` attribute. Change the code to use a tuple instead to avoid this problem.
1 parent 569528b commit cda2553

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

debug_toolbar/panels/staticfiles.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
import uuid
33
from contextvars import ContextVar
4-
from dataclasses import dataclass
54
from os.path import join, normpath
65

76
from django.contrib.staticfiles import finders, storage
@@ -10,26 +9,6 @@
109

1110
from debug_toolbar import panels
1211

13-
14-
@dataclass(eq=True, frozen=True, order=True)
15-
class StaticFile:
16-
"""
17-
Representing the different properties of a static file.
18-
"""
19-
20-
path: str
21-
url: str
22-
23-
def __str__(self):
24-
return self.path
25-
26-
def real_path(self):
27-
return finders.find(self.path)
28-
29-
def url(self):
30-
return self._url
31-
32-
3312
# This will record and map the StaticFile instances with its associated
3413
# request across threads and async concurrent requests state.
3514
request_id_context_var = ContextVar("djdt_request_id_store")
@@ -47,7 +26,7 @@ def url(self, path):
4726
request_id = request_id_context_var.get()
4827
record_static_file_signal.send(
4928
sender=self,
50-
staticfile=StaticFile(path=str(path), url=url),
29+
staticfile=(str(path), url, finders.find(str(path))),
5130
request_id=request_id,
5231
)
5332
return url

debug_toolbar/templates/debug_toolbar/panels/staticfiles.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ <h4>{% blocktranslate count apps_count=staticfiles_apps|length %}Static file app
2525
<h4>{% blocktranslate count staticfiles_count=staticfiles|length %}Static file{% plural %}Static files{% endblocktranslate %}</h4>
2626
{% if staticfiles %}
2727
<dl>
28-
{% for staticfile in staticfiles %}
29-
<dt><strong><a class="toggleTemplate" href="{{ staticfile.url }}">{{ staticfile }}</a></strong></dt>
30-
<dd><samp>{{ staticfile.real_path }}</samp></dd>
28+
{% for path, url, real_path in staticfiles %}
29+
<dt><strong><a class="toggleTemplate" href="{{ url }}" target="_blank" rel="noopener">{{ path }}</a></strong></dt>
30+
<dd><samp>{{ real_path }}</samp></dd>
3131
{% endfor %}
3232
</dl>
3333
{% else %}

tests/panels/test_staticfiles.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def get_response(request):
7171
{
7272
"paths": [
7373
Path("additional_static/base.css"),
74-
Path("additional_static/base.css"),
75-
Path("additional_static/base2.css"),
74+
"additional_static/base.css",
75+
"additional_static/base2.css",
7676
]
7777
},
7878
)
@@ -82,8 +82,12 @@ def get_response(request):
8282
response = self.panel.process_request(request)
8383
self.panel.generate_stats(self.request, response)
8484
self.assertEqual(self.panel.get_stats()["num_used"], 2)
85-
self.assertIn('"/static/additional_static/base.css"', self.panel.content, 1)
86-
self.assertIn('"/static/additional_static/base2.css"', self.panel.content, 1)
85+
self.assertIn(
86+
'href="/static/additional_static/base.css"', self.panel.content, 1
87+
)
88+
self.assertIn(
89+
'href="/static/additional_static/base2.css"', self.panel.content, 1
90+
)
8791

8892
def test_storage_state_preservation(self):
8993
"""Ensure the URLMixin doesn't affect storage state"""
@@ -110,7 +114,7 @@ def test_context_variable_lifecycle(self):
110114
url = storage.staticfiles_storage.url("test.css")
111115
self.assertTrue(url.startswith("/static/"))
112116
# Verify file was tracked
113-
self.assertIn("test.css", [f.path for f in self.panel.used_paths])
117+
self.assertIn("test.css", [f[0] for f in self.panel.used_paths])
114118
finally:
115119
request_id_context_var.reset(token)
116120

0 commit comments

Comments
 (0)