Skip to content

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

Remove StockManager

Anna Siwik02c97dd7a212
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 81d4a139fe..8d658355f5 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -7,6 +7,8 @@ from django_prices.forms import PriceField
from payments import PaymentError, PaymentStatus
from satchless.item import InsufficientStock
+from saleor.product.utils import (
+ allocate_stock, deallocate_stock, decrease_stock)
from ...cart.forms import QuantityField
from ...core.forms import AjaxSelect2ChoiceField
from ...discount.models import Voucher
@@ -139,8 +141,7 @@ class CancelLinesForm(forms.Form):
super().__init__(*args, **kwargs)
def cancel_line(self):
- if self.line.stock:
- Stock.objects.deallocate_stock(self.line.stock, self.line.quantity)
+ deallocate_stock(self.line.stock, self.line.quantity)
order = self.line.delivery_group.order
self.line.quantity = 0
remove_empty_groups(self.line)
@@ -183,7 +184,7 @@ class ChangeQuantityForm(forms.ModelForm):
if stock is not None:
# update stock allocation
delta = quantity - self.initial_quantity
- Stock.objects.allocate_stock(stock, delta)
+ allocate_stock(stock, delta)
change_order_line_quantity(self.instance, quantity)
recalculate_order(self.instance.delivery_group.order)
return self.instance
@@ -317,13 +318,11 @@ class ChangeStockForm(forms.ModelForm):
def save(self, commit=True):
quantity = self.instance.quantity
- if self.old_stock is not None:
- Stock.objects.deallocate_stock(self.old_stock, quantity)
stock = self.instance.stock
- if stock is not None:
- self.instance.stock_location = (
- stock.location.name if stock.location else '')
- Stock.objects.allocate_stock(stock, quantity)
+ self.instance.stock_location = (
+ stock.location.name if stock.location else '')
+ deallocate_stock(self.old_stock, quantity)
+ allocate_stock(stock, quantity)
super().save(commit)
merge_duplicates_into_order_line(self.instance)
return self.instance
diff --git a/saleor/order/transitions.py b/saleor/order/transitions.py
index 27f5614ee8..176763135a 100644
--- a/saleor/order/transitions.py
+++ b/saleor/order/transitions.py
@@ -1,6 +1,8 @@
from saleor.order import GroupStatus
from saleor.order.utils import add_variant_to_delivery_group
from saleor.product.models import Stock
+from saleor.product.utils import (
+ deallocate_stock, decrease_stock, increase_stock)
def process_delivery_group(group, cart_lines, discounts=None):
@@ -15,14 +17,14 @@ def cancel_delivery_group(group):
"""Cancels delivery group by returning products to stocks."""
if group.status == GroupStatus.NEW:
for line in group:
- Stock.objects.deallocate_stock(line.stock, line.quantity)
+ deallocate_stock(line.stock, line.quantity)
elif group.status == GroupStatus.SHIPPED:
for line in group:
- Stock.objects.increase_stock(line.stock, line.quantity)
+ increase_stock(line.stock, line.quantity)
def ship_delivery_group(group, tracking_number=''):
"""Ships delivery group by decreasing products in stocks."""
for line in group.lines.all():
- Stock.objects.decrease_stock(line.stock, line.quantity)
+ decrease_stock(line.stock, line.quantity)
group.tracking_number = tracking_number
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 06fdceb373..20d5ba3d61 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -7,7 +7,7 @@ from django.utils.translation import pgettext_lazy
from prices import Price
from satchless.item import InsufficientStock
-from ..product.models import Stock
+from saleor.product.utils import allocate_stock, deallocate_stock
from ..userprofile.utils import store_user_address
from . import GroupStatus
@@ -93,7 +93,7 @@ def add_variant_to_delivery_group(
unit_price_gross=price.gross,
stock=stock,
stock_location=stock.location.name)
- Stock.objects.allocate_stock(stock, quantity)
+ allocate_stock(stock, quantity)
# refresh stock for accessing quantity_allocated
stock.refresh_from_db()
quantity_left -= quantity
@@ -122,7 +122,7 @@ def add_variant_to_existing_lines(group, variant, total_quantity):
else quantity_left)
line.quantity += quantity
line.save()
- Stock.objects.allocate_stock(line.stock, quantity)
+ allocate_stock(line.stock, quantity)
quantity_left -= quantity
if quantity_left == 0:
break
diff --git a/saleor/product/models.py b/saleor/product/models.py
index ee7e0063c5..4f4c0e0631 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -294,25 +294,6 @@ class StockLocation(models.Model):
return self.name
-class StockManager(models.Manager):
- def allocate_stock(self, stock, quantity):
- stock.quantity_allocated = F('quantity_allocated') + quantity
- stock.save(update_fields=['quantity_allocated'])
-
- def deallocate_stock(self, stock, quantity):
- stock.quantity_allocated = F('quantity_allocated') - quantity
- stock.save(update_fields=['quantity_allocated'])
-
- def increase_stock(self, stock, quantity):
- stock.quantity = F('quantity') + quantity
- stock.save(update_fields=['quantity'])
-
- def decrease_stock(self, stock, quantity):
- stock.quantity = F('quantity') - quantity
- stock.quantity_allocated = F('quantity_allocated') - quantity
- stock.save(update_fields=['quantity', 'quantity_allocated'])
-
-
class Stock(models.Model):
variant = models.ForeignKey(
ProductVariant, related_name='stock', on_delete=models.CASCADE)
@@ -326,8 +307,6 @@ class Stock(models.Model):
currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
blank=True, null=True)
- objects = StockManager()
-
class Meta:
app_label = 'product'
unique_together = ('variant', 'location')
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index c711507cfc..e6453fdfe5 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -3,6 +3,7 @@ from urllib.parse import urlencode
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
+from django.db.models import F
from django.utils.encoding import smart_text
from django_prices.templatetags import prices_i18n
from prices import Price, PriceRange
@@ -370,3 +371,19 @@ def get_margin_for_variant(stock):
margin = price - stock.cost_price
percent = round((margin.gross / price.gross) * 100, 0)
return percent
+
+
+def allocate_stock(stock, quantity):
+ stock.quantity_allocated = F('quantity_allocated') + quantity
+ stock.save(update_fields=['quantity_allocated'])
+
+
+def deallocate_stock(stock, quantity):
+ stock.quantity_allocated = F('quantity_allocated') - quantity
+ stock.save(update_fields=['quantity_allocated'])
+
+
+def decrease_stock(stock, quantity):
+ stock.quantity = F('quantity') - quantity
+ stock.quantity_allocated = F('quantity_allocated') - quantity
+ stock.save(update_fields=['quantity', 'quantity_allocated'])
diff --git a/tests/test_product.py b/tests/test_product.py
index bfa5201cea..cf26198ec4 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -12,6 +12,7 @@ from saleor.cart.models import Cart
from saleor.product import (
ProductAvailabilityStatus, VariantAvailabilityStatus, models)
from saleor.product.utils import (
+ allocate_stock, deallocate_stock, decrease_stock,
get_attributes_display_map, get_availability,
get_product_availability_status, get_variant_availability_status,
get_variant_picker_data)
@@ -31,12 +32,12 @@ def test_stock_selector(product_in_stock):
assert preferred_stock.quantity_available >= 5
-def test_stock_allocator(product_in_stock):
+def test_allocate_stock(product_in_stock):
variant = product_in_stock.variants.get()
stock = variant.select_stockrecord(5)
assert stock.quantity_allocated == 0
- models.Stock.objects.allocate_stock(stock, 1)
- stock = models.Stock.objects.get(pk=stock.pk)
+ allocate_stock(stock, 1)
+ stock.refresh_from_db()
assert stock.quantity_allocated == 1
@@ -45,7 +46,7 @@ def test_decrease_stock(product_in_stock):
stock.quantity = 100
stock.quantity_allocated = 80
stock.save()
- models.Stock.objects.decrease_stock(stock, 50)
+ decrease_stock(stock, 50)
stock.refresh_from_db()
assert stock.quantity == 50
assert stock.quantity_allocated == 30
@@ -67,7 +68,7 @@ def test_deallocate_stock(product_in_stock):
stock.quantity = 100
stock.quantity_allocated = 80
stock.save()
- models.Stock.objects.deallocate_stock(stock, 50)
+ deallocate_stock(stock, 50)
stock.refresh_from_db()
assert stock.quantity == 100
assert stock.quantity_allocated == 30