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

Support backend storage from field #101

Closed
wants to merge 11 commits into from
4 changes: 1 addition & 3 deletions thumbnails/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class ImageField(DjangoImageField):
def __init__(self, *args, **kwargs):
self.resize_source_to = kwargs.pop('resize_source_to', None)
self.pregenerated_sizes = kwargs.pop('pregenerated_sizes', [])
if kwargs.get('storage'):
raise ValueError('Please define storage backend in settings.py instead on the field itself')
self.storage_backend = storage.get_backend()
self.storage_backend = storage.get_backend() or kwargs.get('storage')
self.metadata_backend = metadata.get_backend()
kwargs['storage'] = self.storage_backend
super(ImageField, self).__init__(*args, **kwargs)
Expand Down
6 changes: 3 additions & 3 deletions thumbnails/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def get(self, size, create=True):

if thumbnail is None:
thumbnail = images.get(self.source_image.name, size,
self.metadata_backend, self.storage)
self.storage, self.metadata_backend)

if thumbnail is None:
thumbnail = self.create(size)
Expand All @@ -103,15 +103,15 @@ def create(self, size):
Creates and return a thumbnail of a given size.
"""
thumbnail = images.create(self.source_image.name, size,
self.metadata_backend, self.storage)
self.storage, self.metadata_backend)
return thumbnail

def delete(self, size):
"""
Deletes a thumbnail of a given size
"""
images.delete(self.source_image.name, size,
self.metadata_backend, self.storage)
self.storage, self.metadata_backend)

if self._thumbnails is not None and self._thumbnails.get(size):
del(self._thumbnails[size])
Expand Down
13 changes: 3 additions & 10 deletions thumbnails/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ def get_thumbnail_name(source_name, size):
return os.path.join(conf.BASE_DIR, filename)


def create(source_name, size, metadata_backend=None, storage_backend=None):
def create(source_name, size, storage_backend, metadata_backend=None):
"""
Creates a thumbnail file and its relevant metadata. Returns a
Thumbnail instance.
"""

if storage_backend is None:
storage_backend = backends.storage.get_backend()
if metadata_backend is None:
metadata_backend = backends.metadata.get_backend()

Expand All @@ -91,12 +88,10 @@ def save(source_name, size, metadata_backend, storage_backend, image_file):
return Thumbnail(metadata=metadata, storage=storage_backend)


def get(source_name, size, metadata_backend=None, storage_backend=None):
def get(source_name, size, storage_backend, metadata_backend=None):
"""
Returns a Thumbnail instance, or None if thumbnail does not yet exist.
"""
if storage_backend is None:
storage_backend = backends.storage.get_backend()
if metadata_backend is None:
metadata_backend = backends.metadata.get_backend()

Expand All @@ -107,12 +102,10 @@ def get(source_name, size, metadata_backend=None, storage_backend=None):
return Thumbnail(metadata=metadata, storage=storage_backend)


def delete(source_name, size, metadata_backend=None, storage_backend=None):
def delete(source_name, size, storage_backend, metadata_backend=None):
"""
Deletes a thumbnail file and its relevant metadata.
"""
if storage_backend is None:
storage_backend = backends.storage.get_backend()
if metadata_backend is None:
metadata_backend = backends.metadata.get_backend()

Expand Down
3 changes: 1 addition & 2 deletions thumbnails/management/commands/delete_thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def handle(self, path_to_model, field_name, size, *args, **kwargs):
instances = model.objects.exclude(**exclude_args)

metadata_backend = metadata.get_backend()
storage_backend = storage.get_backend()

for instance in instances:
field = getattr(instance, field_name)
images.delete(field.name, size, metadata_backend, storage_backend)
images.delete(field.name, size, field.thumbnails.storage, metadata_backend)
18 changes: 9 additions & 9 deletions thumbnails/tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_create(self):
self.assertFalse(self.storage_backend.exists(thumbnail_name))

thumbnail = images.create(self.source_name, 'small',
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)
self.assertTrue(self.storage_backend.exists(thumbnail.name))
self.assertNotEqual(
self.metadata_backend.get_thumbnail(self.source_name, 'small'),
Expand All @@ -55,15 +55,15 @@ def test_get(self):
"""
self.assertEqual(
images.get(self.source_name, 'default',
self.metadata_backend, self.storage_backend),
self.storage_backend, self.metadata_backend),
None
)
thumbnail = images.create(self.source_name, 'default',
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)

self.assertEqual(
images.get(self.source_name, 'default',
self.metadata_backend, self.storage_backend),
self.storage_backend, self.metadata_backend),
thumbnail
)

Expand All @@ -83,10 +83,10 @@ def test_delete(self):
Ensure that ``delete`` works properly.
"""
thumbnail = images.create(self.source_name, 'small',
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)

images.delete(self.source_name, 'small',
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)
self.assertFalse(self.storage_backend.exists(thumbnail.name))
self.assertEqual(
self.metadata_backend.get_thumbnail(self.source_name, 'small'),
Expand All @@ -95,7 +95,7 @@ def test_delete(self):

# key with format is not defined in settings anymore
thumbnail = images.create(self.source_name, 'source_with_format',
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)

thumbnail_size_data = {
'small': {
Expand All @@ -107,7 +107,7 @@ def test_delete(self):
with patch('thumbnails.images.conf.SIZES', thumbnail_size_data):
# ensure error not happening, and its files and metadata are still removed
images.delete(self.source_name, 'source_with_format',
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)
self.assertFalse(self.storage_backend.exists(thumbnail.name))
self.assertIsNone(self.metadata_backend.get_thumbnail(self.source_name, 'source_with_format'))

Expand All @@ -128,7 +128,7 @@ def test_delete_with_no_thumbnails_file(self):
self.assertFalse(os.path.exists(thumbnail_path))

images.delete(self.source_name, size,
self.metadata_backend, self.storage_backend)
self.storage_backend, self.metadata_backend)

# thumbnails and their metadata are also deleted
self.assertEqual(len(os.listdir(avatar_folder)), 0)
Expand Down