Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Split stocks utils in many files

Mira Szewczykbdae869e6517
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 470f1634c0..317cad0cdc 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -40,12 +40,10 @@ from ..order.actions import order_created
from ..order.emails import send_order_confirmation, send_staff_order_confirmation
from ..order.models import Order, OrderLine
from ..shipping.models import ShippingMethod
-from ..stock.stock_management import (
- allocate_stock,
- check_stock_quantity,
- get_available_quantity,
-)
+from ..stock.utils.availability import check_stock_quantity, get_available_quantity
+from ..stock.utils.management import allocate_stock
from . import AddressType
+
from .models import Checkout, CheckoutLine
if TYPE_CHECKING:
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 9ad319ba74..37eb914bcf 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -13,6 +13,7 @@ from .payment.schema import PaymentMutations, PaymentQueries
from .product.schema import ProductMutations, ProductQueries
from .shipping.schema import ShippingMutations, ShippingQueries
from .shop.schema import ShopMutations, ShopQueries
+from .stock.schema import StockMutations, StockQueries
from .translations.schema import TranslationQueries
from .warehouse.schema import WarehouseMutations, WarehouseQueries
from .webhook.schema import WebhookMutations, WebhookQueries
@@ -33,6 +34,7 @@ class Query(
ProductQueries,
ShippingQueries,
ShopQueries,
+ StockQueries,
TranslationQueries,
WarehouseQueries,
WebhookQueries,
@@ -54,6 +56,7 @@ class Mutation(
ProductMutations,
ShippingMutations,
ShopMutations,
+ StockMutations,
WarehouseMutations,
WebhookMutations,
WishlistMutations,
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 30542ca9bf..35245e93e0 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -8,7 +8,7 @@ from django.utils.translation import pgettext_lazy
from ..core import analytics
from ..extensions.manager import get_extensions_manager
from ..payment import ChargeStatus, CustomPaymentChoices, PaymentError
-from ..stock.stock_management import decrease_stock
+from ..stock.utils.management import decrease_stock
from . import FulfillmentStatus, OrderStatus, emails, events, utils
from .emails import send_fulfillment_confirmation_to_customer, send_payment_confirmation
from .models import Fulfillment, FulfillmentLine
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 50ef8930a9..71726c203c 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -15,7 +15,8 @@ from ..order import OrderStatus
from ..order.models import Order, OrderLine
from ..product.utils.digital_products import get_default_digital_content_settings
from ..shipping.models import ShippingMethod
-from ..stock.stock_management import allocate_stock, deallocate_stock, increase_stock
+from ..stock.utils.availability import check_stock_quantity
+from ..stock.utils.management import allocate_stock, deallocate_stock, increase_stock
from . import events
@@ -167,8 +168,9 @@ def add_variant_to_order(
By default, raises InsufficientStock exception if quantity could not be
fulfilled. This can be disabled by setting `allow_overselling` to True.
"""
+ country = order.shipping_address.country
if not allow_overselling:
- variant.check_quantity(quantity)
+ check_stock_quantity(variant, country, quantity)
try:
line = order.lines.get(variant=variant)
diff --git a/saleor/stock/utils/availability.py b/saleor/stock/utils/availability.py
new file mode 100644
index 0000000000..2be2c70bd9
--- /dev/null
+++ b/saleor/stock/utils/availability.py
@@ -0,0 +1,41 @@
+from typing import TYPE_CHECKING
+
+from ...core.exceptions import InsufficientStock
+from ..models import Stock
+
+if TYPE_CHECKING:
+ from ...product.models import Product, ProductVariant
+
+
+def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity: int):
+ try:
+ stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ except Stock.DoesNotExist:
+ raise InsufficientStock(variant)
+
+ if variant.track_inventory and quantity > stock.quantity_available:
+ raise InsufficientStock(variant)
+
+
+def get_available_quantity(variant: "ProductVariant", country_code: str) -> int:
+ try:
+ stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ except Stock.DoesNotExist:
+ return 0
+ return stock.quantity_available
+
+
+def is_variant_in_stock(variant: "ProductVariant", country_code: str) -> bool:
+ stocks = Stock.objects.for_country(country_code).filter(
+ product_variant__pk=variant.pk
+ )
+ return stocks.exists()
+
+
+def is_product_in_stock(product: "Product", country_code: str) -> bool:
+ stocks = (
+ Stock.objects.select_related("product_variant__product")
+ .for_country(country_code)
+ .filter(product_variant__product=product)
+ )
+ return stocks.exists()
diff --git a/saleor/stock/stock_management.py b/saleor/stock/utils/management.py
similarity index 56%
rename from saleor/stock/stock_management.py
rename to saleor/stock/utils/management.py
index 449ae9b470..337533612c 100644
--- a/saleor/stock/stock_management.py
+++ b/saleor/stock/utils/management.py
@@ -1,30 +1,11 @@
from typing import TYPE_CHECKING
-from ..core.exceptions import InsufficientStock
-from .models import Stock
+from ..models import Stock
if TYPE_CHECKING:
from ..product.models import ProductVariant
-def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity: int):
- try:
- stock = Stock.objects.for_country(country_code).get(product_variant=variant)
- except Stock.DoesNotExist:
- raise InsufficientStock(variant)
-
- if variant.track_inventory and quantity > stock.quantity_available:
- raise InsufficientStock(variant)
-
-
-def get_available_quantity(variant: "ProductVariant", country_code: str) -> int:
- try:
- stock = Stock.objects.for_country(country_code).get(product_variant=variant)
- except Stock.DoesNotExist:
- return 0
- return stock.quantity_available
-
-
def allocate_stock(variant: "ProductVariant", country_code: str, quantity: int):
stock = Stock.objects.for_country(country_code).get(product_variant=variant)
stock.allocate_stock(quantity)
diff --git a/tests/test_product.py b/tests/test_product.py
index 768231fa0f..dc57524c3a 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -14,7 +14,7 @@ from saleor.product.thumbnails import create_product_thumbnails
from saleor.product.utils.attributes import associate_attribute_values_to_instance
from saleor.product.utils.costs import get_margin_for_variant
from saleor.product.utils.digital_products import increment_download_count
-from saleor.stock.stock_management import (
+from saleor.stock.utils.management import (
allocate_stock,
deallocate_stock,
decrease_stock,
diff --git a/tests/test_stock.py b/tests/test_stock.py
index a972b4dbb9..58797953c0 100644
--- a/tests/test_stock.py
+++ b/tests/test_stock.py
@@ -2,7 +2,7 @@ import pytest
from saleor.core.exceptions import InsufficientStock
from saleor.stock.models import Stock
-from saleor.stock.stock_management import check_stock_quantity
+from saleor.stock.utils.availability import check_stock_quantity
COUNTRY_CODE = "US"