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

Fixes #729 - THUMBNAIL_STORAGES is an alias to Django STORAGES #769

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions docs/reference/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
6 changes: 2 additions & 4 deletions sorl/thumbnail/conf/defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.conf import settings

# When True ThumbnailNode.render can raise errors
THUMBNAIL_DEBUG = False

Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion sorl/thumbnail/default.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.files.storage import storages
from django.utils.functional import LazyObject

from sorl.thumbnail.conf import settings
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion sorl/thumbnail/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions tests/thumbnail_tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand Down
Loading