From: Jan Szejko Date: Tue, 4 Dec 2018 17:43:28 +0000 (+0100) Subject: delete old file when attachment changes X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/56fdd60e7a54f19cd9fc10db475688a557ed7df7 delete old file when attachment changes --- diff --git a/contact/forms.py b/contact/forms.py index a9e3366..cc60c73 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -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()