diff --git a/cotisations/models.py b/cotisations/models.py index 64ef791d..d7178f20 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -33,6 +33,7 @@ each. from __future__ import unicode_literals from dateutil.relativedelta import relativedelta +import os from django.db import models from django.db.models import Q, Max @@ -976,3 +977,34 @@ class DocumentTemplate(RevMixin, AclMixin, models.Model): def __str__(self): return str(self.name) + +@receiver(models.signals.post_delete, sender=DocumentTemplate) +def auto_delete_file_on_delete(sender, instance, **kwargs): + """ + Deletes file from filesystem + when corresponding `DocumentTemplate` object is deleted. + """ + if instance.template: + if os.path.isfile(instance.template.path): + os.remove(instance.template.path) + + +@receiver(models.signals.pre_save, sender=DocumentTemplate) +def auto_delete_file_on_change(sender, instance, **kwargs): + """ + Deletes old file from filesystem + when corresponding `DocumentTemplate` object is updated + with new file. + """ + if not instance.pk: + return False + + try: + old_file = DocumentTemplate.objects.get(pk=instance.pk).template + except DocumentTemplate.DoesNotExist: + return False + + new_file = instance.template + if not old_file == new_file: + if os.path.isfile(old_file.path): + os.remove(old_file.path) diff --git a/cotisations/templates/cotisations/facture.html b/cotisations/templates/cotisations/facture.html index 65b05199..5dddb305 100644 --- a/cotisations/templates/cotisations/facture.html +++ b/cotisations/templates/cotisations/facture.html @@ -51,7 +51,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% bootstrap_form_errors discount_form %} {% endif %} -