From 9e9d26c8aa44181e616edac616c27a8a1a24f150 Mon Sep 17 00:00:00 2001 From: Amir Abedi Date: Sat, 21 Jan 2023 14:34:42 +0330 Subject: [PATCH 1/2] hotfix for DEBUG=True and USE_SRI=True and ManifestStaticFilesStorage for STATICFILES_STORAGE --- sri/utils.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/sri/utils.py b/sri/utils.py index 4a8179d..7dbde9e 100644 --- a/sri/utils.py +++ b/sri/utils.py @@ -1,28 +1,24 @@ -import logging import os from pathlib import Path +from django.conf import settings from django.contrib.staticfiles.finders import find as find_static_file from django.contrib.staticfiles.storage import staticfiles_storage from django.core.cache import DEFAULT_CACHE_ALIAS, caches from django.core.cache.backends.base import InvalidCacheBackendError -logger = logging.getLogger(__name__) - def get_static_path(path: str) -> Path: """ Resolves a path commonly passed to `{% static %}` into a filesystem path """ - if hasattr(staticfiles_storage, "stored_name"): + if (not settings.DEBUG) and hasattr(staticfiles_storage, "stored_name"): path = staticfiles_storage.stored_name(path) + collected_file_path = staticfiles_storage.path(path) + if os.path.exists(collected_file_path): + return Path(collected_file_path) - collected_file_path = staticfiles_storage.path(path) - if os.path.exists(collected_file_path): - return Path(collected_file_path) - - logger.debug("File not found in staticfiles_storage - checking source files") source_static_file_path = find_static_file(path) if source_static_file_path is not None: return Path(source_static_file_path) From 74c2cdc451290f1b24c2e188558d1ad5863d5e99 Mon Sep 17 00:00:00 2001 From: Amir Abedi Date: Sat, 21 Jan 2023 15:14:59 +0330 Subject: [PATCH 2/2] hotfix for DEBUG=True and USE_SRI=True and ManifestStaticFilesStorage --- sri/utils.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sri/utils.py b/sri/utils.py index 7dbde9e..f6029c8 100644 --- a/sri/utils.py +++ b/sri/utils.py @@ -1,3 +1,4 @@ +import logging import os from pathlib import Path @@ -7,6 +8,8 @@ from django.core.cache import DEFAULT_CACHE_ALIAS, caches from django.core.cache.backends.base import InvalidCacheBackendError +logger = logging.getLogger(__name__) + def get_static_path(path: str) -> Path: """ @@ -15,10 +18,12 @@ def get_static_path(path: str) -> Path: if (not settings.DEBUG) and hasattr(staticfiles_storage, "stored_name"): path = staticfiles_storage.stored_name(path) - collected_file_path = staticfiles_storage.path(path) - if os.path.exists(collected_file_path): - return Path(collected_file_path) + collected_file_path = staticfiles_storage.path(path) + if os.path.exists(collected_file_path): + return Path(collected_file_path) + + logger.debug("File not found in staticfiles_storage - checking source files") source_static_file_path = find_static_file(path) if source_static_file_path is not None: return Path(source_static_file_path)