ref: remove signals in favor of save() override
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 \
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user