From b597a6ac5b2067cd7bc31b4334e5907cdec9223b Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <ynerant@crans.org>
Date: Sun, 21 Feb 2021 23:05:27 +0100
Subject: [PATCH] Fix soge credit deletion when the account is not validated
 yet

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
---
 apps/treasury/models.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/apps/treasury/models.py b/apps/treasury/models.py
index 7782ebec..b2b2596c 100644
--- a/apps/treasury/models.py
+++ b/apps/treasury/models.py
@@ -381,9 +381,14 @@ class SogeCredit(models.Model):
             tr.valid = True
             tr.created_at = timezone.now()
             tr.save()
-        self.credit_transaction.valid = False
-        self.credit_transaction.reason += " (invalide)"
-        self.credit_transaction.save()
+        if self.credit_transaction:
+            # If the soge credit is deleted while the user is not validated yet,
+            # there is not credit transaction.
+            # There is a credit transaction iff the user declares that no bank account
+            # was opened after the validation of the account.
+            self.credit_transaction.valid = False
+            self.credit_transaction.reason += " (invalide)"
+            self.credit_transaction.save()
         super().delete(**kwargs)
 
     class Meta:
-- 
GitLab