From 610ef1577234d80fd164035fb207f28e78dd66d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org>
Date: Tue, 2 May 2023 11:51:19 +0200
Subject: [PATCH] de-duplicate contract issues

---
 .../management/commands/import_old_contracts.py     | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/contracts/management/commands/import_old_contracts.py b/contracts/management/commands/import_old_contracts.py
index 73ecca6..d2fafd0 100644
--- a/contracts/management/commands/import_old_contracts.py
+++ b/contracts/management/commands/import_old_contracts.py
@@ -75,9 +75,17 @@ class Command(BaseCommand):
         )
 
     def use_issue(self, name: str) -> None:
-        issue = ContractIssue.objects.filter(name=name).first()
+        issues = ContractIssue.objects.filter(name=name).all()
 
-        if issue is None:
+        if len(issues) != 0:
+            issue = issues[0]
+        elif len(issues) > 1:
+            for issue in issues[1:]:
+                for contract in issue.contracts:
+                    contract.issues.add(issue)
+
+                issue.delete()
+        else:
             issue = ContractIssue(name=name)
             issue.save()
 
@@ -1375,6 +1383,7 @@ class Command(BaseCommand):
                 Contract,
                 ContractType,
                 ContractFilingArea,
+                ContractIssue,
                 ContractFile,
                 Contractee,
                 ContracteeSignature,
-- 
GitLab