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.exceptions import BadRequest, ValidationError
|
||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||
from django.db import transaction
|
||||
from django.db.models import (
|
||||
CASCADE,
|
||||
PROTECT,
|
||||
|
|
@ -1617,9 +1618,10 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi
|
|||
case "balance":
|
||||
if order.user.payments_balance.amount < amount:
|
||||
raise NotEnoughMoneyError(_("insufficient funds to complete the order"))
|
||||
with transaction.atomic():
|
||||
order.status = "CREATED"
|
||||
order.buy_time = timezone.now()
|
||||
order.order_products.all().update(status="DELIVERING")
|
||||
order.update_order_products_statuses("DELIVERING")
|
||||
order.save()
|
||||
return order
|
||||
case "payment":
|
||||
|
|
@ -1708,6 +1710,9 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): # type: ignore [mi
|
|||
self.status = "FINISHED"
|
||||
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]]):
|
||||
for product in products:
|
||||
self.add_product(
|
||||
|
|
|
|||
Loading…
Reference in a new issue