From 4ed76b85de363aa29300418c74e93c2eec1db3dd Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Sun, 6 Jul 2025 19:21:15 +0300 Subject: [PATCH] Features: 1) Add pre_save signal handling for Transaction model; 2) Enhance error status handling within process_transaction_changes. Fixes: 1) Correct type conversion for process status to a string for consistency. Extra: 1) Adjust signal usage from post_save to pre_save for improved transaction handling. --- payments/signals.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/payments/signals.py b/payments/signals.py index 102654af..ffd1111f 100644 --- a/payments/signals.py +++ b/payments/signals.py @@ -1,4 +1,4 @@ -from django.db.models.signals import post_save +from django.db.models.signals import post_save, pre_save from django.dispatch import receiver from payments.gateways import AbstractGateway @@ -13,7 +13,7 @@ def create_balance_on_user_creation_signal(instance, created, **_kwargs): Balance.objects.create(user=instance) -@receiver(post_save, sender=Transaction) +@receiver(pre_save, sender=Transaction) def process_transaction_changes(instance, created, **_kwargs): if created: try: @@ -26,9 +26,9 @@ def process_transaction_changes(instance, created, **_kwargs): gateway = AbstractGateway() gateway.process_transaction(instance) except Exception as e: - instance.process = {"status": "NOGATEWAY", "error": str(e)} + instance.process = {"status": "ERRORED", "error": str(e)} if not created: - status = instance.process.get("status", "").lower() + status = str(instance.process.get("status", "")).lower() success = instance.process.get("success", False) if ("success" in status or success) and (instance.process.get("notify", False)):