SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit a68d4b1b authored by George Katsikas's avatar George Katsikas :goat:
Browse files

handle deletion & update of submissionattachments

fix #193
parent 947b04fa
No related branches found
No related tags found
No related merge requests found
...@@ -285,12 +285,22 @@ class SubsidyAttachment(models.Model): ...@@ -285,12 +285,22 @@ class SubsidyAttachment(models.Model):
# Delete attachment files with same name if they exist, allowing replacement without name change # Delete attachment files with same name if they exist, allowing replacement without name change
@receiver(pre_save, sender=SubsidyAttachment) @receiver(pre_save, sender=SubsidyAttachment)
def delete_old_attachment_file(sender, instance, **kwargs): def delete_old_attachment_file(sender, instance, **kwargs):
if instance.pk: """
old_attachment = SubsidyAttachment.objects.get(pk=instance.pk) Replace existing file on update if a new one is provided.
old_filename = old_attachment.attachment.name.split("/")[-1] """
if instance.pk and instance.attachment:
old = SubsidyAttachment.objects.get(pk=instance.pk)
if old.attachment and old.attachment != instance.attachment:
old.attachment.delete(save=False)
if old_attachment.attachment and old_filename == instance.attachment.name: @receiver(models.signals.post_delete, sender=SubsidyAttachment)
old_attachment.attachment.delete(save=False) def auto_delete_file_on_delete(sender, instance, **kwargs):
"""
Deletes file from filesystem when its object is deleted.
"""
if instance.attachment:
instance.attachment.delete(save=False)
########################### ###########################
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment