Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use channel_slug in allocate_stocks for getting stocks onlu from available warehouses for given channel
IKarbowiake22fa96674e3
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex e0e427907b..be992c6109 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -386,7 +386,7 @@ def _create_order(OrderLine.objects.bulk_create(order_lines)country_code = checkout_info.get_country()- allocate_stocks(order_lines_info, country_code)+ allocate_stocks(order_lines_info, country_code, checkout_info.channel.slug)# Add gift cards to the orderfor gift_card in checkout.gift_cards.select_for_update():diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.pyindex 75ff66246b..525b5f075b 100644--- a/saleor/graphql/order/mutations/draft_orders.py+++ b/saleor/graphql/order/mutations/draft_orders.py@@ -378,7 +378,7 @@ class DraftOrderComplete(BaseMutation):line=line, quantity=line.quantity, variant=line.variant)try:- allocate_stocks([line_data], country)+ allocate_stocks([line_data], country, order.channel.slug)except InsufficientStock as exc:errors = prepare_insufficient_stock_order_validation_errors(exc)raise ValidationError({"lines": errors})diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.pyindex c1a048b392..e56495c449 100644--- a/saleor/warehouse/management.py+++ b/saleor/warehouse/management.py@@ -17,7 +17,9 @@ StockData = namedtuple("StockData", ["pk", "quantity"])@transaction.atomic-def allocate_stocks(order_lines_info: Iterable["OrderLineData"], country_code: str):+def allocate_stocks(+ order_lines_info: Iterable["OrderLineData"], country_code: str, channel_slug: str+):"""Allocate stocks for given `order_lines` in given country.Function lock for update all stocks and allocations for variants in@@ -37,7 +39,7 @@ def allocate_stocks(order_lines_info: Iterable["OrderLineData"], country_code: sstocks = list(Stock.objects.select_for_update(of=("self",))- .for_country(country_code)+ .for_country(country_code, channel_slug).filter(product_variant__in=variants).order_by("pk").values("id", "product_variant", "pk", "quantity")diff --git a/saleor/warehouse/tests/test_stock_management.py b/saleor/warehouse/tests/test_stock_management.pyindex e43ec60aa7..66a16d159f 100644--- a/saleor/warehouse/tests/test_stock_management.py+++ b/saleor/warehouse/tests/test_stock_management.py@@ -18,13 +18,13 @@ from ..models import AllocationCOUNTRY_CODE = "US"-def test_allocate_stocks(order_line, stock):+def test_allocate_stocks(order_line, stock, channel_USD):stock.quantity = 100stock.save(update_fields=["quantity"])line_data = OrderLineData(line=order_line, variant=order_line.variant, quantity=50)- allocate_stocks([line_data], COUNTRY_CODE)+ allocate_stocks([line_data], COUNTRY_CODE, channel_USD.slug)stock.refresh_from_db()assert stock.quantity == 100@@ -32,7 +32,7 @@ def test_allocate_stocks(order_line, stock):assert allocation.quantity_allocated == 50-def test_allocate_stocks_multiply_lines(order_line, order, product, stock):+def test_allocate_stocks_multiply_lines(order_line, order, product, stock, channel_USD):stock.quantity = 100stock.save(update_fields=["quantity"])@@ -56,7 +56,7 @@ def test_allocate_stocks_multiply_lines(order_line, order, product, stock):line=order_line_2, variant=variant_2, quantity=quantity_2)- allocate_stocks([line_data_1, line_data_2], COUNTRY_CODE)+ allocate_stocks([line_data_1, line_data_2], COUNTRY_CODE, channel_USD.slug)stock.refresh_from_db()assert stock.quantity == 100@@ -68,12 +68,12 @@ def test_allocate_stocks_multiply_lines(order_line, order, product, stock):assert allocation.quantity_allocated == quantity_2-def test_allocate_stock_many_stocks(order_line, variant_with_many_stocks):+def test_allocate_stock_many_stocks(order_line, variant_with_many_stocks, channel_USD):variant = variant_with_many_stocksstocks = variant.stocks.all()line_data = OrderLineData(line=order_line, variant=order_line.variant, quantity=5)- allocate_stocks([line_data], COUNTRY_CODE)+ allocate_stocks([line_data], COUNTRY_CODE, channel_USD.slug)allocations = Allocation.objects.filter(order_line=order_line, stock__in=stocks)assert allocations[0].quantity_allocated == 4@@ -84,13 +84,14 @@ def test_allocate_stock_many_stocks_partially_allocated(order_line,order_line_with_allocation_in_many_stocks,order_line_with_one_allocation,+ channel_USD,):allocated_line = order_line_with_allocation_in_many_stocksvariant = allocated_line.variantstocks = variant.stocks.all()line_data = OrderLineData(line=order_line, variant=order_line.variant, quantity=3)- allocate_stocks([line_data], COUNTRY_CODE)+ allocate_stocks([line_data], COUNTRY_CODE, channel_USD.slug)allocations = Allocation.objects.filter(order_line=order_line, stock__in=stocks)assert allocations[0].quantity_allocated == 1@@ -98,7 +99,7 @@ def test_allocate_stock_many_stocks_partially_allocated(def test_allocate_stock_partially_allocated_insufficient_stocks(- order_line, order_line_with_allocation_in_many_stocks+ order_line, order_line_with_allocation_in_many_stocks, channel_USD):allocated_line = order_line_with_allocation_in_many_stocksvariant = allocated_line.variant@@ -106,20 +107,33 @@ def test_allocate_stock_partially_allocated_insufficient_stocks(line_data = OrderLineData(line=order_line, variant=order_line.variant, quantity=6)with pytest.raises(InsufficientStock):- allocate_stocks([line_data], COUNTRY_CODE)+ allocate_stocks([line_data], COUNTRY_CODE, channel_USD.slug)assert not Allocation.objects.filter(order_line=order_line, stock__in=stocks).exists()-def test_allocate_stock_insufficient_stocks(order_line, variant_with_many_stocks):+def test_allocate_stocks_no_channel_shipping_zones(order_line, stock, channel_USD):+ channel_USD.shipping_zones.clear()++ stock.quantity = 100+ stock.save(update_fields=["quantity"])++ line_data = OrderLineData(line=order_line, variant=order_line.variant, quantity=50)+ with pytest.raises(InsufficientStock):+ allocate_stocks([line_data], COUNTRY_CODE, channel_USD.slug)+++def test_allocate_stock_insufficient_stocks(+ order_line, variant_with_many_stocks, channel_USD+):variant = variant_with_many_stocksstocks = variant.stocks.all()line_data = OrderLineData(line=order_line, variant=order_line.variant, quantity=10)with pytest.raises(InsufficientStock):- allocate_stocks([line_data], COUNTRY_CODE)+ allocate_stocks([line_data], COUNTRY_CODE, channel_USD.slug)assert not Allocation.objects.filter(order_line=order_line, stock__in=stocks@@ -127,7 +141,7 @@ def test_allocate_stock_insufficient_stocks(order_line, variant_with_many_stocksdef test_allocate_stock_insufficient_stocks_for_multiply_lines(- order_line, variant_with_many_stocks, product+ order_line, variant_with_many_stocks, product, channel_USD):variant = variant_with_many_stocksstocks = variant.stocks.all()@@ -152,7 +166,7 @@ def test_allocate_stock_insufficient_stocks_for_multiply_lines()with pytest.raises(InsufficientStock) as exc:- allocate_stocks([line_data_1, line_data_2], COUNTRY_CODE)+ allocate_stocks([line_data_1, line_data_2], COUNTRY_CODE, channel_USD.slug)assert set(item.variant for item in exc._excinfo[1].items) == {variant, variant_2}