Skip to content

Fix #206 -- Overwrite variations by defaults #213

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

Merged
merged 1 commit into from
Aug 28, 2019
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
21 changes: 11 additions & 10 deletions stdimage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ def is_smaller(img, variation):
return img.size[0] > variation['width'] \
or img.size[1] > variation['height']

def render_variations(self, replace=False):
def render_variations(self, replace=True):
"""Render all image variations and saves them to the storage."""
for _, variation in self.field.variations.items():
self.render_variation(self.name, variation, replace, self.storage)

@classmethod
def render_variation(cls, file_name, variation, replace=False,
def render_variation(cls, file_name, variation, replace=True,
storage=default_storage):
"""Render an image variation and saves it to the storage."""
variation_name = cls.get_variation_name(file_name, variation['name'])
if storage.exists(variation_name):
if replace:
storage.delete(variation_name)
logger.info('File "%s" already exists and has been replaced.',
variation_name)
else:
logger.info('File "%s" already exists.', variation_name)
return variation_name
file_overwrite = getattr(storage, 'file_overwrite', False)
if not replace and storage.exists(variation_name):
logger.info('File "%s" already exists.', variation_name)
return variation_name
elif replace and not file_overwrite and storage.exists(variation_name):
logger.warning(
'File "%s" already exists and will be overwritten.', variation_name
)
storage.delete(variation_name)

ImageFile.LOAD_TRUNCATED_IMAGES = True
with storage.open(file_name) as f:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_no_replace(self, image_upload_file):
file_path = obj.image.thumbnail.path
assert os.path.exists(file_path)
before = os.path.getmtime(file_path)
time.sleep(1)
time.sleep(0.1)
call_command(
'rendervariations',
'tests.ThumbnailModel.image',
Expand All @@ -71,7 +71,7 @@ def test_replace(self, image_upload_file):
file_path = obj.image.thumbnail.path
assert os.path.exists(file_path)
before = os.path.getmtime(file_path)
time.sleep(1)
time.sleep(0.1)
call_command(
'rendervariations',
'tests.ThumbnailModel.image',
Expand All @@ -87,7 +87,7 @@ def test_ignore_missing(self, image_upload_file):
assert os.path.exists(file_path)
os.remove(file_path)
assert not os.path.exists(file_path)
time.sleep(1)
time.sleep(0.1)
call_command(
'rendervariations',
'tests.ThumbnailModel.image',
Expand All @@ -101,7 +101,7 @@ def test_short_ignore_missing(self, image_upload_file):
assert os.path.exists(file_path)
os.remove(file_path)
assert not os.path.exists(file_path)
time.sleep(1)
time.sleep(0.1)
call_command(
'rendervariations',
'tests.ThumbnailModel.image',
Expand All @@ -115,7 +115,7 @@ def test_no_ignore_missing(self, image_upload_file):
assert os.path.exists(file_path)
os.remove(file_path)
assert not os.path.exists(file_path)
time.sleep(1)
time.sleep(0.1)
with pytest.raises(CommandError):
call_command(
'rendervariations',
Expand Down
14 changes: 14 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import os
import time

import pytest
from django.conf import settings
Expand Down Expand Up @@ -210,6 +211,19 @@ def test_render_variations_callback(self, db):
file_path = obj.image.thumbnail.path
assert os.path.exists(file_path)

def test_render_variations_overwrite(self, db, image_upload_file):
obj = ThumbnailModel.objects.create(image=image_upload_file)
file_path = obj.image.thumbnail.path
before = os.path.getmtime(file_path)
time.sleep(0.1)
os.remove(obj.image.path)
assert os.path.exists(file_path)
obj.image = image_upload_file
obj.save()
assert file_path == obj.image.thumbnail.path
after = os.path.getmtime(file_path)
assert before != after, obj.image.path


class TestValidators(TestStdImage):
def test_max_size_validator(self, admin_client):
Expand Down