SciPost Code Repository

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

allow replacement of subsidy attachments

parent 25b74518
No related branches found
No related tags found
No related merge requests found
...@@ -263,6 +263,11 @@ class SubsidyAttachmentForm(forms.ModelForm): ...@@ -263,6 +263,11 @@ class SubsidyAttachmentForm(forms.ModelForm):
def clean_attachment(self): def clean_attachment(self):
attachment = self.cleaned_data["attachment"] attachment = self.cleaned_data["attachment"]
# Allow already uploaded attachments
if hasattr(self.instance, "attachment") and not attachment is None:
return attachment
filename_regex = ( filename_regex = (
"^SciPost_" "^SciPost_"
"[0-9]{4,}(-[0-9]{4,})?_[A-Z]{2,}_[\w]+_" "[0-9]{4,}(-[0-9]{4,})?_[A-Z]{2,}_[\w]+_"
......
...@@ -10,6 +10,8 @@ from django.contrib.contenttypes.models import ContentType ...@@ -10,6 +10,8 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.db import models from django.db import models
from django.db.models import Sum from django.db.models import Sum
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.html import format_html from django.utils.html import format_html
...@@ -280,6 +282,17 @@ class SubsidyAttachment(models.Model): ...@@ -280,6 +282,17 @@ class SubsidyAttachment(models.Model):
return False return False
# Delete attachment files with same name if they exist, allowing replacement without name change
@receiver(pre_save, sender=SubsidyAttachment)
def delete_old_attachment_file(sender, instance, **kwargs):
if instance.pk:
old_attachment = SubsidyAttachment.objects.get(pk=instance.pk)
old_filename = old_attachment.attachment.name.split("/")[-1]
if old_attachment.attachment and old_filename == instance.attachment.name:
old_attachment.attachment.delete(save=False)
########################### ###########################
# Work hours registration # # Work hours registration #
########################### ###########################
......
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