schon/payments/models.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

69 lines
2.4 KiB
Python

from constance import config
from django.contrib.postgres.indexes import GinIndex
from django.db.models import CASCADE, CharField, FloatField, ForeignKey, JSONField, OneToOneField, QuerySet
from django.utils.translation import gettext_lazy as _
from core.abstract import NiceModel
class Transaction(NiceModel):
amount = FloatField(null=False, blank=False)
balance = ForeignKey("payments.Balance", on_delete=CASCADE, blank=True, null=True, related_name="transactions")
currency = CharField(max_length=3, null=False, blank=False)
payment_method = CharField(max_length=20, null=True, blank=True)
order = ForeignKey(
"core.Order",
on_delete=CASCADE,
blank=True,
null=True,
help_text=_("order to process after paid"),
related_name="payments_transactions",
)
process = JSONField(verbose_name=_("processing details"), default=dict)
def __str__(self):
return (
f"{self.balance.user.email} | {self.amount}"
if self.balance
else f"{self.order.attributes.get('customer_email')} | {self.amount}"
)
def save(self, **kwargs):
if self.amount != 0.0 and (
(config.PAYMENT_GATEWAY_MINIMUM <= self.amount <= config.PAYMENT_GATEWAY_MAXIMUM)
or (config.PAYMENT_GATEWAY_MINIMUM == 0 and config.PAYMENT_GATEWAY_MAXIMUM == 0)
):
if len(str(self.amount).split(".")[1]) > 2:
self.amount = round(self.amount, 2)
super().save(**kwargs)
return self
raise ValueError(
_(f"transaction amount must fit into {config.PAYMENT_GATEWAY_MINIMUM}-{config.PAYMENT_GATEWAY_MAXIMUM}")
)
class Meta:
verbose_name = _("transaction")
verbose_name_plural = _("transactions")
indexes = [
GinIndex(fields=["process"]),
]
class Balance(NiceModel):
amount = FloatField(null=False, blank=False, default=0)
user = OneToOneField(
to="vibes_auth.User", on_delete=CASCADE, blank=True, null=True, related_name="payments_balance"
)
transactions: QuerySet["Transaction"]
def __str__(self):
return f"{self.user.email} | {self.amount}"
class Meta:
verbose_name = _("balance")
verbose_name_plural = _("balances")
def save(self, **kwargs):
if self.amount != 0.0 and len(str(self.amount).split(".")[1]) > 2:
self.amount = round(self.amount, 2)
super().save(**kwargs)