Features: 1) Switch transaction signal from pre_save to post_save for processing changes; 2) Add created parameter handling for improved transaction processing logic;

Fixes: 1) Remove unused `pre_save` signal import;

Extra: 1) Update conditional logic for `process_transaction_changes` to improve clarity and error handling; 2) Minor formatting adjustments.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-06 23:30:06 +03:00
parent 220e50c01b
commit ea236743ae

View file

@ -1,4 +1,4 @@
from django.db.models.signals import post_save, pre_save
from django.db.models.signals import post_save
from django.dispatch import receiver
from payments.gateways import AbstractGateway
@ -13,11 +13,9 @@ def create_balance_on_user_creation_signal(instance, created, **_kwargs):
Balance.objects.create(user=instance)
@receiver(pre_save, sender=Transaction)
def process_transaction_changes(instance, **_kwargs):
is_new = instance.pk is None
if is_new:
@receiver(post_save, sender=Transaction)
def process_transaction_changes(instance, created, **_kwargs):
if created:
try:
match instance.process.get("gateway", "default"):
case "gateway":
@ -29,7 +27,7 @@ def process_transaction_changes(instance, **_kwargs):
gateway.process_transaction(instance)
except Exception as e:
instance.process = {"status": "ERRORED", "error": str(e)}
else:
if not created:
status = str(instance.process.get("status", "")).lower()
success = instance.process.get("success", False)