From 7c19e40d3a821a13268f3c4fea731754f32edf10 Mon Sep 17 00:00:00 2001 From: Florian Demmer Date: Fri, 23 Feb 2024 14:46:39 +0100 Subject: [PATCH] Handle un-hashed filenames with ManifestStaticFilesStorage and DEBUG=True --- CHANGELOG.md | 1 + django_weasyprint/tests/__init__.py | 1 + django_weasyprint/tests/test_utils.py | 12 ++++++++++++ django_weasyprint/utils.py | 2 +- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ae7da..6547c00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [2.3.0] - 2024-02-23 - Make it possible to set [WeasyPrint options][2] via `WeasyTemplateResponse` (#79) +- Add support for non-hashed path when `ManifestStaticFilesStorage` is used with `DEBUG=True` [2]: https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#weasyprint.DEFAULT_OPTIONS diff --git a/django_weasyprint/tests/__init__.py b/django_weasyprint/tests/__init__.py index 765a3b7..2d086b7 100644 --- a/django_weasyprint/tests/__init__.py +++ b/django_weasyprint/tests/__init__.py @@ -15,6 +15,7 @@ settings.configure( + DEBUG=False, SECRET_KEY=get_random_secret_key(), ALLOWED_HOSTS=['testserver'], ROOT_URLCONF=__name__, diff --git a/django_weasyprint/tests/test_utils.py b/django_weasyprint/tests/test_utils.py index e73dd4f..076e0ef 100644 --- a/django_weasyprint/tests/test_utils.py +++ b/django_weasyprint/tests/test_utils.py @@ -96,6 +96,18 @@ def test_manifest_static(self, mock_fetcher, mock_find, mock_open, hashed_files) mock_open.assert_called_once_with('/www/static/css/styles.css', 'rb') self.assert_data(data, '/www/static/css/styles.css', 'text/css') + mock_find.reset_mock() + mock_open.reset_mock() + + # ManifestStaticFilesStorage.url() returns un-hashed URL with DEBUG=True, + # django_url_fetcher() is still able to find the file. + with override_settings(DEBUG=True): + data = django_url_fetcher('file:///static/css/styles.css') + mock_fetcher.assert_not_called() + mock_find.assert_called_once_with('css/styles.css') + mock_open.assert_called_once_with('/www/static/css/styles.css', 'rb') + self.assert_data(data, '/www/static/css/styles.css', 'text/css') + @override_settings(STATIC_URL='/static/', STATIC_ROOT='/www/static') @mock.patch('django_weasyprint.utils.open') @mock.patch('django_weasyprint.utils.find', return_value=None) diff --git a/django_weasyprint/utils.py b/django_weasyprint/utils.py index ca90b99..e1f78c3 100644 --- a/django_weasyprint/utils.py +++ b/django_weasyprint/utils.py @@ -51,7 +51,7 @@ def django_url_fetcher(url, *args, **kwargs): # strip the STATIC_URL prefix to get the relative filesystem path relative_path = url_path.replace(settings.STATIC_URL, '', 1) # detect hashed files storage and get path with un-hashed filename - if hasattr(staticfiles_storage, 'hashed_files'): + if not settings.DEBUG and hasattr(staticfiles_storage, 'hashed_files'): log.debug('Hashed static files storage detected') relative_path = get_reversed_hashed_files()[relative_path] data['filename'] = Path(relative_path).name