From 872456df207614d4b1be217fbaadd2c6b8389484 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 9 Aug 2020 12:31:06 +0200 Subject: [PATCH] :bug: Don't break the note --- apps/note/api/serializers.py | 4 ++-- apps/note/models/transactions.py | 16 ++++++++-------- static/js/consos.js | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/note/api/serializers.py b/apps/note/api/serializers.py index 4b2ed0a6..1230de4b 100644 --- a/apps/note/api/serializers.py +++ b/apps/note/api/serializers.py @@ -237,9 +237,9 @@ class TransactionPolymorphicSerializer(PolymorphicSerializer): field.set(value) else: setattr(instance, attr, value) - instance.validate(True) + instance.validate() else: - serializer.Meta.model(**attrs).validate(True) + serializer.Meta.model(**attrs).validate() attrs[self.resource_type_field_name] = resource_type return super().validate(attrs) diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index 6cc5174b..762a2bdf 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -164,7 +164,7 @@ class Transaction(PolymorphicModel): models.Index(fields=['destination']), ] - def validate(self, reset=False): + def validate(self): previous_source_balance = self.source.balance previous_dest_balance = self.destination.balance @@ -188,23 +188,19 @@ class Transaction(PolymorphicModel): source_balance = self.source.balance dest_balance = self.destination.balance - if reset: - self.source.balance = previous_source_balance - self.destination.balance = previous_dest_balance - if source_balance > 2147483647 or source_balance < -2147483648\ or dest_balance > 2147483647 or dest_balance < -2147483648: raise ValidationError(_("The note balances must be between - 21 474 836.47 € and 21 474 836.47 €.")) + return source_balance - previous_source_balance, dest_balance - previous_dest_balance + @transaction.atomic def save(self, *args, **kwargs): """ When saving, also transfer money between two notes """ with transaction.atomic(): - self.source.refresh_from_db() - self.destination.refresh_from_db() - self.validate(False) + diff_source, diff_dest = self.validate() if not self.source.is_active or not self.destination.is_active: if 'force_insert' not in kwargs or not kwargs['force_insert']: @@ -229,9 +225,13 @@ class Transaction(PolymorphicModel): self.log("Saved") # Save notes + self.source.refresh_from_db() + self.source.balance += diff_source self.source._force_save = True self.source.save() self.log("Source saved") + self.destination.refresh_from_db() + self.destination.balance += diff_dest self.destination._force_save = True self.destination.save() self.log("Destination saved") diff --git a/static/js/consos.js b/static/js/consos.js index 652b0901..24d0d0fb 100644 --- a/static/js/consos.js +++ b/static/js/consos.js @@ -218,7 +218,7 @@ function consume(source, source_alias, dest, quantity, amount, reason, type, cat addMsg("Attention, La transaction depuis la note " + source_alias + " a été réalisée avec " + "succès, mais la note émettrice " + source_alias + " est en négatif.", "warning", 30000); - if (source.note.membership && source.note.membership.date_end < new Date().toISOString()) + if (source.membership && source.membership.date_end < new Date().toISOString()) addMsg("Attention : la note émettrice " + source.name + " n'est plus adhérente.", "danger", 30000); }