Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix review remarks
fowczrek8a321b0dbcf8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7349f1616f..99a8e2dc09 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -98,6 +98,7 @@ All notable, unreleased changes to this project will be documented in this file.- Use AccountErrorCode.INVALID_CREDENTIALS instead of INVALID_PASSWORD - #5495 by @koradon- Add tests for pagination - #5468 by @koradon- Add job abstract model and interface - #5510 by @IKarbowiak+- Refactor implementation of allocation - #5445 by @fowczarek## 2.9.0diff --git a/saleor/core/exceptions.py b/saleor/core/exceptions.pyindex 987f485d9f..03829f737c 100644--- a/saleor/core/exceptions.py+++ b/saleor/core/exceptions.py@@ -8,6 +8,16 @@ class InsufficientStock(Exception):self.code = CheckoutErrorCode.INSUFFICIENT_STOCK+class AllocationError(Exception):+ def __init__(self, order_line, quantity):+ super().__init__(+ f"Can't deallocate {quantity} for variant: {order_line.variant}"+ f" in order: {order_line.order}"+ )+ self.order_line = order_line+ self.quantity = quantity++class ReadOnlyException(Exception):def __init__(self, msg=None):if msg is None:diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex b19ad90bc4..a0f30d20fd 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -478,15 +478,15 @@ def create_order_lines(order, discounts, how_many=10):lines = OrderLine.objects.bulk_create(lines)manager = get_plugins_manager()country = order.shipping_method.shipping_zone.countries[0]+ warehouses = Warehouse.objects.filter(+ shipping_zones__countries__contains=country+ ).order_by("?")+ warehouse_iter = itertools.cycle(warehouses)for line in lines:unit_price = manager.calculate_order_line_unit(line)line.unit_price = unit_priceline.tax_rate = unit_price.tax / unit_price.net- warehouse = (- Warehouse.objects.filter(shipping_zones__countries__contains=country)- .order_by("?")- .first()- )+ warehouse = next(warehouse_iter)increase_stock(line, warehouse, line.quantity, allocate=True)OrderLine.objects.bulk_update(lines,diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex d26f8bc37e..8e05689fc6 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -10,9 +10,9 @@ from ..core.taxes import zero_moneyfrom ..core.weight import zero_weightfrom ..discount.models import NotApplicable, Voucher, VoucherTypefrom ..discount.utils import get_products_voucher_discount, validate_voucher_in_order-from ..plugins.manager import get_plugins_managerfrom ..order import OrderStatusfrom ..order.models import Order, OrderLine+from ..plugins.manager import get_plugins_managerfrom ..product.utils.digital_products import get_default_digital_content_settingsfrom ..shipping.models import ShippingMethodfrom ..warehouse.management import deallocate_stock, increase_stock@@ -259,6 +259,9 @@ def delete_order_line(line):def restock_order_lines(order):"""Return ordered products to corresponding stocks."""country = get_order_country(order)+ default_warehouse = Warehouse.objects.filter(+ shipping_zones__countries__contains=country+ ).first()for line in order:if line.variant and line.variant.track_inventory:@@ -266,12 +269,9 @@ def restock_order_lines(order):deallocate_stock(line, line.quantity_unfulfilled)if line.quantity_fulfilled > 0:allocation = line.allocations.first()- if allocation:- warehouse = allocation.stock.warehouse- else:- warehouse = Warehouse.objects.filter(- shipping_zones__countries__contains=country- ).first()+ warehouse = (+ allocation.stock.warehouse if allocation else default_warehouse+ )increase_stock(line, warehouse, line.quantity_fulfilled)if line.quantity_fulfilled > 0:@@ -282,15 +282,14 @@ def restock_order_lines(order):def restock_fulfillment_lines(fulfillment):"""Return fulfilled products to corresponding stocks."""country = get_order_country(fulfillment.order)+ default_warehouse = Warehouse.objects.filter(+ shipping_zones__countries__contains=country+ ).first()+for line in fulfillment:if line.order_line.variant and line.order_line.variant.track_inventory:allocation = line.order_line.allocations.first()- if allocation:- warehouse = allocation.stock.warehouse- else:- warehouse = Warehouse.objects.filter(- shipping_zones__countries__contains=country- ).first()+ warehouse = allocation.stock.warehouse if allocation else default_warehouseincrease_stock(line.order_line, warehouse, line.quantity, allocate=True)diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.pyindex e160b4e814..53048864c1 100644--- a/saleor/warehouse/management.py+++ b/saleor/warehouse/management.py@@ -3,7 +3,7 @@ from typing import TYPE_CHECKINGfrom django.db import transactionfrom django.db.models import F, Sum-from ..core.exceptions import InsufficientStock+from ..core.exceptions import AllocationError, InsufficientStockfrom .models import Allocation, Stock, Warehouseif TYPE_CHECKING:@@ -63,8 +63,9 @@ def allocate_stock(quantity_allocated += quantity_to_allocateif quantity_allocated == quantity:Allocation.objects.bulk_create(allocations)- return None- raise InsufficientStock(order_line.variant)+ break+ if not quantity_allocated == quantity:+ raise InsufficientStock(order_line.variant)@transaction.atomic@@ -94,11 +95,9 @@ def deallocate_stock(order_line: "OrderLine", quantity: int):quantity_dealocated += quantity_to_deallocateif quantity_dealocated == quantity:Allocation.objects.bulk_update(allocations, ["quantity_allocated"])- return None- raise Exception(- f"Can't deallocate {quantity} for variant: {order_line.variant}"- f" in order: {order_line.order}"- )+ break+ if not quantity_dealocated == quantity:+ raise AllocationError(order_line, quantity)@transaction.atomic@@ -176,11 +175,9 @@ def decrease_stock(order_line: "OrderLine", quantity: int):if quantity_decreased == quantity:Allocation.objects.bulk_update(allocations, ["quantity_allocated"])Stock.objects.bulk_update(updated_stocks, ["quantity"])- return None- raise Exception(- f"Can't decrease {quantity} for variant: {order_line.variant}"- f" in order: {order_line.order}"- )+ break+ if not quantity_decreased == quantity:+ raise AllocationError(order_line, quantity)@transaction.atomicdiff --git a/tests/test_stock_management.py b/tests/test_stock_management.pyindex ba76c4cff2..54fa286845 100644--- a/tests/test_stock_management.py+++ b/tests/test_stock_management.py@@ -207,22 +207,6 @@ def test_decrease_stock_many_allocations(order_line_with_allocation_in_many_stocdecrease_stock(order_line, 3)allocations = order_line.allocations.all()- if not allocations[0].quantity_allocated == 0:- import ipdb-- ipdb.set_trace()- if not allocations[0].stock.quantity == 2:- import ipdb-- ipdb.set_trace()- if not allocations[1].quantity_allocated == 0:- import ipdb-- ipdb.set_trace()- if not allocations[1].stock.quantity == 2:- import ipdb-- ipdb.set_trace()assert allocations[0].quantity_allocated == 0assert allocations[0].stock.quantity == 2assert allocations[1].quantity_allocated == 0@@ -237,22 +221,6 @@ def test_decrease_stock_many_allocations_partially(decrease_stock(order_line, 2)allocations = order_line.allocations.all()- if not allocations[0].quantity_allocated == 0:- import ipdb-- ipdb.set_trace()- if not allocations[0].stock.quantity == 2:- import ipdb-- ipdb.set_trace()- if not allocations[1].quantity_allocated == 1:- import ipdb-- ipdb.set_trace()- if not allocations[1].stock.quantity == 3:- import ipdb-- ipdb.set_trace()assert allocations[0].quantity_allocated == 0assert allocations[0].stock.quantity == 2assert allocations[1].quantity_allocated == 1