Skip to content

The static files panel shouldn't choke on unexpected data types #2021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions debug_toolbar/panels/staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ 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 self.path
Expand All @@ -27,7 +28,7 @@ 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
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Pending
* Added Python 3.13 to the CI matrix.
* Removed support for Python 3.8 as it has reached end of life.
* Converted to Django Commons PyPI release process.
* Fixed a crash which occurred when using non-``str`` static file values.

5.0.0-alpha (2024-09-01)
------------------------
Expand Down
20 changes: 19 additions & 1 deletion tests/panels/test_staticfiles.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/templates/staticfiles/path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% load static %}{% static path %}