8000 Fix #229 -- Delete old file after validation is passed · codingjoe/django-stdimage@3e473c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e473c1

Browse files
Fix #229 -- Delete old file after validation is passed
Previously files were deleted even if the from validation failed. Co-Authored-By: codingjoe <johannes@maron.family>
1 parent 0c5b5e5 commit 3e473c1

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

stdimage/models.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def __init__(
264264

265265
super().__init__(verbose_name=verbose_name, name=name, **kwargs)
266266

267+
# The attribute name of the old file to use on the model object
268+
self._old_attname = "_old_%s" % name
269+
267270
def add_variation(self, name, params):
268271
variation = self.def_variation.copy()
269272
variation["kwargs"] = {}
@@ -313,9 +316,19 @@ def save_form_data(self, instance, data):
313316
if self.delete_orphans and (data is False or data is not None):
314317
file = getattr(instance, self.name)
315318
if file and file._committed and file != data:
316-
file.delete(save=False)
319+
# Store the old file which should be deleted if the new one is valid
320+
setattr(instance, self._old_attname, file)
317321
super().save_form_data(instance, data)
318322

323+
def pre_save(self, model_instance, add):
324+
if hasattr(model_instance, self._old_attname):
325+
# Delete the old file and its variations from the storage
326+
old_file = getattr(model_instance, self._old_attname)
327+
old_file.delete_variations()
328+
old_file.storage.delete(old_file.name)
329+
delattr(model_instance, self._old_attname)
330+
return super().pre_save(model_instance, add)
331+
319332
def deconstruct(self):
320333
name, path, args, kwargs = super().deconstruct()
321334
return (

tests/forms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ class ThumbnailModelForm(forms.ModelForm):
77
class Meta:
88
model = models.ThumbnailModel
99
fields = "__all__"
10+
11+
12+
class MinSizeModelForm(forms.ModelForm):
13+
class Meta:
14+
model = models.MinSizeModel
15+
fields = "__all__"

tests/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ class MaxSizeModel(models.Model):
9595

9696

9797
class MinSizeModel(models.Model):
98-
image = StdImageField(upload_to=upload_to, validators=[MinSizeValidator(200, 200)])
98+
image = StdImageField(
99+
upload_to=upload_to,
100+
delete_orphans=True,
101+
validators=[MinSizeValidator(200, 200)],
102+
)
99103

100104

101105
class ForceMinSizeModel(models.Model):

tests/test_forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@ def test_save_form_data__none(self, db):
4545
obj = form.save()
4646
assert obj.image
4747
assert os.path.exists(org_path)
48+
49+
def test_save_form_data__invalid(self, db):
50+
instance = models.MinSizeModel.objects.create(
51+
image=self.fixtures["600x400.jpg"]
52+
)
53+
org_path = instance.image.path
54+
assert os.path.exists(org_path)
55+
form = forms.MinSizeModelForm(
56+
files={"image": self.fixtures["100.gif"]},
57+
instance=instance,
58+
)
59+
assert not form.is_valid()
60+
assert os.path.exists(org_path)

0 commit comments

Comments
 (0)
0