Skip to content

Commit 7efcd28

Browse files
committed
Fix #206 -- Overwrite variations by defaults
We now overwrite files by default when a new files gets uploaded. This fixes an issue, where someone might upload a file with the same name twice the first initial variations get not replaced with a version of the newer file. It happens in the least IO consuming way. The rendervariations management command still behaves as before where variations are not replaced by default.
1 parent f8a967c commit 7efcd28

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

stdimage/models.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,25 @@ def is_smaller(img, variation):
4848
return img.size[0] > variation['width'] \
4949
or img.size[1] > variation['height']
5050

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

5656
@classmethod
57-
def render_variation(cls, file_name, variation, replace=False,
57+
def render_variation(cls, file_name, variation, replace=True,
5858
storage=default_storage):
5959
"""Render an image variation and saves it to the storage."""
6060
variation_name = cls.get_variation_name(file_name, variation['name'])
61-
if storage.exists(variation_name):
62-
if replace:
63-
storage.delete(variation_name)
64-
logger.info('File "%s" already exists and has been replaced.',
65-
variation_name)
66-
else:
67-
logger.info('File "%s" already exists.', variation_name)
68-
return variation_name
61+
file_overwrite = getattr(storage, 'file_overwrite', False)
62+
if not replace and storage.exists(variation_name):
63+
logger.info('File "%s" already exists.', variation_name)
64+
return variation_name
65+
elif replace and not file_overwrite and storage.exists(variation_name):
66+
logger.warning(
67+
'File "%s" already exists and will be overwritten.', variation_name
68+
)
69+
storage.delete(variation_name)
6970

7071
ImageFile.LOAD_TRUNCATED_IMAGES = True
7172
with storage.open(file_name) as f:

tests/test_commands.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_no_replace(self, image_upload_file):
5757
file_path = obj.image.thumbnail.path
5858
assert os.path.exists(file_path)
5959
before = os.path.getmtime(file_path)
60-
time.sleep(1)
60+
time.sleep(0.1)
6161
call_command(
6262
'rendervariations',
6363
'tests.ThumbnailModel.image',
@@ -71,7 +71,7 @@ def test_replace(self, image_upload_file):
7171
file_path = obj.image.thumbnail.path
7272
assert os.path.exists(file_path)
7373
before = os.path.getmtime(file_path)
74-
time.sleep(1)
74+
time.sleep(0.1)
7575
call_command(
7676
'rendervariations',
7777
'tests.ThumbnailModel.image',
@@ -87,7 +87,7 @@ def test_ignore_missing(self, image_upload_file):
8787
assert os.path.exists(file_path)
8888
os.remove(file_path)
8989
assert not os.path.exists(file_path)
90-
time.sleep(1)
90+
time.sleep(0.1)
9191
call_command(
9292
'rendervariations',
9393
'tests.ThumbnailModel.image',
@@ -101,7 +101,7 @@ def test_short_ignore_missing(self, image_upload_file):
101101
assert os.path.exists(file_path)
102102
os.remove(file_path)
103103
assert not os.path.exists(file_path)
104-
time.sleep(1)
104+
time.sleep(0.1)
105105
call_command(
106106
'rendervariations',
107107
'tests.ThumbnailModel.image',
@@ -115,7 +115,7 @@ def test_no_ignore_missing(self, image_upload_file):
115115
assert os.path.exists(file_path)
116116
os.remove(file_path)
117117
assert not os.path.exists(file_path)
118-
time.sleep(1)
118+
time.sleep(0.1)
119119
with pytest.raises(CommandError):
120120
call_command(
121121
'rendervariations',

tests/test_models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io
22
import os
3+
import time
34

45
import pytest
56
from django.conf import settings
@@ -210,6 +211,19 @@ def test_render_variations_callback(self, db):
210211
file_path = obj.image.thumbnail.path
211212
assert os.path.exists(file_path)
212213

214+
def test_render_variations_overwrite(self, db, image_upload_file):
215+
obj = ThumbnailModel.objects.create(image=image_upload_file)
216+
file_path = obj.image.thumbnail.path
217+
before = os.path.getmtime(file_path)
218+
time.sleep(0.1)
219+
os.remove(obj.image.path)
220+
assert os.path.exists(file_path)
221+
obj.image = image_upload_file
222+
obj.save()
223+
assert file_path == obj.image.thumbnail.path
224+
after = os.path.getmtime(file_path)
225+
assert before != after, obj.image.path
226+
213227

214228
class TestValidators(TestStdImage):
215229
def test_max_size_validator(self, admin_client):

0 commit comments

Comments
 (0)