Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move stock-related functions to separate module
Mira Szewczykf5f4cdd6d1c6
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 606440e5e8..470f1634c0 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -40,7 +40,11 @@ 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 check_stock_quantity, get_available_quantity+from ..stock.stock_management import (+ allocate_stock,+ check_stock_quantity,+ get_available_quantity,+)from . import AddressTypefrom .models import Checkout, CheckoutLine@@ -729,7 +733,6 @@ def create_order(Current user's language is saved in the order so we can later determinewhich language to use when sending email."""- from ..product.utils import allocate_stockfrom ..order.utils import add_gift_card_to_orderorder = Order.objects.filter(checkout_token=checkout.token).first()@@ -746,7 +749,7 @@ def create_order(for line in order_lines: # type: OrderLinevariant = line.variantif variant and variant.track_inventory:- allocate_stock(variant, line.quantity)+ allocate_stock(variant, checkout.country, line.quantity)# Add gift cards to the orderfor gift_card in checkout.gift_cards.select_for_update():diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex ce3dfda090..30542ca9bf 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 ..product.utils import decrease_stock+from ..stock.stock_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 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_managerfrom ..order import OrderStatusfrom ..order.models import Order, OrderLine-from ..product.utils import allocate_stock, deallocate_stock, increase_stockfrom ..product.utils.digital_products import get_default_digital_content_settingsfrom ..shipping.models import ShippingMethod+from ..stock.stock_management import allocate_stock, deallocate_stock, increase_stockfrom . import eventsdiff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.pyindex 9be41df7d6..dbe1707c95 100644--- a/saleor/product/utils/__init__.py+++ b/saleor/product/utils/__init__.py@@ -3,7 +3,6 @@ from urllib.parse import urlencodefrom django.conf import settingsfrom django.db import transaction-from django.db.models import Ffrom ...core.taxes import TaxedMoney, zero_taxed_moneyfrom ..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.pyindex 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 Ffrom django.utils.translation import pgettext_lazyfrom ..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.pyindex 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 0return 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.pyindex 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 productdiff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex 21649a623c..c60b8b798e 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -34,6 +34,7 @@ from saleor.extensions.manager import get_extensions_managerfrom saleor.order import OrderEvents, OrderEventsEmailsfrom saleor.order.models import OrderEventfrom saleor.shipping.models import ShippingZone+from saleor.stock.models import Stockdef test_is_valid_shipping_method(checkout_with_item, address, shipping_zone):diff --git a/tests/test_product.py b/tests/test_product.pyindex a0822aae03..768231fa0f 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -11,15 +11,15 @@ from saleor.product import modelsfrom saleor.product.filters import filter_products_by_attributes_valuesfrom saleor.product.models import DigitalContentUrlfrom 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(