Skip to content
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

Refs #2068: Do not reinstantiate staticfiles storage classes #2069

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
61 changes: 18 additions & 43 deletions debug_toolbar/panels/staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
from contextvars import ContextVar
from os.path import join, normpath

from django.conf import settings
from django.contrib.staticfiles import finders, storage
from django.dispatch import Signal
from django.utils.functional import LazyObject
from django.utils.translation import gettext_lazy as _, ngettext

from debug_toolbar import panels
Expand Down Expand Up @@ -37,46 +35,21 @@ def url(self):
record_static_file_signal = Signal()


class DebugConfiguredStorage(LazyObject):
"""
A staticfiles storage class to be used for collecting which paths
are resolved by using the {% static %} template tag (which uses the
`url` method).
"""

def _setup(self):
try:
# From Django 4.2 use django.core.files.storage.storages in favor
# of the deprecated django.core.files.storage.get_storage_class
from django.core.files.storage import storages

configured_storage_cls = storages["staticfiles"].__class__
except ImportError:
# Backwards compatibility for Django versions prior to 4.2
from django.core.files.storage import get_storage_class

configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)

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
# configured to handle this request, we don't need to capture
# the static file.
request_id = request_id_context_var.get()
record_static_file_signal.send(
sender=self,
staticfile=StaticFile(path=str(path), url=url),
request_id=request_id,
)
return url

self._wrapped = DebugStaticFilesStorage()


_original_storage = storage.staticfiles_storage
class URLMixin:
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
# configured to handle this request, we don't need to capture
# the static file.
request_id = request_id_context_var.get()
record_static_file_signal.send(
sender=self,
staticfile=StaticFile(path=str(path), url=url),
request_id=request_id,
)
return url


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

@classmethod
def ready(cls):
storage.staticfiles_storage = DebugConfiguredStorage()
cls = storage.staticfiles_storage.__class__
if URLMixin not in cls.mro():
cls.__bases__ = (URLMixin, *cls.__bases__)

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