Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Select for update stocks
Mira Szewczyk7b7c755639cc
diff --git a/saleor/stock/management.py b/saleor/stock/management.pyindex 671dbbd080..22d4098427 100644--- a/saleor/stock/management.py+++ b/saleor/stock/management.py@@ -1,27 +1,36 @@from typing import TYPE_CHECKING+from django.db import transaction+from .models import Stockif TYPE_CHECKING:from ..product.models import ProductVariantdef allocate_stock(variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True) -> Stock:- stock = Stock.objects.for_country(country_code).get(product_variant=variant)+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(+ country_code, variant+ )stock.allocate_stock(quantity, commit=commit)return stockdef deallocate_stock(variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True) -> Stock:- stock = Stock.objects.for_country(country_code).get(product_variant=variant)+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(+ country_code, variant+ )stock.deallocate_stock(quantity, commit=commit)return stockdef increase_stock(variant: "ProductVariant",country_code: str,@@ -29,14 +38,19 @@ def increase_stock(allocate: bool = False,commit: bool = True,) -> Stock:- stock = Stock.objects.for_country(country_code).get(product_variant=variant)+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(+ country_code, variant+ )stock.increase_stock(quantity, allocate=allocate, commit=commit)return stockdef decrease_stock(variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True) -> Stock:- stock = Stock.objects.for_country(country_code).get(product_variant=variant)+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(+ country_code, variant+ )stock.decrease_stock(quantity, commit=commit)return stock