From dd1a5d344594a0b100b7c73a44a37ae54fda67da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Thu, 16 Nov 2023 15:55:50 +0100 Subject: [PATCH] add support for Django 4.2 storages --- easy_thumbnails/conf.py | 7 +++++++ easy_thumbnails/storage.py | 23 +++++++++++++++-------- easy_thumbnails/tests/settings.py | 10 ++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/easy_thumbnails/conf.py b/easy_thumbnails/conf.py index 76da3467..3e8a6044 100644 --- a/easy_thumbnails/conf.py +++ b/easy_thumbnails/conf.py @@ -100,8 +100,15 @@ class Settings(AppSettings): THUMBNAIL_DEFAULT_STORAGE = ( 'easy_thumbnails.storage.ThumbnailFileSystemStorage') """ + (DEPRECATED) The default Django storage for *saving* generated thumbnails. """ + + THUMBNAIL_DEFAULT_STORAGE_ALIAS = 'easy_thumbnails' + """ + Django 4.2+: The default Django storage name for *saving* generated thumbnails. + """ + THUMBNAIL_MEDIA_ROOT = '' """ Used by easy-thumbnail's default storage to locate where thumbnails are diff --git a/easy_thumbnails/storage.py b/easy_thumbnails/storage.py index 58e2867e..b9af71cd 100644 --- a/easy_thumbnails/storage.py +++ b/easy_thumbnails/storage.py @@ -1,9 +1,22 @@ -from django.core.files.storage import FileSystemStorage, get_storage_class +from django.core.files.storage import FileSystemStorage from django.utils.deconstruct import deconstructible from django.utils.functional import LazyObject from easy_thumbnails.conf import settings +def get_storage(): + # If the user has specified a custom storage backend, use it. + try: + from django.core.files.storage import storages + return storages[settings.THUMBNAIL_DEFAULT_STORAGE_ALIAS] + except (ImportError, TypeError): + from django.core.files.storage import get_storage_class + storage_class = get_storage_class(settings.THUMBNAIL_DEFAULT_STORAGE) + class ThumbnailDefaultStorage(LazyObject): + def _setup(self): + self._wrapped = storage_class() + return ThumbnailDefaultStorage() + @deconstructible class ThumbnailFileSystemStorage(FileSystemStorage): @@ -22,10 +35,4 @@ def __init__(self, location=None, base_url=None, *args, **kwargs): super().__init__(location, base_url, *args, **kwargs) -class ThumbnailDefaultStorage(LazyObject): - def _setup(self): - self._wrapped = get_storage_class( - settings.THUMBNAIL_DEFAULT_STORAGE)() - - -thumbnail_default_storage = ThumbnailDefaultStorage() +thumbnail_default_storage = get_storage() diff --git a/easy_thumbnails/tests/settings.py b/easy_thumbnails/tests/settings.py index 0d34c3a2..5914a6b4 100644 --- a/easy_thumbnails/tests/settings.py +++ b/easy_thumbnails/tests/settings.py @@ -28,6 +28,16 @@ 'easy_thumbnails.tests.apps.EasyThumbnailsTestConfig', ] + +STORAGES = { + "easy_thumbnails": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, +} + TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates',