Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Split stocks utils in many files
Mira Szewczykbdae869e6517
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 470f1634c0..317cad0cdc 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -40,12 +40,10 @@ from ..order.actions import order_createdfrom ..order.emails import send_order_confirmation, send_staff_order_confirmationfrom ..order.models import Order, OrderLinefrom ..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_stockfrom . import AddressType+from .models import Checkout, CheckoutLineif TYPE_CHECKING:diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex 9ad319ba74..37eb914bcf 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -13,6 +13,7 @@ from .payment.schema import PaymentMutations, PaymentQueriesfrom .product.schema import ProductMutations, ProductQueriesfrom .shipping.schema import ShippingMutations, ShippingQueriesfrom .shop.schema import ShopMutations, ShopQueries+from .stock.schema import StockMutations, StockQueriesfrom .translations.schema import TranslationQueriesfrom .warehouse.schema import WarehouseMutations, WarehouseQueriesfrom .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.pyindex 30542ca9bf..35245e93e0 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -8,7 +8,7 @@ from django.utils.translation import pgettext_lazyfrom ..core import analyticsfrom ..extensions.manager import get_extensions_managerfrom ..payment import ChargeStatus, CustomPaymentChoices, PaymentError-from ..stock.stock_management import decrease_stock+from ..stock.utils.management import decrease_stockfrom . import FulfillmentStatus, OrderStatus, emails, events, utilsfrom .emails import send_fulfillment_confirmation_to_customer, send_payment_confirmationfrom .models import Fulfillment, FulfillmentLinediff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 50ef8930a9..71726c203c 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -15,7 +15,8 @@ from ..order import OrderStatusfrom ..order.models import Order, OrderLinefrom ..product.utils.digital_products import get_default_digital_content_settingsfrom ..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_stockfrom . import events@@ -167,8 +168,9 @@ def add_variant_to_order(By default, raises InsufficientStock exception if quantity could not befulfilled. This can be disabled by setting `allow_overselling` to True."""+ country = order.shipping_address.countryif 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.pynew file mode 100644index 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.pysimilarity index 56%rename from saleor/stock/stock_management.pyrename to saleor/stock/utils/management.pyindex 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 Stockif 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.pyindex 768231fa0f..dc57524c3a 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -14,7 +14,7 @@ from saleor.product.thumbnails import create_product_thumbnailsfrom saleor.product.utils.attributes import associate_attribute_values_to_instancefrom saleor.product.utils.costs import get_margin_for_variantfrom 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.pyindex a972b4dbb9..58797953c0 100644--- a/tests/test_stock.py+++ b/tests/test_stock.py@@ -2,7 +2,7 @@ import pytestfrom saleor.core.exceptions import InsufficientStockfrom saleor.stock.models import Stock-from saleor.stock.stock_management import check_stock_quantity+from saleor.stock.utils.availability import check_stock_quantityCOUNTRY_CODE = "US"