From 2041dcaa1ba825294550ad1df76863d6c50b211a Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Fri, 11 Oct 2024 12:27:50 +0200
Subject: [PATCH] add command to detach attachments from payments

---
 scipost_django/finances/admin.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/scipost_django/finances/admin.py b/scipost_django/finances/admin.py
index c20c26aa5..3e333816c 100644
--- a/scipost_django/finances/admin.py
+++ b/scipost_django/finances/admin.py
@@ -66,6 +66,17 @@ class SubsidyAdmin(admin.ModelAdmin):
         return obj.organization.get_orgtype_display()
 
 
+@admin.action(description="Detach from all schedules")
+def detach(modeladmin, request, queryset):
+    for obj in queryset:
+        if (payment_proof := getattr(obj, "proof_of_payment_for", None)) is not None:
+            payment_proof.proof_of_payment = None
+            payment_proof.save()
+        if (invoice_proof := getattr(obj, "invoice_for", None)) is not None:
+            invoice_proof.invoice = None
+            invoice_proof.save()
+
+
 @admin.register(SubsidyAttachment)
 class SubsidyAttachmentAdmin(admin.ModelAdmin):
     list_display = [
@@ -86,6 +97,7 @@ class SubsidyAttachmentAdmin(admin.ModelAdmin):
         "subsidy__organization__name_original",
         "subsidy__organization__acronym",
     ]
+    actions = [detach]
 
 
 @admin.register(SubsidyPayment)
-- 
GitLab