Skip to content

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

Extract draft order validators to utils

fowczarekb74d025039a6
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 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 I18nMixin
from ...account.types import AddressInput
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ...core.types.common import Decimal, Error
+from ...core.types.common import Decimal
from ...product.types import ProductVariant
from ..types import Order, OrderLine
+from ..utils import check_for_draft_order_errors
class 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.email
elif 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.UNFULFILLED
if not order.is_shipping_required():
order.shipping_method_name = None
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
new file mode 100644
index 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 errors
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index e8ffc899c5..b208fad5b4 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -4,15 +4,13 @@ import graphene
import pytest
from tests.api.utils import get_graphql_content
-from saleor.account.models import Address
from saleor.core.utils.taxes import ZERO_TAXED_MONEY
from 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_errors
from saleor.graphql.payment.types import PaymentChargeStatusEnum
from saleor.order import OrderEvents, OrderEventsEmails, OrderStatus
from saleor.order.models import Order, OrderEvent