Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Require channel_slug in check_stock_quantity_bulk method
IKarbowiakd7f2f977e4b3
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex a582bfb69e..e0e427907b 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -234,7 +234,9 @@ def _create_lines_for_order(for variant_translation in variants_translation}- check_stock_quantity_bulk(variants, country_code, quantities)+ check_stock_quantity_bulk(+ variants, country_code, quantities, checkout_info.channel.slug+ )return [_create_line_for_order(diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex a226f3f11f..5558581879 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -108,7 +108,7 @@ def add_variant_to_checkout(return checkout-def add_variants_to_checkout(checkout, variants, quantities):+def add_variants_to_checkout(checkout, variants, quantities, channel_slug):"""Add variants to checkout.Suitable for new checkouts as it always creates new checkout lines without checking@@ -117,7 +117,7 @@ def add_variants_to_checkout(checkout, variants, quantities):# check quantitiescountry_code = checkout.get_country()- check_stock_quantity_bulk(variants, country_code, quantities)+ check_stock_quantity_bulk(variants, country_code, quantities, channel_slug)channel_listings = product_models.ProductChannelListing.objects.filter(channel_id=checkout.channel.id,diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 1f640f79ca..d0680ea05a 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -359,6 +359,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):@classmethod@transaction.atomic()def save(cls, info, instance: models.Checkout, cleaned_input):+ channel = cleaned_input["channel"]# Create the checkout objectinstance.save()@@ -371,7 +372,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):quantities = cleaned_input.get("quantities")if variants and quantities:try:- add_variants_to_checkout(instance, variants, quantities)+ add_variants_to_checkout(instance, variants, quantities, channel.slug)except InsufficientStock as exc:error = prepare_insufficient_stock_checkout_validation_error(exc)raise ValidationError({"lines": error})diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.pyindex 6fc72ef649..342264e3b9 100644--- a/saleor/warehouse/availability.py+++ b/saleor/warehouse/availability.py@@ -1,5 +1,5 @@from collections import defaultdict-from typing import TYPE_CHECKING, List+from typing import TYPE_CHECKING, Dict, Iterable, Listfrom django.db.models import Sumfrom django.db.models.functions import Coalesce@@ -37,7 +37,12 @@ def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity:raise InsufficientStock([InsufficientStockData(variant=variant)])-def check_stock_quantity_bulk(variants, country_code, quantities, channel_slug=None):+def check_stock_quantity_bulk(+ variants: Iterable["ProductVariant"],+ country_code: str,+ quantities: Iterable[int],+ channel_slug: str,+):"""Validate if there is stock available for given variants in given country.:raises InsufficientStock: when there is not enough items in stock for a variant.@@ -48,14 +53,16 @@ def check_stock_quantity_bulk(variants, country_code, quantities, channel_slug=N.annotate_available_quantity())- variant_stocks = defaultdict(list)+ variant_stocks: Dict[int, List[Stock]] = defaultdict(list)for stock in all_variants_stocks:variant_stocks[stock.product_variant_id].append(stock)insufficient_stocks: List[InsufficientStockData] = []for variant, quantity in zip(variants, quantities):stocks = variant_stocks.get(variant.pk, [])- available_quantity = sum([stock.available_quantity for stock in stocks])+ available_quantity = sum(+ [stock.available_quantity for stock in stocks] # type: ignore+ )if not stocks:insufficient_stocks.append(diff --git a/saleor/warehouse/tests/test_stock_availability.py b/saleor/warehouse/tests/test_stock_availability.pyindex a794bccfa4..aa8d4c9e07 100644--- a/saleor/warehouse/tests/test_stock_availability.py+++ b/saleor/warehouse/tests/test_stock_availability.py@@ -45,7 +45,7 @@ def test_check_stock_quantity_without_one_stock(variant_with_many_stocks):assert check_stock_quantity(variant_with_many_stocks, COUNTRY_CODE, 4) is None-def test_check_stock_quantity_bulk(variant_with_many_stocks):+def test_check_stock_quantity_bulk(variant_with_many_stocks, channel_USD):variant = variant_with_many_stockscountry_code = "US"available_quantity = _get_available_quantity(variant.stocks.all())@@ -53,7 +53,7 @@ def test_check_stock_quantity_bulk(variant_with_many_stocks):# test that it doesn't raise error for available quantityassert (check_stock_quantity_bulk(- [variant_with_many_stocks], country_code, [available_quantity]+ [variant_with_many_stocks], country_code, [available_quantity], channel_USD)is None)@@ -61,12 +61,29 @@ def test_check_stock_quantity_bulk(variant_with_many_stocks):# test that it raises an error for exceeded quantitywith pytest.raises(InsufficientStock):check_stock_quantity_bulk(- [variant_with_many_stocks], country_code, [available_quantity + 1]+ [variant_with_many_stocks],+ country_code,+ [available_quantity + 1],+ channel_USD,)# test that it raises an error if no stocks are foundvariant.stocks.all().delete()with pytest.raises(InsufficientStock):check_stock_quantity_bulk(- [variant_with_many_stocks], country_code, [available_quantity]+ [variant_with_many_stocks], country_code, [available_quantity], channel_USD+ )+++def test_check_stock_quantity_bulk_no_channel_shipping_zones(+ variant_with_many_stocks, channel_USD+):+ variant = variant_with_many_stocks+ country_code = "US"+ available_quantity = _get_available_quantity(variant.stocks.all())+ channel_USD.shipping_zones.clear()++ with pytest.raises(InsufficientStock):+ check_stock_quantity_bulk(+ [variant_with_many_stocks], country_code, [available_quantity], channel_USD)