Skip to content

Commit

Permalink
add support for Django 4.2 storages
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed May 28, 2024
1 parent f57b334 commit 2d58fd4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
7 changes: 7 additions & 0 deletions easy_thumbnails/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions easy_thumbnails/management/commands/thumbnail_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import time
from datetime import date

from django.core.files.storage import get_storage_class
from django.core.management.base import BaseCommand
from django.utils.timezone import datetime, timedelta

from easy_thumbnails.conf import settings
from easy_thumbnails.models import Source
from easy_thumbnails.storage import get_storage


class ThumbnailCollectionCleaner:
Expand Down Expand Up @@ -52,7 +52,7 @@ def clean_up(self, dry_run=False, verbosity=1, last_n_days=0,
self.stdout.write("Dry run...")

if not storage:
storage = get_storage_class(settings.THUMBNAIL_DEFAULT_STORAGE)()
storage = get_storage()

sources_to_delete = []
time_start = time.time()
Expand Down
24 changes: 16 additions & 8 deletions easy_thumbnails/storage.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.core.files.storage import FileSystemStorage
from django.core.files.storage.handler import InvalidStorageError
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, InvalidStorageError):
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):
Expand All @@ -22,10 +36,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()
10 changes: 10 additions & 0 deletions easy_thumbnails/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2d58fd4

Please sign in to comment.