ref: remove signals in favor of save() override
This commit is contained in:
@@ -21,10 +21,10 @@ class Account(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.user}'s Konto"
|
return f"{self.user}'s Konto"
|
||||||
|
|
||||||
# def change_balance(self, amount):
|
def update_balance(self, amount: 'Decimal') -> None:
|
||||||
# self.balance = self.balance - amount
|
if amount:
|
||||||
# self.save()
|
self.balance += amount
|
||||||
# return True
|
self.save()
|
||||||
|
|
||||||
# def lock(self):
|
# def lock(self):
|
||||||
# self.locked = True
|
# self.locked = True
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from django.urls import reverse
|
|||||||
|
|
||||||
from app.base.forms.fields import DateTimeField, TextField
|
from app.base.forms.fields import DateTimeField, TextField
|
||||||
from app.base.forms.utils import datetime_now
|
from app.base.forms.utils import datetime_now
|
||||||
|
from app.base.models.transaction import Transaction
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -42,11 +43,14 @@ class Booking(models.Model):
|
|||||||
if self.end_time else '')
|
if self.end_time else '')
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
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
|
prev = Booking.objects.get(pk=self.pk) if self.pk else None
|
||||||
|
|
||||||
rv = super().save(*args, **kwargs)
|
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:
|
if prev and prev.user != self.user:
|
||||||
prev.user.update_last_visit(None)
|
prev.user.update_last_visit(None)
|
||||||
if not prev or prev.user != self.user or \
|
if not prev or prev.user != self.user or \
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ class Person(models.Model):
|
|||||||
if self._state.adding:
|
if self._state.adding:
|
||||||
self.created = date.today()
|
self.created = date.today()
|
||||||
self.last_visit = date.today()
|
self.last_visit = date.today()
|
||||||
|
Account.objects.create(user=self)
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -38,4 +38,37 @@ class Transaction(models.Model):
|
|||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if self._state.adding:
|
if self._state.adding:
|
||||||
self.time_stamp = datetime_now()
|
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)
|
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,
|
||||||
|
)
|
||||||
|
|||||||
@@ -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
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -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,
|
|
||||||
)
|
|
||||||
@@ -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)
|
|
||||||
@@ -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()
|
|
||||||
Reference in New Issue
Block a user