ref: remove signals in favor of save() override

This commit is contained in:
relikd
2023-06-08 14:36:55 +02:00
parent cbb543abce
commit e2ebf30f2a
8 changed files with 43 additions and 78 deletions

View File

@@ -21,10 +21,10 @@ class Account(models.Model):
def __str__(self):
return f"{self.user}'s Konto"
# def change_balance(self, amount):
# self.balance = self.balance - amount
# self.save()
# return True
def update_balance(self, amount: 'Decimal') -> None:
if amount:
self.balance += amount
self.save()
# def lock(self):
# self.locked = True

View File

@@ -5,6 +5,7 @@ from django.urls import reverse
from app.base.forms.fields import DateTimeField, TextField
from app.base.forms.utils import datetime_now
from app.base.models.transaction import Transaction
from typing import TYPE_CHECKING
if TYPE_CHECKING:
@@ -42,11 +43,14 @@ class Booking(models.Model):
if self.end_time else '')
def save(self, *args, **kwargs):
# update last_visit time of all involved persons
prev = Booking.objects.get(pk=self.pk) if self.pk else None
rv = super().save(*args, **kwargs)
# update Transaction
Transaction.upsertFromBooking(self)
# update last_visit time of all involved persons
if prev and prev.user != self.user:
prev.user.update_last_visit(None)
if not prev or prev.user != self.user or \

View File

@@ -59,6 +59,7 @@ class Person(models.Model):
if self._state.adding:
self.created = date.today()
self.last_visit = date.today()
Account.objects.create(user=self)
return super().save(*args, **kwargs)
@property

View File

@@ -38,4 +38,37 @@ class Transaction(models.Model):
def save(self, *args, **kwargs):
if self._state.adding:
self.time_stamp = datetime_now()
self.account.update_balance(self.amount)
elif self.pk: # basically an "else", but just to be sure
prev = Transaction.objects.get(pk=self.pk)
self.account.update_balance(self.amount - prev.amount)
return super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
self.account.update_balance(-self.amount)
return super().delete(*args, **kwargs)
@staticmethod
def upsertFromBooking(booking: 'Booking'):
amount = booking.calculated_price
should_exist = booking.end_time and amount
description = f'{booking.type.label} ({booking.duration or 0} Min)'
# Create or update existing Transaction
transaction = Transaction.objects.filter(booking=booking).first()
if transaction:
if should_exist:
transaction.amount = -amount
transaction.description = description
transaction.save()
else:
transaction.delete()
elif should_exist:
Transaction.objects.create(
account=booking.user.account,
amount=-amount,
description=description,
booking=booking,
)

View File

@@ -1,7 +0,0 @@
from app.base.signals.booking import booking_post_save
from app.base.signals.person import person_post_save
from app.base.signals.transaction import (
transaction_pre_save,
transaction_post_save,
transaction_pre_delete
)

View File

@@ -1,29 +0,0 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from app.base.models import Transaction, Booking
@receiver(post_save, sender=Booking)
def booking_post_save(sender, instance: Booking, created: bool, **kwargs):
amount = instance.calculated_price
with_transaction = instance.end_time and amount
description = f'{instance.type.label} ({instance.duration or 0} Min)'
# Create or update existing Transaction
transaction = Transaction.objects.filter(booking=instance).first()
if transaction:
if with_transaction:
transaction.amount = -amount
transaction.description = description
transaction.save()
else:
transaction.delete()
elif with_transaction:
Transaction.objects.create(
account=instance.user.account,
amount=-amount,
description=description,
booking=instance,
)

View File

@@ -1,9 +0,0 @@
from django.dispatch import receiver
from django.db.models.signals import post_save
from app.base.models import Person, Account
@receiver(post_save, sender=Person)
def person_post_save(sender, instance: Person, created: bool, **kwargs):
if created:
Account.objects.create(user=instance)

View File

@@ -1,28 +0,0 @@
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save, pre_delete
from app.base.models import Transaction
@receiver(pre_save, sender=Transaction)
def transaction_pre_save(sender, instance: Transaction, **kwargs):
if instance.pk:
pre_edit = Transaction.objects.get(pk=instance.pk)
if pre_edit.amount != instance.amount:
delta = pre_edit.amount - instance.amount
account = instance.account
account.balance = account.balance - delta
account.save()
@receiver(post_save, sender=Transaction)
def transaction_post_save(sender, instance: Transaction, created, **kwargs):
if created:
instance.account.balance += instance.amount
instance.account.save()
@receiver(pre_delete, sender=Transaction)
def transaction_pre_delete(sender, instance: Transaction, **kwargs):
instance.account.balance -= instance.amount
instance.account.save()