From 220e50c01b8ad02d1bd650257b16bcc0f5e8238a Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Sun, 6 Jul 2025 23:17:23 +0300 Subject: [PATCH] Features: 1) Restructure transaction processing to handle unsaved instances using `pk` check; 2) Instantiate `AbstractGateway` to prevent potential shared reference issues; Fixes: 1) Correct logic to properly detect new transactions for processing; Extra: 1) Minor code tidy-up with consistent case handling; --- payments/signals.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/payments/signals.py b/payments/signals.py index ffd1111f..6853f288 100644 --- a/payments/signals.py +++ b/payments/signals.py @@ -14,20 +14,22 @@ def create_balance_on_user_creation_signal(instance, created, **_kwargs): @receiver(pre_save, sender=Transaction) -def process_transaction_changes(instance, created, **_kwargs): - if created: +def process_transaction_changes(instance, **_kwargs): + is_new = instance.pk is None + + if is_new: try: match instance.process.get("gateway", "default"): case "gateway": - gateway = AbstractGateway + gateway = AbstractGateway() case "default": - gateway = AbstractGateway + gateway = AbstractGateway() case _: gateway = AbstractGateway() gateway.process_transaction(instance) except Exception as e: instance.process = {"status": "ERRORED", "error": str(e)} - if not created: + else: status = str(instance.process.get("status", "")).lower() success = instance.process.get("success", False)