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.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-08 14:56:38 +03:00
parent aa643f6773
commit 3427c6ad22
2 changed files with 14 additions and 4 deletions

View file

@ -1282,11 +1282,11 @@ class PromoCode(ExportModelOperationsMixin("promocode"), NiceModel): # type: ig
if self.discount_type == "percent": if self.discount_type == "percent":
promo_amount -= round(promo_amount * (float(self.discount_percent) / 100), 2) # type: ignore [arg-type] 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() order.save()
elif self.discount_type == "amount": elif self.discount_type == "amount":
promo_amount -= round(float(self.discount_amount), 2) # type: ignore [arg-type] 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() order.save()
else: else:
raise ValueError(_(f"invalid discount type for promocode {self.uuid}")) 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, chosen_products: list | None = None,
) -> Self | Transaction | None: ) -> Self | Transaction | None:
order = self order = self
if not self.attributes:
self.attributes = {}
if chosen_products: if chosen_products:
order = Order.objects.create(status="MOMENTAL", user=self.user) order = Order.objects.create(status="MOMENTAL", user=self.user)
order.bulk_add_products(chosen_products) order.bulk_add_products(chosen_products)
@ -1595,7 +1599,13 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi
if force_payment: if force_payment:
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: if not order.user:
raise ValueError(_("you cannot buy an order without a user")) raise ValueError(_("you cannot buy an order without a user"))

View file

@ -68,7 +68,7 @@ class CallbackAPIView(APIView):
""" """
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
logger.debug(request.__dict__) logger.debug(f"{request.__dict__}\n")
try: try:
gateway = kwargs.get("gateway", "") gateway = kwargs.get("gateway", "")
# noinspection PyUnreachableCode # noinspection PyUnreachableCode