schon/payments/signals.py
Egor fureunoir Gorbunov 330177f6e4 Features: 1) Add # noinspection PyUnusedLocal annotations to various viewsets, filters, and migrations to suppress unnecessary warnings; 2) Improve post method in BusinessPurchaseView to handle exceptions and inactive orders gracefully; 3) Refactor resolve_transactions and related resolvers in Graphene to include more specific typing hints; 4) Include defensive coding for attributes in several models to ensure type safety.
Fixes: 1) Correct default manager assignment in `Product` model; 2) Address potential division by zero in `AbsoluteFTPStorage`; 3) Ensure proper exception handling for missing `order` attributes in CRM gateway methods; 4) Rectify inaccurate string formatting for `Transaction` `__str__` method.

Extra: Refactor various minor code style issues, including formatting corrections in the README, alignment in the emailing utility, and suppressed pycharm-specific inspections; clean up unused imports across files; enhance error messaging consistency.
2025-10-01 17:26:07 +03:00

42 lines
1.5 KiB
Python

import logging
import traceback
from django.db.models.signals import post_save
from django.dispatch import receiver
from payments.gateways import AbstractGateway
from payments.models import Balance, Transaction
from payments.utils.emailing import balance_deposit_email
from vibes_auth.models import User
logger = logging.getLogger("django")
@receiver(post_save, sender=User)
def create_balance_on_user_creation_signal(instance, created, **_kwargs):
if created:
Balance.objects.create(user=instance)
@receiver(post_save, sender=Transaction)
def process_transaction_changes(instance, created, **_kwargs):
if created:
try:
gateway = None
match instance.process.get("gateway", "default"):
case "gateway":
gateway = AbstractGateway()
case "default":
gateway = AbstractGateway()
case _:
gateway = AbstractGateway()
gateway.process_transaction(instance)
except Exception as e:
instance.process = {"status": "ERRORED", "error": str(e)}
logger.error(f"Error processing transaction {instance.uuid}: {e}\n{traceback.format_exc()}")
if not created:
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)):
balance_deposit_email.delay(instance.uuid)