ref: remove signals in favor of save() override
This commit is contained in:
@@ -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