Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor CancelOrderForm to not call CancelGroupForm
Maciek Jabłoński83b4022a91a2
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 96be11b657..06bbcc012f 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -13,6 +13,7 @@ from ...cart.forms import QuantityFieldfrom ...discount.models import Voucherfrom ...order import Statusfrom ...order.models import DeliveryGroup, Order, OrderedItem, OrderNote+from ...order.utils import cancel_order, cancel_delivery_groupfrom ...product.models import ProductVariant, Stock@@ -236,17 +237,7 @@ class CancelGroupForm(forms.Form):super(CancelGroupForm, self).__init__(*args, **kwargs)def cancel_group(self):- for line in self.delivery_group:- if line.stock:- Stock.objects.deallocate_stock(line.stock, line.quantity)- self.delivery_group.status = Status.CANCELLED- self.delivery_group.save()- other_groups = self.delivery_group.order.groups.all()- statuses = set(other_groups.values_list('status', flat=True))- if statuses == {Status.CANCELLED}:- # Cancel whole order- self.delivery_group.order.status = Status.CANCELLED- self.delivery_group.order.save(update_fields=['status'])+ cancel_delivery_group(self.delivery_group)class CancelOrderForm(forms.Form):@@ -265,9 +256,7 @@ class CancelOrderForm(forms.Form):return datadef cancel_order(self):- for group in self.order.groups.all():- group_form = CancelGroupForm(delivery_group=group)- group_form.cancel_group()+ cancel_order(self.order)class RemoveVoucherForm(forms.Form):diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex b1f54ededf..7ac7397a68 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -1,15 +1,15 @@import loggingfrom functools import wraps-from payments.signals import status_changedfrom django.dispatch import receiverfrom django.shortcuts import get_object_or_404, redirect+from payments.signals import status_changed-from .models import Order+from . import Statusfrom ..core import analyticsfrom ..product.models import Stockfrom ..userprofile.utils import store_user_address-+from .models import Orderlogger = logging.getLogger(__name__)@@ -67,3 +67,24 @@ def add_items_to_delivery_group(delivery_group, partition, discounts=None):# allocate quantity to avoid oversellingStock.objects.allocate_stock(stock, quantity)++def cancel_delivery_group(group, cancel_order=True):+ for line in group:+ if line.stock:+ Stock.objects.deallocate_stock(line.stock, line.quantity)+ group.status = Status.CANCELLED+ group.save()+ if cancel_order:+ other_groups = group.order.groups.all()+ statuses = set(other_groups.values_list('status', flat=True))+ if statuses == {Status.CANCELLED}:+ # Cancel whole order+ group.order.status = Status.CANCELLED+ group.order.save(update_fields=['status'])+++def cancel_order(order):+ for group in order.groups.all():+ cancel_delivery_group(group, cancel_order=False)+ order.status = Status.CANCELLED+ order.save()