Skip to content

Commit d0f13b2

Browse files
committed
Delete old file when validation is passed (fix #229)
1 parent f6d953b commit d0f13b2

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

stdimage/models.py

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

255255
super().__init__(verbose_name=verbose_name, name=name, **kwargs)
256256

257+
# The attribute name of the old file to use on the model object
258+
self._old_attname = "_old_%s" % name
259+
257260
def add_variation(self, name, params):
258261
variation = self.def_variation.copy()
259262
variation["kwargs"] = {}
@@ -303,9 +306,19 @@ def save_form_data(self, instance, data):
303306
if self.delete_orphans and (data is False or data is not None):
304307
file = getattr(instance, self.name)
305308
if file and file._committed and file != data:
306-
file.delete(save=False)
309+
# Store the old file which should be deleted if the new one is valid
310+
setattr(instance, self._old_attname, file)
307311
super().save_form_data(instance, data)
308312

313+
def pre_save(self, model_instance, add):
314+
if hasattr(model_instance, self._old_attname):
315+
# Delete the old file and its variations from the storage
316+
old_file = getattr(model_instance, self._old_attname)
317+
old_file.delete_variations()
318+
old_file.storage.delete(old_file.name)
319+
delattr(model_instance, self._old_attname)
320+
return super().pre_save(model_instance, add)
321+
309322

310323
class JPEGFieldFile(StdImageFieldFile):
311324
@classmethod

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.ThumbnailModelForm(
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy