Features: 1) Add update_order_products_statuses method for bulk updating product statuses; 2) Wrap order creation logic in atomic transaction to ensure integrity.
Fixes: 1) Add missing `transaction` import in `core/models.py`. Extra: 1) Refactor order product status updates to use the new method; 2) Enhance readability and maintainability of order processing logic.
This commit is contained in:
parent
c2fd267374
commit
171f2cb20f
1 changed files with 10 additions and 5 deletions
|
|
@ -9,6 +9,7 @@ from django.contrib.postgres.indexes import GinIndex
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import BadRequest, ValidationError
|
from django.core.exceptions import BadRequest, ValidationError
|
||||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||||
|
from django.db import transaction
|
||||||
from django.db.models import (
|
from django.db.models import (
|
||||||
CASCADE,
|
CASCADE,
|
||||||
PROTECT,
|
PROTECT,
|
||||||
|
|
@ -1617,11 +1618,12 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi
|
||||||
case "balance":
|
case "balance":
|
||||||
if order.user.payments_balance.amount < amount:
|
if order.user.payments_balance.amount < amount:
|
||||||
raise NotEnoughMoneyError(_("insufficient funds to complete the order"))
|
raise NotEnoughMoneyError(_("insufficient funds to complete the order"))
|
||||||
order.status = "CREATED"
|
with transaction.atomic():
|
||||||
order.buy_time = timezone.now()
|
order.status = "CREATED"
|
||||||
order.order_products.all().update(status="DELIVERING")
|
order.buy_time = timezone.now()
|
||||||
order.save()
|
order.update_order_products_statuses("DELIVERING")
|
||||||
return order
|
order.save()
|
||||||
|
return order
|
||||||
case "payment":
|
case "payment":
|
||||||
order.status = "PAYMENT"
|
order.status = "PAYMENT"
|
||||||
order.save()
|
order.save()
|
||||||
|
|
@ -1708,6 +1710,9 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi
|
||||||
self.status = "FINISHED"
|
self.status = "FINISHED"
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
def update_order_products_statuses(self, status: str = "PENDING"):
|
||||||
|
self.order_products.update(status=status)
|
||||||
|
|
||||||
def bulk_add_products(self, products: list[dict[str, Any]]):
|
def bulk_add_products(self, products: list[dict[str, Any]]):
|
||||||
for product in products:
|
for product in products:
|
||||||
self.add_product(
|
self.add_product(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue