1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-22 23:42:25 +00:00

🐛 Don't break the note

This commit is contained in:
Yohann D'ANELLO 2020-08-09 12:31:06 +02:00
parent 963ba05d01
commit 872456df20
3 changed files with 11 additions and 11 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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);
}