Skip to content

Commit 5e98abd

Browse files
committed
Refs #2068: Do not reinstantiate staticfiles storage classes
1 parent fdd636c commit 5e98abd

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

debug_toolbar/panels/staticfiles.py

+18-43
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
from contextvars import ContextVar
44
from os.path import join, normpath
55

6-
from django.conf import settings
76
from django.contrib.staticfiles import finders, storage
87
from django.dispatch import Signal
9-
from django.utils.functional import LazyObject
108
from django.utils.translation import gettext_lazy as _, ngettext
119

1210
from debug_toolbar import panels
@@ -37,46 +35,21 @@ def url(self):
3735
record_static_file_signal = Signal()
3836

3937

40-
class DebugConfiguredStorage(LazyObject):
41-
"""
42-
A staticfiles storage class to be used for collecting which paths
43-
are resolved by using the {% static %} template tag (which uses the
44-
`url` method).
45-
"""
46-
47-
def _setup(self):
48-
try:
49-
# From Django 4.2 use django.core.files.storage.storages in favor
50-
# of the deprecated django.core.files.storage.get_storage_class
51-
from django.core.files.storage import storages
52-
53-
configured_storage_cls = storages["staticfiles"].__class__
54-
except ImportError:
55-
# Backwards compatibility for Django versions prior to 4.2
56-
from django.core.files.storage import get_storage_class
57-
58-
configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)
59-
60-
class DebugStaticFilesStorage(configured_storage_cls):
61-
def url(self, path):
62-
url = super().url(path)
63-
with contextlib.suppress(LookupError):
64-
# For LookupError:
65-
# The ContextVar wasn't set yet. Since the toolbar wasn't properly
66-
# configured to handle this request, we don't need to capture
67-
# the static file.
68-
request_id = request_id_context_var.get()
69-
record_static_file_signal.send(
70-
sender=self,
71-
staticfile=StaticFile(path=str(path), url=url),
72-
request_id=request_id,
73-
)
74-
return url
75-
76-
self._wrapped = DebugStaticFilesStorage()
77-
78-
79-
_original_storage = storage.staticfiles_storage
38+
class URLMixin:
39+
def url(self, path):
40+
url = super().url(path)
41+
with contextlib.suppress(LookupError):
42+
# For LookupError:
43+
# The ContextVar wasn't set yet. Since the toolbar wasn't properly
44+
# configured to handle this request, we don't need to capture
45+
# the static file.
46+
request_id = request_id_context_var.get()
47+
record_static_file_signal.send(
48+
sender=self,
49+
staticfile=StaticFile(path=str(path), url=url),
50+
request_id=request_id,
51+
)
52+
return url
8053

8154

8255
class StaticFilesPanel(panels.Panel):
@@ -103,7 +76,9 @@ def __init__(self, *args, **kwargs):
10376

10477
@classmethod
10578
def ready(cls):
106-
storage.staticfiles_storage = DebugConfiguredStorage()
79+
cls = storage.staticfiles_storage.__class__
80+
if URLMixin not in cls.mro():
81+
cls.__bases__ = (URLMixin, *cls.__bases__)
10782

10883
def _store_static_files_signal_handler(self, sender, staticfile, **kwargs):
10984
# Only record the static file if the request_id matches the one

0 commit comments

Comments
 (0)