Skip to content

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

Move statuses management logic to transitions file

Anna Siwikb5d6c18e6adc
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 86732acb5f..b2085eacb3 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -13,9 +13,9 @@ from ...discount.models import Voucher
from ...order import GroupStatus
from ...order.models import DeliveryGroup, OrderLine, OrderNote
from ...order.utils import (
- add_variant_to_delivery_group, cancel_delivery_group, cancel_order,
- change_order_line_quantity, merge_duplicates_into_order_line,
- move_order_line_to_group, recalculate_order, remove_empty_groups
+ add_variant_to_delivery_group, cancel_order, change_order_line_quantity,
+ merge_duplicates_into_order_line, move_order_line_to_group,
+ recalculate_order, remove_empty_groups
)
from ...product.models import Product, ProductVariant, Stock
@@ -224,7 +224,8 @@ class CancelGroupForm(forms.Form):
super().__init__(*args, **kwargs)
def cancel_group(self):
- cancel_delivery_group(self.delivery_group)
+ self.delivery_group.cancel()
+ self.delivery_group.save()
class CancelOrderForm(forms.Form):
diff --git a/saleor/order/models.py b/saleor/order/models.py
index e339ab3529..ac06385d13 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -2,7 +2,6 @@ from decimal import Decimal
from uuid import uuid4
from django.conf import settings
-from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import Q
@@ -16,10 +15,11 @@ from payments.models import BasePayment
from prices import FixedDiscount, Price
from satchless.item import ItemLine, ItemSet
+from saleor.order.transitions import ship_delivery_group, cancel_delivery_group
from . import emails, GroupStatus, OrderStatus
from ..core.utils import build_absolute_uri
from ..discount.models import Voucher
-from ..product.models import Product, Stock
+from ..product.models import Product
from ..userprofile.models import Address
@@ -217,20 +217,14 @@ class DeliveryGroup(models.Model, ItemSet):
@transition(
field=status, source=GroupStatus.NEW, target=GroupStatus.SHIPPED)
def ship(self):
- for line in self.lines.all():
- Stock.objects.decrease_stock(line.stock, line.quantity)
+ ship_delivery_group(self)
@transition(
field=status,
source=[GroupStatus.NEW, GroupStatus.SHIPPED],
target=GroupStatus.CANCELLED)
def cancel(self):
- if self.status == GroupStatus.NEW:
- for line in self:
- Stock.objects.deallocate_stock(line.stock, line.quantity)
- else:
- for line in self:
- Stock.objects.increase_stock(line.stock, line.quantity)
+ cancel_delivery_group(self)
@property
def shipping_required(self):
diff --git a/saleor/order/transitions.py b/saleor/order/transitions.py
new file mode 100644
index 0000000000..cafd895040
--- /dev/null
+++ b/saleor/order/transitions.py
@@ -0,0 +1,18 @@
+from saleor.order import GroupStatus
+from saleor.product.models import Stock
+
+
+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)
+ else:
+ for line in group:
+ Stock.objects.increase_stock(line.stock, line.quantity)
+
+
+def ship_delivery_group(group):
+ """Ships delivery group by decreasing products in stocks."""
+ for line in group.lines.all():
+ Stock.objects.decrease_stock(line.stock, line.quantity)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 63d3f38803..5defb98b71 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -29,9 +29,10 @@ def check_order_status(func):
def cancel_order(order):
- """Cancells order by cancelling all associated shipment groups."""
+ """Cancels order by cancelling all associated shipment groups."""
for group in order.groups.all():
- cancel_delivery_group(group)
+ group.cancel()
+ group.save()
def recalculate_order(order):
@@ -136,12 +137,6 @@ def add_variant_to_existing_lines(group, variant, total_quantity):
return quantity_left
-def cancel_delivery_group(group):
- """Cancells shipment group and (optionally) it's order if necessary."""
- group.cancel()
- group.save()
-
-
def merge_duplicates_into_order_line(line):
"""Merges duplicated lines in shipment group into one (given) line.
If there are no duplicates, nothing will happen.