Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add option to get only stock available in given country
Mira Szewczyk174d8896c497
diff --git a/saleor/stock/models.py b/saleor/stock/models.pyindex 10193369f3..4844df5290 100644--- a/saleor/stock/models.py+++ b/saleor/stock/models.py@@ -17,6 +17,14 @@ class StockQuerySet(models.QuerySet):product_variant=product_variant,)+ def for_country(self, country_code: str):+ query_warehouse = models.Subquery(+ Warehouse.objects.prefetch_related("shipping_zones")+ .filter(shipping_zones__countries__contains=country_code)+ .values("pk")+ )+ return self.filter(warehouse__in=query_warehouse)+class Stock(models.Model):warehouse = models.ForeignKey(Warehouse, null=False, on_delete=models.PROTECT)diff --git a/tests/conftest.py b/tests/conftest.pyindex c4855fbe65..af2840801a 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -78,6 +78,7 @@ from saleor.shipping.models import ()from saleor.site import AuthenticationBackendsfrom saleor.site.models import AuthorizationKey, SiteSettings+from saleor.stock.models import Stockfrom saleor.warehouse.models import Warehousefrom saleor.webhook.event_types import WebhookEventTypefrom saleor.webhook.models import Webhook@@ -208,7 +209,9 @@ def site_settings(db, settings) -> SiteSettings:@pytest.fixturedef checkout(db):- return Checkout.objects.create()+ checkout = Checkout.objects.create()+ checkout.set_country("US")+ return checkout@pytest.fixture@@ -594,7 +597,7 @@ def product_type_without_variant():@pytest.fixture-def product(product_type, category):+def product(product_type, category, warehouse):product_attr = product_type.product_attributes.first()product_attr_value = product_attr.values.first()@@ -612,11 +615,10 @@ def product(product_type, category):variant_attr_value = variant_attr.values.first()variant = ProductVariant.objects.create(- product=product,- sku="123",- cost_price=Money("1.00", "USD"),- quantity=10,- quantity_allocated=1,+ product=product, sku="123", cost_price=Money("1.00", "USD")+ )+ Stock.objects.create(+ warehouse=warehouse, product_variant=variant, quantity=10, quantity_allocated=1)associate_attribute_values_to_instance(variant, variant_attr, variant_attr_value)@@ -624,7 +626,7 @@ def product(product_type, category):@pytest.fixture-def product_with_single_variant(product_type, category):+def product_with_single_variant(product_type, category, warehouse):product = Product.objects.create(name="Test product with single variant",price=Money("1.99", "USD"),@@ -636,14 +638,13 @@ def product_with_single_variant(product_type, category):product=product,sku="SKU_SINGLE_VARIANT",cost_price=Money("1.00", "USD"),- quantity=101,- quantity_allocated=1,)+ Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=101, quantity_allocated=1)return product@pytest.fixture-def product_with_two_variants(product_type, category):+def product_with_two_variants(product_type, category, warehouse):product = Product.objects.create(name="Test product with two variants",price=Money("10.00", "USD"),@@ -651,24 +652,24 @@ def product_with_two_variants(product_type, category):category=category,)- ProductVariant.objects.bulk_create(- [+ variants = [ProductVariant(product=product,sku=f"Product variant #{i}",cost_price=Money("1.00", "USD"),- quantity=10,- quantity_allocated=1,)for i in (1, 2)]+ ProductVariant.objects.bulk_create(variants)+ Stock.objects.bulk_create(+ [Stock(warehouse=warehouse, product_variant=variant, quantity=10, quantity_allocated=1)])return product@pytest.fixture-def product_with_variant_with_two_attributes(color_attribute, size_attribute, category):+def product_with_variant_with_two_attributes(color_attribute, size_attribute, category, warehouse):product_type = ProductType.objects.create(name="Type with two variants", has_variants=True, is_shipping_required=True)@@ -684,11 +685,10 @@ def product_with_variant_with_two_attributes(color_attribute, size_attribute, ca)variant = ProductVariant.objects.create(- product=product,- sku="prodVar1",- cost_price=Money("1.00", "USD"),- quantity=10,- quantity_allocated=1,+ product=product, sku="prodVar1", cost_price=Money("1.00", "USD")+ )+ Stock.objects.create(+ product_variant=variant, warehouse=warehouse, quantity=10, quantity_allocated=1)associate_attribute_values_to_instance(@@ -723,7 +723,7 @@ def product_with_multiple_values_attributes(product, product_type, category) ->@pytest.fixture-def product_with_default_variant(product_type_without_variant, category):+def product_with_default_variant(product_type_without_variant, category, warehouse):product = Product.objects.create(name="Test product",price=Money(10, "USD"),@@ -731,20 +731,15 @@ def product_with_default_variant(product_type_without_variant, category):category=category,is_published=True,)- ProductVariant.objects.create(- product=product, sku="1234", track_inventory=True, quantity=100- )+ ProductVariant.objects.create(product=product, sku="1234", track_inventory=True)+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=100)return product@pytest.fixturedef variant(product):product_variant = ProductVariant.objects.create(- product=product,- sku="SKU_A",- cost_price=Money(1, "USD"),- quantity=5,- quantity_allocated=3,+ product=product, sku="SKU_A", cost_price=Money(1, "USD"))return product_variant@@ -787,7 +782,7 @@ def product_without_category(product):@pytest.fixture-def product_list(product_type, category):+def product_list(product_type, category, warehouse):product_attr = product_type.product_attributes.first()attr_value = product_attr.values.first()@@ -821,28 +816,31 @@ def product_list(product_type, category):]))- ProductVariant.objects.bulk_create(- [- ProductVariant(- product=products[0],- sku=str(uuid.uuid4()).replace("-", ""),- track_inventory=True,- quantity=100,- ),- ProductVariant(- product=products[1],- sku=str(uuid.uuid4()).replace("-", ""),- track_inventory=True,- quantity=100,- ),- ProductVariant(- product=products[2],- sku=str(uuid.uuid4()).replace("-", ""),- track_inventory=True,- quantity=100,- ),- ]+ variants = list(+ ProductVariant.objects.bulk_create(+ [+ ProductVariant(+ product=products[0],+ sku=str(uuid.uuid4()).replace("-", ""),+ track_inventory=True,+ ),+ ProductVariant(+ product=products[1],+ sku=str(uuid.uuid4()).replace("-", ""),+ track_inventory=True,+ ),+ ProductVariant(+ product=products[2],+ sku=str(uuid.uuid4()).replace("-", ""),+ track_inventory=True,+ ),+ ]+ ))+ stocks = []+ for variant in variants:+ stocks.append(Stock(warehouse=warehouse, product_variant=variant, quantity=100))+ Stock.objects.bulk_create(stocks)for product in products:associate_attribute_values_to_instance(product, product_attr, attr_value)@@ -898,7 +896,7 @@ def unavailable_product(product_type, category):@pytest.fixture-def unavailable_product_with_variant(product_type, category):+def unavailable_product_with_variant(product_type, category, warehouse):product = Product.objects.create(name="Test product",price=Money("10.00", "USD"),@@ -911,11 +909,10 @@ def unavailable_product_with_variant(product_type, category):variant_attr_value = variant_attr.values.first()variant = ProductVariant.objects.create(- product=product,- sku="123",- cost_price=Money(1, "USD"),- quantity=10,- quantity_allocated=1,+ product=product, sku="123", cost_price=Money(1, "USD")+ )+ Stock.objects.create(+ product_variant=variant, warehouse=warehouse, quantity=10, quantity_allocated=1)associate_attribute_values_to_instance(variant, variant_attr, variant_attr_value)@@ -1034,7 +1031,7 @@ def gift_card_created_by_staff(staff_user):@pytest.fixture-def order_with_lines(order, product_type, category, shipping_zone):+def order_with_lines(order, product_type, category, shipping_zone, warehouse):product = Product.objects.create(name="Test product",price=Money("10.00", "USD"),@@ -1043,11 +1040,10 @@ def order_with_lines(order, product_type, category, shipping_zone):is_published=True,)variant = ProductVariant.objects.create(- product=product,- sku="SKU_A",- cost_price=Money(1, "USD"),- quantity=5,- quantity_allocated=3,+ product=product, sku="SKU_A", cost_price=Money(1, "USD")+ )+ Stock.objects.create(+ warehouse=warehouse, product_variant=variant, quantity=5, quantity_allocated=3)net = variant.get_price()gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)@@ -1070,11 +1066,10 @@ def order_with_lines(order, product_type, category, shipping_zone):is_published=True,)variant = ProductVariant.objects.create(- product=product,- sku="SKU_B",- cost_price=Money(2, "USD"),- quantity=2,- quantity_allocated=2,+ product=product, sku="SKU_B", cost_price=Money(2, "USD")+ )+ Stock.objects.create(+ product_variant=variant, warehouse=warehouse, quantity=2, quantity_allocated=2)net = variant.get_price()@@ -1812,4 +1807,16 @@ def warehouse(address, shipping_zone):warehouse.shipping_zones.add(shipping_zone)warehouse.save()return warehouse+<<<<<<< HEAD>>>>>>> Test permissions for warehouse views+=======+++def stock(variant, warehouse):+ return Stock.objects.get_or_create(+ product_variant=variant,+ warehouse=warehouse,+ defaults={"quantity": 5, "quantity_allocated": 3},+ )[0]+>>>>>>> Add option to get only stock available in given countrydiff --git a/tests/test_stock.py b/tests/test_stock.pynew file mode 100644index 0000000000..ed189d3c4f--- /dev/null+++ b/tests/test_stock.py@@ -0,0 +1,13 @@+from saleor.stock.models import Stock+++def test_stock_for_country(product):+ country_code = "US"+ stock = Stock.objects.get()+ warehouse = stock.warehouse+ assert country_code in warehouse.countries+ assert stock.warehouse == warehouse++ stock_qs = Stock.objects.for_country(country_code)+ assert stock_qs.count() == 1+ assert stock_qs.first() == stock