Skip to content

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

Move stock-related functions to separate module

Mira Szewczykf5f4cdd6d1c6
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 606440e5e8..470f1634c0 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -40,7 +40,11 @@ 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 check_stock_quantity, get_available_quantity
+from ..stock.stock_management import (
+ allocate_stock,
+ check_stock_quantity,
+ get_available_quantity,
+)
from . import AddressType
from .models import Checkout, CheckoutLine
@@ -729,7 +733,6 @@ def create_order(
Current user's language is saved in the order so we can later determine
which language to use when sending email.
"""
- from ..product.utils import allocate_stock
from ..order.utils import add_gift_card_to_order
order = Order.objects.filter(checkout_token=checkout.token).first()
@@ -746,7 +749,7 @@ def create_order(
for line in order_lines: # type: OrderLine
variant = line.variant
if variant and variant.track_inventory:
- allocate_stock(variant, line.quantity)
+ allocate_stock(variant, checkout.country, line.quantity)
# Add gift cards to the order
for gift_card in checkout.gift_cards.select_for_update():
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index ce3dfda090..30542ca9bf 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 ..product.utils import decrease_stock
+from ..stock.stock_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 81f6601268..50ef8930a9 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -13,9 +13,9 @@ from ..discount.utils import get_products_voucher_discount, validate_voucher_in_
from ..extensions.manager import get_extensions_manager
from ..order import OrderStatus
from ..order.models import Order, OrderLine
-from ..product.utils import allocate_stock, deallocate_stock, increase_stock
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 . import events
diff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.py
index 9be41df7d6..dbe1707c95 100644
--- a/saleor/product/utils/__init__.py
+++ b/saleor/product/utils/__init__.py
@@ -3,7 +3,6 @@ from urllib.parse import urlencode
from django.conf import settings
from django.db import transaction
-from django.db.models import F
from ...core.taxes import TaxedMoney, zero_taxed_money
from ..tasks import update_products_minimal_variant_prices_task
@@ -17,32 +16,6 @@ if TYPE_CHECKING:
from ..models import Product, ProductVariant, Category
-def allocate_stock(variant: "ProductVariant", quantity: int):
- variant.quantity_allocated = F("quantity_allocated") + quantity
- variant.save(update_fields=["quantity_allocated"])
-
-
-def deallocate_stock(variant: "ProductVariant", quantity: int):
- variant.quantity_allocated = F("quantity_allocated") - quantity
- variant.save(update_fields=["quantity_allocated"])
-
-
-def decrease_stock(variant: "ProductVariant", quantity: int):
- variant.quantity = F("quantity") - quantity
- variant.quantity_allocated = F("quantity_allocated") - quantity
- variant.save(update_fields=["quantity", "quantity_allocated"])
-
-
-def increase_stock(variant: "ProductVariant", quantity, allocate: bool = False):
- """Return given quantity of product to a stock."""
- variant.quantity = F("quantity") + quantity
- update_fields = ["quantity"]
- if allocate:
- variant.quantity_allocated = F("quantity_allocated") + quantity
- update_fields.append("quantity_allocated")
- variant.save(update_fields=update_fields)
-
-
def calculate_revenue_for_variant(
variant: "ProductVariant", start_date: Union["date", "datetime"]
) -> TaxedMoney:
diff --git a/saleor/stock/models.py b/saleor/stock/models.py
index 4844df5290..9fae186987 100644
--- a/saleor/stock/models.py
+++ b/saleor/stock/models.py
@@ -1,4 +1,5 @@
from django.db import models
+from django.db.models import F
from django.utils.translation import pgettext_lazy
from ..core.exceptions import InsufficientStock
@@ -56,6 +57,34 @@ class Stock(models.Model):
def is_available(self):
return self.quantity > 0
- def check_quantity(self, quantity):
+ def check_quantity(self, quantity: int):
if quantity > self.quantity_available:
raise InsufficientStock(self)
+
+ def allocate_stock(self, quantity: int, commit: bool = True):
+ self.quantity_allocated = F("quantity_allocated") + quantity
+ if commit:
+ self.save(update_fields=["quantity_allocated"])
+
+ def deallocate_stock(self, quantity: int, commit: bool = True):
+ self.quantity_allocated = F("quantity_allocated") - quantity
+ if commit:
+ self.save(update_fields=["quantity_allocated"])
+
+ def increase_stock(
+ self, quantity: int, allocate: bool = False, commit: bool = True
+ ):
+ """Return given quantity of product to a stock."""
+ self.quantity = F("quantity") + quantity
+ update_fields = ["quantity"]
+ if allocate:
+ self.quantity_allocated = F("quantity_allocated") + quantity
+ update_fields.append("quantity_allocated")
+ if commit:
+ self.save(update_fields=update_fields)
+
+ def decrease_stock(self, quantity: int, commit: bool = True):
+ self.quantity = F("quantity") - quantity
+ self.quantity_allocated = F("quantity_allocated") - quantity
+ if commit:
+ self.save(update_fields=["quantity", "quantity_allocated"])
diff --git a/saleor/stock/stock_management.py b/saleor/stock/stock_management.py
index a07a3046cf..449ae9b470 100644
--- a/saleor/stock/stock_management.py
+++ b/saleor/stock/stock_management.py
@@ -11,7 +11,7 @@ def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity:
try:
stock = Stock.objects.for_country(country_code).get(product_variant=variant)
except Stock.DoesNotExist:
- raise InsufficientStock
+ raise InsufficientStock(variant)
if variant.track_inventory and quantity > stock.quantity_available:
raise InsufficientStock(variant)
@@ -23,3 +23,23 @@ def get_available_quantity(variant: "ProductVariant", country_code: str) -> int:
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)
+
+
+def deallocate_stock(variant: "ProductVariant", country_code: str, quantity: int):
+ stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock.deallocate_stock(quantity)
+
+
+def increase_stock(variant: "ProductVariant", country_code: str, quantity: int):
+ stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock.increase_stock(quantity)
+
+
+def decrease_stock(variant: "ProductVariant", country_code: str, quantity: int):
+ stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock.decrease_stock(quantity)
diff --git a/tests/conftest.py b/tests/conftest.py
index bd364d759c..9b22a75d47 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -760,7 +760,7 @@ def product_variant_list(product):
@pytest.fixture
-def product_without_shipping(category):
+def product_without_shipping(category, warehouse):
product_type = ProductType.objects.create(
name="Type with no shipping", has_variants=False, is_shipping_required=False
)
@@ -771,7 +771,8 @@ def product_without_shipping(category):
category=category,
is_published=True,
)
- ProductVariant.objects.create(product=product, sku="SKU_B")
+ variant = ProductVariant.objects.create(product=product, sku="SKU_B")
+ Stock.objects.create(product_variant=variant, warehouse=warehouse, quantity=1)
return product
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 21649a623c..c60b8b798e 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -34,6 +34,7 @@ from saleor.extensions.manager import get_extensions_manager
from saleor.order import OrderEvents, OrderEventsEmails
from saleor.order.models import OrderEvent
from saleor.shipping.models import ShippingZone
+from saleor.stock.models import Stock
def test_is_valid_shipping_method(checkout_with_item, address, shipping_zone):
diff --git a/tests/test_product.py b/tests/test_product.py
index a0822aae03..768231fa0f 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -11,15 +11,15 @@ from saleor.product import models
from saleor.product.filters import filter_products_by_attributes_values
from saleor.product.models import DigitalContentUrl
from saleor.product.thumbnails import create_product_thumbnails
-from saleor.product.utils import (
+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 (
allocate_stock,
deallocate_stock,
decrease_stock,
increase_stock,
)
-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
@pytest.mark.parametrize(