Skip to content

Commit

Permalink
Fixes #729 - THUMBNAIL_STORAGES is an alias to Django STORAGES
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed Oct 25, 2024
1 parent 594418a commit b348c63
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
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

0 comments on commit b348c63

Please sign in to comment.