From 3427c6ad22a8a6473c36551a31d3aff62d13df80 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Tue, 8 Jul 2025 14:56:38 +0300 Subject: [PATCH] Features: 1) Add validation for empty `attributes` in `Order` model; 2) Introduce `final_amount` retrieval for streamlined promocode application. Fixes: 1) Update `logger.debug` call to improve readability; 2) Correct `promocode` key to `promocode_uuid` in order attributes dictionary to ensure consistent naming. Extra: 1) Minor cleanup and code comments in `Order` processing logic. --- core/models.py | 16 +++++++++++++--- payments/views.py | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/models.py b/core/models.py index 7fb82a04..eb05e54a 100644 --- a/core/models.py +++ b/core/models.py @@ -1282,11 +1282,11 @@ class PromoCode(ExportModelOperationsMixin("promocode"), NiceModel): # type: ig if self.discount_type == "percent": promo_amount -= round(promo_amount * (float(self.discount_percent) / 100), 2) # type: ignore [arg-type] - order.attributes.update({"promocode": str(self.uuid), "final_price": promo_amount}) + order.attributes.update({"promocode_uuid": str(self.uuid), "final_price": promo_amount}) order.save() elif self.discount_type == "amount": promo_amount -= round(float(self.discount_amount), 2) # type: ignore [arg-type] - order.attributes.update({"promocode": str(self.uuid), "final_price": promo_amount}) + order.attributes.update({"promocode_uuid": str(self.uuid), "final_price": promo_amount}) order.save() else: raise ValueError(_(f"invalid discount type for promocode {self.uuid}")) @@ -1572,6 +1572,10 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi chosen_products: list | None = None, ) -> Self | Transaction | None: order = self + + if not self.attributes: + self.attributes = {} + if chosen_products: order = Order.objects.create(status="MOMENTAL", user=self.user) order.bulk_add_products(chosen_products) @@ -1595,7 +1599,13 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi if force_payment: force = "payment" - amount = order.apply_promocode(promocode_uuid) if promocode_uuid else order.total_price + amount = self.attributes.get("final_amount") or order.total_price + + if self.attributes.get("promocode_uuid") and not self.attributes.get("final_amount"): + amount = order.apply_promocode(self.attributes.get("promocode_uuid")) + + if promocode_uuid and not self.attributes.get("final_amount"): + amount = order.apply_promocode(promocode_uuid) if not order.user: raise ValueError(_("you cannot buy an order without a user")) diff --git a/payments/views.py b/payments/views.py index 49b60ab5..ef83ba09 100644 --- a/payments/views.py +++ b/payments/views.py @@ -68,7 +68,7 @@ class CallbackAPIView(APIView): """ def post(self, request, *args, **kwargs): - logger.debug(request.__dict__) + logger.debug(f"{request.__dict__}\n") try: gateway = kwargs.get("gateway", "") # noinspection PyUnreachableCode