Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extract draft order validators to utils
fowczarekb74d025039a6
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.pyindex 2771437fc0..6b23a1a2c0 100644--- a/saleor/graphql/order/mutations/draft_orders.py+++ b/saleor/graphql/order/mutations/draft_orders.py@@ -13,9 +13,10 @@ from ....order.utils import (from ...account.i18n import I18nMixinfrom ...account.types import AddressInputfrom ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation-from ...core.types.common import Decimal, Error+from ...core.types.common import Decimalfrom ...product.types import ProductVariantfrom ..types import Order, OrderLine+from ..utils import check_for_draft_order_errorsclass OrderLineInput(graphene.InputObjectType):@@ -153,32 +154,6 @@ class DraftOrderDelete(ModelDeleteMutation):return user.has_perm('order.manage_orders')-def check_for_draft_order_errors(order, errors):- """Return a list of errors associated with the order.-- Checks, if given order has a proper shipping address and method- set up and return list of errors if not.- """- if order.get_total_quantity() == 0:- errors.append(- Error(- field='lines',- message='Could not create order without any products.'))- if order.is_shipping_required():- method = order.shipping_method- shipping_address = order.shipping_address- shipping_not_valid = (- method and shipping_address and- shipping_address.country.code not in method.shipping_zone.countries) # noqa- if shipping_not_valid:- errors.append(- Error(- field='shipping',- message='Shipping method is not valid for chosen shipping '- 'address'))- return errors--class DraftOrderComplete(BaseMutation):order = graphene.Field(Order, description='Completed order.')@@ -191,7 +166,7 @@ class DraftOrderComplete(BaseMutation):description = 'Completes creating an order.'@classmethod- def update_user_fields(cls, order, errors):+ def update_user_fields(cls, order):if order.user:order.user_email = order.user.emailelif order.user_email:@@ -199,21 +174,18 @@ class DraftOrderComplete(BaseMutation):order.user = User.objects.get(email=order.user_email)except User.DoesNotExist:order.user = None- else:- cls.add_error(- errors, field=None,- message='Both user and user_email fields are null')@classmethod@permission_required('order.manage_orders')def mutate(cls, root, info, id):errors = []order = cls.get_node_or_error(info, id, errors, 'id', Order)- errors = check_for_draft_order_errors(order, errors)- cls.update_user_fields(order, errors)+ if order:+ errors = check_for_draft_order_errors(order, errors)if errors:return cls(errors=errors)+ cls.update_user_fields(order)order.status = OrderStatus.UNFULFILLEDif not order.is_shipping_required():order.shipping_method_name = Nonediff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.pynew file mode 100644index 0000000000..e96541e48d--- /dev/null+++ b/saleor/graphql/order/utils.py@@ -0,0 +1,33 @@+from ..core.types.common import Error+++def check_for_draft_order_errors(order, errors=[]):+ # TODO: New Description+ """Return a list of errors associated with the order.++ Checks, if given order has a proper shipping address and method+ set up and return list of errors if not.+ """+ if order.get_total_quantity() == 0:+ errors.append(+ Error(+ field='lines',+ message='Could not create order without any products.'))+ if order.is_shipping_required():+ method = order.shipping_method+ shipping_address = order.shipping_address+ shipping_not_valid = (+ method and shipping_address and+ shipping_address.country.code not in method.shipping_zone.countries) # noqa+ if shipping_not_valid:+ errors.append(+ Error(+ field='shipping',+ message='Shipping method is not valid for chosen shipping '+ 'address'))+ if not order.user and not order.user_email:+ errors.append(+ Error(+ field=None,+ message='Both user and user_email fields are null'))+ return errorsdiff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex e8ffc899c5..b208fad5b4 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -4,15 +4,13 @@ import grapheneimport pytestfrom tests.api.utils import get_graphql_content-from saleor.account.models import Addressfrom saleor.core.utils.taxes import ZERO_TAXED_MONEYfrom saleor.graphql.core.types import ReportingPeriod-from saleor.graphql.order.mutations.draft_orders import (- check_for_draft_order_errors)from saleor.graphql.order.mutations.orders import (clean_order_cancel, clean_order_capture, clean_order_mark_as_paid,clean_refund_payment, clean_void_payment)from saleor.graphql.order.types import OrderEventsEmailsEnum, OrderStatusFilter+from saleor.graphql.order.utils import check_for_draft_order_errorsfrom saleor.graphql.payment.types import PaymentChargeStatusEnumfrom saleor.order import OrderEvents, OrderEventsEmails, OrderStatusfrom saleor.order.models import Order, OrderEvent