From 987b4324e0d37f3add400045fbb9a21baeb1812a Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 12 Mar 2024 13:15:22 +0100
Subject: [PATCH] allow attachment misnaming only when orphaned

---
 scipost_django/finances/forms.py | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/scipost_django/finances/forms.py b/scipost_django/finances/forms.py
index 34aac0d01..0aee1b6cc 100644
--- a/scipost_django/finances/forms.py
+++ b/scipost_django/finances/forms.py
@@ -315,13 +315,13 @@ class SubsidyAttachmentForm(forms.ModelForm):
         required=False,
     )
 
-    def clean_attachment(self):
+    def clean(self):
+        orphaned = self.cleaned_data["subsidy"] is None
         attachment = self.cleaned_data["attachment"]
-        existing_attachment = getattr(self.instance, "attachment", None)
 
-        # Allow already uploaded attachments
-        if existing_attachment and attachment is not None:
-            return attachment
+        # Allow misnamed orphans
+        if orphaned:
+            return
 
         filename_regex = (
             "^SciPost_"
@@ -336,7 +336,6 @@ class SubsidyAttachmentForm(forms.ModelForm):
                 "The filename does not match the required regex pattern "
                 f"'{filename_regex}'",
             )
-        return attachment
 
 
 #############
-- 
GitLab