Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move canceling order to separate function
Anna Siwik42dee5df34a0
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 6104e53767..4f4e192baf 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -11,13 +11,13 @@ from ...account.i18n import (from ...cart.forms import QuantityFieldfrom ...core.exceptions import InsufficientStockfrom ...discount.utils import decrease_voucher_usage-from ...order import FulfillmentStatus, OrderStatus+from ...order import FulfillmentStatusfrom ...order.emails import send_note_confirmationfrom ...order.models import Fulfillment, FulfillmentLine, OrderLine, OrderNotefrom ...order.utils import (add_variant_to_order, change_order_line_quantity,merge_duplicates_into_order_line, recalculate_order,- restock_fulfillment_lines, restock_order_lines)+ restock_fulfillment_lines, cancel_order)from ...product.models import Product, ProductVariant, Stockfrom ...product.utils import allocate_stock, deallocate_stockfrom ..forms import AjaxSelect2ChoiceField@@ -196,10 +196,7 @@ class CancelOrderForm(forms.Form):return datadef cancel_order(self):- if self.cleaned_data.get('restock'):- restock_order_lines(self.order)- self.order.status = OrderStatus.CANCELED- self.order.save(update_fields=['status'])+ cancel_order(self.order, self.cleaned_data.get('restock'))class CancelFulfillmentForm(forms.Form):diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 2600ee0725..43bf4d68f4 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -6,6 +6,7 @@ from django.shortcuts import get_object_or_404, redirectfrom ..account.utils import store_user_addressfrom ..core.exceptions import InsufficientStock+from ..order import FulfillmentStatus, OrderStatusfrom ..product.utils import allocate_stock, deallocate_stock, increase_stock@@ -43,6 +44,19 @@ def recalculate_order(order):order.save()+def cancel_order(order, restock):+ """Cancel order and associated fulfillments.++ Return products to corresponding stocks if restock is set to True."""+ if restock:+ restock_order_lines(order)+ for fulfillment in order.fulfillments.all():+ fulfillment.status = FulfillmentStatus.CANCELED+ fulfillment.save(update_fields=['status'])+ order.status = OrderStatus.CANCELED+ order.save(update_fields=['status'])++def attach_order_to_user(order, user):"""Associates existing order with user account."""order.user = userdiff --git a/tests/test_order.py b/tests/test_order.pyindex bbac328f53..cdc5779576 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -4,12 +4,12 @@ from django.urls import reversefrom prices import Money, TaxedMoneyfrom tests.utils import get_redirect_location-from saleor.order import models+from saleor.order import FulfillmentStatus, models, OrderStatusfrom saleor.order.emails import collect_data_for_emailfrom saleor.order.forms import OrderNoteFormfrom saleor.order.utils import (- add_variant_to_order, recalculate_order, restock_fulfillment_lines,- restock_order_lines)+ add_variant_to_order, cancel_order, recalculate_order,+ restock_fulfillment_lines, restock_order_lines)def test_total_setter():@@ -204,3 +204,11 @@ def test_restock_fulfillment_lines(fulfilled_order):assert stock_1.quantity_allocated == stock_2_quantity_allocated_beforeassert stock_1.quantity == stock_1_quantity_before + line_1.quantityassert stock_2.quantity == stock_2_quantity_before + line_2.quantity+++def test_cancel_order(fulfilled_order):+ cancel_order(fulfilled_order, restock=False)+ assert all([+ f.status == FulfillmentStatus.CANCELED+ for f in fulfilled_order.fulfillments.all()])+ assert fulfilled_order.status == OrderStatus.CANCELED