diff --git a/debug_toolbar/panels/staticfiles.py b/debug_toolbar/panels/staticfiles.py index 544caab81..fb2cdd093 100644 --- a/debug_toolbar/panels/staticfiles.py +++ b/debug_toolbar/panels/staticfiles.py @@ -1,6 +1,7 @@ import contextlib import uuid from contextvars import ContextVar +from dataclasses import dataclass from os.path import join, normpath from django.contrib.staticfiles import finders, storage @@ -10,14 +11,14 @@ from debug_toolbar import panels +@dataclass(eq=True, frozen=True) class StaticFile: """ Representing the different properties of a static file. """ - def __init__(self, *, path, url): - self.path = path - self._url = url + path: str + url: str def __str__(self): return self.path @@ -72,7 +73,7 @@ def title(self): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_found = 0 - self.used_paths = [] + self.used_paths = set() self.request_id = str(uuid.uuid4()) @classmethod @@ -88,7 +89,7 @@ def _store_static_files_signal_handler(self, sender, staticfile, **kwargs): # concurrent connections and we want to avoid storing of same # staticfile from other connections as well. if request_id_context_var.get() == self.request_id: - self.used_paths.append(staticfile) + self.used_paths.add(staticfile) def enable_instrumentation(self): self.ctx_token = request_id_context_var.set(self.request_id) @@ -112,7 +113,7 @@ def generate_stats(self, request, response): { "num_found": self.num_found, "num_used": len(self.used_paths), - "staticfiles": self.used_paths, + "staticfiles": sorted(self.used_paths), "staticfiles_apps": self.get_staticfiles_apps(), "staticfiles_dirs": self.get_staticfiles_dirs(), "staticfiles_finders": self.get_staticfiles_finders(), diff --git a/docs/changes.rst b/docs/changes.rst index 271153c6f..223c06db5 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -30,6 +30,7 @@ Pending * Extend example app to contain an async version. * Added ``debug_toolbar.store.DatabaseStore`` for persistent debug data storage. +* Deduplicated static files in the staticfiles panel. 5.2.0 (2025-04-29) ------------------ diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index 79b05cb06..472ad15bd 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -17,6 +17,7 @@ biome checkbox contrib csp +deduplicated dicts django fallbacks diff --git a/tests/templates/staticfiles/path.html b/tests/templates/staticfiles/path.html index bf3781c3b..9e9ff1b88 100644 --- a/tests/templates/staticfiles/path.html +++ b/tests/templates/staticfiles/path.html @@ -1 +1,3 @@ -{% load static %}{% static path %} +{% load static %} +{# A single file used twice #} +{% static path %}{% static path %}