From ea236743aec0a0f937354683dd9b934772e22169 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Sun, 6 Jul 2025 23:30:06 +0300 Subject: [PATCH] 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. --- payments/signals.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/payments/signals.py b/payments/signals.py index 6853f288..1dafc8f5 100644 --- a/payments/signals.py +++ b/payments/signals.py @@ -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)