From b348c63d7a148ec3fec2b8b820b1c621570a5fb4 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 28 Sep 2024 16:26:40 +0200 Subject: [PATCH] Fixes #729 - THUMBNAIL_STORAGES is an alias to Django STORAGES --- CHANGES.rst | 5 +++++ docs/reference/settings.rst | 6 ++++-- sorl/thumbnail/conf/defaults.py | 6 ++---- sorl/thumbnail/default.py | 6 +++++- sorl/thumbnail/images.py | 2 +- tests/settings/default.py | 2 +- tests/thumbnail_tests/test_storage.py | 7 +++++++ 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 37748df2f..b9e794ef5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,11 @@ Changes ======= +Unreleased +========== +* ``THUMBNAIL_STORAGE`` should now be an alias in the Django ``STORAGES`` setting. + The old way of specifying a dotted path to a Storage module is still supported. + 12.11.0 ======= * Deprecated ``THUMBNAIL_KVSTORE``. Only the Django cache-based store will be diff --git a/docs/reference/settings.rst b/docs/reference/settings.rst index 3988f6ad2..14b26da1e 100644 --- a/docs/reference/settings.rst +++ b/docs/reference/settings.rst @@ -166,9 +166,11 @@ Only applicable for the convert Engine. ``THUMBNAIL_STORAGE`` ===================== -- Default: ``settings.DEFAULT_FILE_STORAGE`` +- Default: ``default`` -The storage class to use for the generated thumbnails. +The storage to use for the generated thumbnails, as an alias from the Django +``STORAGES`` setting. +Optionally accepts a path to a Storage subclass. ``THUMBNAIL_REDIS_URL`` diff --git a/sorl/thumbnail/conf/defaults.py b/sorl/thumbnail/conf/defaults.py index 5618622ed..ab6e30f9c 100644 --- a/sorl/thumbnail/conf/defaults.py +++ b/sorl/thumbnail/conf/defaults.py @@ -1,5 +1,3 @@ -from django.conf import settings - # When True ThumbnailNode.render can raise errors THUMBNAIL_DEBUG = False @@ -30,8 +28,8 @@ THUMBNAIL_VIPSTHUMBNAIL = 'vipsthumbnail' THUMBNAIL_VIPSHEADER = 'vipsheader' -# Storage for the generated thumbnails -THUMBNAIL_STORAGE = settings.STORAGES['default']['BACKEND'] +# Storage for the generated thumbnails, as an alias of the Django STORAGES setting. +THUMBNAIL_STORAGE = 'default' # Redis settings THUMBNAIL_REDIS_DB = 0 diff --git a/sorl/thumbnail/default.py b/sorl/thumbnail/default.py index fbbccc40e..d3092fe36 100644 --- a/sorl/thumbnail/default.py +++ b/sorl/thumbnail/default.py @@ -1,3 +1,4 @@ +from django.core.files.storage import storages from django.utils.functional import LazyObject from sorl.thumbnail.conf import settings @@ -21,7 +22,10 @@ def _setup(self): class Storage(LazyObject): def _setup(self): - self._wrapped = get_module_class(settings.THUMBNAIL_STORAGE)() + if "." in settings.THUMBNAIL_STORAGE: + self._wrapped = get_module_class(settings.THUMBNAIL_STORAGE)() + else: + self._wrapped = storages[settings.THUMBNAIL_STORAGE] backend = Backend() diff --git a/sorl/thumbnail/images.py b/sorl/thumbnail/images.py index a656ec2b9..a3bac713f 100644 --- a/sorl/thumbnail/images.py +++ b/sorl/thumbnail/images.py @@ -8,7 +8,7 @@ from urllib.request import Request, urlopen from django.core.files.base import ContentFile, File -from django.core.files.storage import Storage # , default_storage +from django.core.files.storage import Storage from django.utils.encoding import force_str from django.utils.functional import LazyObject, empty diff --git a/tests/settings/default.py b/tests/settings/default.py index 2c98c5319..3394afcc2 100644 --- a/tests/settings/default.py +++ b/tests/settings/default.py @@ -12,7 +12,7 @@ 'level': 'ERROR', } THUMBNAIL_KVSTORE = 'tests.thumbnail_tests.kvstore.TestKVStore' -THUMBNAIL_STORAGE = 'tests.thumbnail_tests.storage.TestStorage' +THUMBNAIL_STORAGE = 'default' STORAGES = { "default": { "BACKEND": "tests.thumbnail_tests.storage.TestStorage", diff --git a/tests/thumbnail_tests/test_storage.py b/tests/thumbnail_tests/test_storage.py index 2b8c244dc..7e26c8add 100644 --- a/tests/thumbnail_tests/test_storage.py +++ b/tests/thumbnail_tests/test_storage.py @@ -1,5 +1,7 @@ import unittest +from django.test.utils import override_settings + from sorl.thumbnail import default, get_thumbnail from sorl.thumbnail.helpers import get_module_class @@ -37,6 +39,11 @@ def test_safe_methods(self): self.assertIsNotNone(im.y) self.assertEqual(self.log, []) + @override_settings(THUMBNAIL_STORAGE="tests.thumbnail_tests.storage.TestStorage") + def test_storage_setting_as_path_to_class(self): + storage = default.Storage() + self.assertEqual(storage.__class__.__name__, "TestStorage") + class UrlStorageTestCase(unittest.TestCase): def test_encode_utf8_filenames(self):