delete old file when attachment changes
authorJan Szejko <janek37@gmail.com>
Tue, 4 Dec 2018 17:43:28 +0000 (18:43 +0100)
committerJan Szejko <janek37@gmail.com>
Tue, 4 Dec 2018 17:43:28 +0000 (18:43 +0100)
contact/forms.py

index a9e3366..cc60c73 100644 (file)
@@ -1,4 +1,6 @@
 # -*- coding: utf-8 -*-
+import os
+
 from django.contrib.sites.models import Site
 from django.core.exceptions import ValidationError
 from django.core.files.uploadedfile import UploadedFile
@@ -141,7 +143,9 @@ class ContactForm(forms.Form):
             email_changed = True
         for name, value in self.cleaned_data.items():
             if isinstance(value, UploadedFile):
-                Attachment.objects.filter(contact=contact, tag=name).delete()  # delete files?
+                for attachment in Attachment.objects.filter(contact=contact, tag=name):
+                    os.remove(attachment.file.path)
+                    attachment.delete()
                 attachment = Attachment(contact=contact, tag=name)
                 attachment.file.save(value.name, value)
                 attachment.save()