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:
parent
aa643f6773
commit
3427c6ad22
2 changed files with 14 additions and 4 deletions
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue