From f46f9d131f792526ffe5ca96d05ae15e9bc73fb0 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Sun, 27 Oct 2024 08:29:45 +0100 Subject: [PATCH 1/3] StaticFile.__str__ should always return a str, not e.g. a Path() instance Refs #2002 --- debug_toolbar/panels/staticfiles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/panels/staticfiles.py b/debug_toolbar/panels/staticfiles.py index b0997404c..cfe9f5ab1 100644 --- a/debug_toolbar/panels/staticfiles.py +++ b/debug_toolbar/panels/staticfiles.py @@ -21,7 +21,7 @@ def __init__(self, path): self.path = path def __str__(self): - return self.path + return str(self.path) def real_path(self): return finders.find(self.path) From 9bcf85a1857dd398bf2178fbf74cb2c69686a49a Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Sun, 27 Oct 2024 08:31:04 +0100 Subject: [PATCH 2/3] Add a changelog entry --- docs/changes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changes.rst b/docs/changes.rst index 4bb54144b..d04040c83 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -6,6 +6,7 @@ Pending * Added Python 3.13 to the CI matrix. * Removed support for Python 3.8 as it has reached end of life. +* Fixed a crash which occurred when using non-``str`` static file values. 5.0.0-alpha (2024-09-01) ------------------------ From be42eebf754959b84915d6991e0a606675283118 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Sun, 27 Oct 2024 09:51:59 +0100 Subject: [PATCH 3/3] Add a test for Path() static files --- debug_toolbar/panels/staticfiles.py | 12 +++++++----- tests/panels/test_staticfiles.py | 20 +++++++++++++++++++- tests/templates/staticfiles/path.html | 1 + 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 tests/templates/staticfiles/path.html diff --git a/debug_toolbar/panels/staticfiles.py b/debug_toolbar/panels/staticfiles.py index cfe9f5ab1..3dd29e979 100644 --- a/debug_toolbar/panels/staticfiles.py +++ b/debug_toolbar/panels/staticfiles.py @@ -17,17 +17,18 @@ class StaticFile: Representing the different properties of a static file. """ - def __init__(self, path): + def __init__(self, *, path, url): self.path = path + self._url = url def __str__(self): - return str(self.path) + return self.path def real_path(self): return finders.find(self.path) def url(self): - return storage.staticfiles_storage.url(self.path) + return self._url # This will record and map the StaticFile instances with its associated @@ -58,6 +59,7 @@ def _setup(self): class DebugStaticFilesStorage(configured_storage_cls): def url(self, path): + url = super().url(path) with contextlib.suppress(LookupError): # For LookupError: # The ContextVar wasn't set yet. Since the toolbar wasn't properly @@ -66,10 +68,10 @@ def url(self, path): request_id = request_id_context_var.get() record_static_file_signal.send( sender=self, - staticfile=StaticFile(path), + staticfile=StaticFile(path=str(path), url=url), request_id=request_id, ) - return super().url(path) + return url self._wrapped = DebugStaticFilesStorage() diff --git a/tests/panels/test_staticfiles.py b/tests/panels/test_staticfiles.py index 3caedc4eb..334b0b6a3 100644 --- a/tests/panels/test_staticfiles.py +++ b/tests/panels/test_staticfiles.py @@ -1,7 +1,9 @@ +from pathlib import Path + from django.conf import settings from django.contrib.staticfiles import finders from django.shortcuts import render -from django.test import AsyncRequestFactory +from django.test import AsyncRequestFactory, RequestFactory from ..base import BaseTestCase @@ -58,3 +60,19 @@ def test_insert_content(self): "django.contrib.staticfiles.finders.AppDirectoriesFinder", content ) self.assertValidHTML(content) + + def test_path(self): + def get_response(request): + # template contains one static file + return render( + request, + "staticfiles/path.html", + {"path": Path("additional_static/base.css")}, + ) + + self._get_response = get_response + request = RequestFactory().get("/") + response = self.panel.process_request(request) + self.panel.generate_stats(self.request, response) + self.assertEqual(self.panel.num_used, 1) + self.assertIn('"/static/additional_static/base.css"', self.panel.content) diff --git a/tests/templates/staticfiles/path.html b/tests/templates/staticfiles/path.html new file mode 100644 index 000000000..bf3781c3b --- /dev/null +++ b/tests/templates/staticfiles/path.html @@ -0,0 +1 @@ +{% load static %}{% static path %}