Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix review remarks
fowczarekb97d20ccfb27
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.pyindex 6b23a1a2c0..4b325198df 100644--- a/saleor/graphql/order/mutations/draft_orders.py+++ b/saleor/graphql/order/mutations/draft_orders.py@@ -16,7 +16,7 @@ from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ...core.types.common import Decimalfrom ...product.types import ProductVariantfrom ..types import Order, OrderLine-from ..utils import check_for_draft_order_errors+from ..utils import can_finalize_draft_orderclass OrderLineInput(graphene.InputObjectType):@@ -181,7 +181,7 @@ class DraftOrderComplete(BaseMutation):errors = []order = cls.get_node_or_error(info, id, errors, 'id', Order)if order:- errors = check_for_draft_order_errors(order, errors)+ errors = can_finalize_draft_order(order, errors)if errors:return cls(errors=errors)diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex c5491722bc..3b077679cd 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -9,7 +9,7 @@ from ..core.types.common import CountableDjangoObjectTypefrom ..core.types.money import Money, TaxedMoneyfrom ..payment.types import OrderAction, Payment, PaymentChargeStatusEnumfrom ..shipping.types import ShippingMethod-from .utils import can_finalize_order+from .utils import can_finalize_draft_orderOrderEventsEnum = graphene.Enum.from_enum(OrderEvents)OrderEventsEmailsEnum = graphene.Enum.from_enum(OrderEventsEmails)@@ -177,7 +177,9 @@ class Order(CountableDjangoObjectType):description='The sum of line prices not including shipping.')status_display = graphene.String(description='User-friendly order status.')can_finalize = graphene.Boolean(- description='Informs if an order draft can be complete.')+ description=(+ 'Informs whether a draft order can be finalized',+ '(turned into a regular order).'))total_authorized = graphene.Field(Money, description='Amount authorized for the order.')total_captured = graphene.Field(@@ -289,7 +291,8 @@ class Order(CountableDjangoObjectType):@staticmethoddef resolve_can_finalize(self, info):- return can_finalize_order(self)+ errors = can_finalize_draft_order(self, [])+ return not errors@staticmethoddef resolve_user_email(self, info):diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.pyindex 69dc52f008..1dac6e1643 100644--- a/saleor/graphql/order/utils.py+++ b/saleor/graphql/order/utils.py@@ -1,7 +1,7 @@from ..core.types.common import Error-def check_for_draft_order_errors(order, errors):+def can_finalize_draft_order(order, errors):"""Return a list of errors associated with the order.Checks, if given order has a proper customer data, shipping@@ -30,12 +30,3 @@ def check_for_draft_order_errors(order, errors):field=None,message='Both user and user_email fields are null'))return errors---def can_finalize_order(order):- """Return `True` if an order draft can be complete.- """-- errors = []- errors = check_for_draft_order_errors(order, errors)- return not errorsdiff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 13b1057c6a..c9f076a86a 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -10,8 +10,7 @@ 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 (- can_finalize_order, check_for_draft_order_errors)+from saleor.graphql.order.utils import can_finalize_draft_orderfrom saleor.graphql.payment.types import PaymentChargeStatusEnumfrom saleor.order import OrderEvents, OrderEventsEmails, OrderStatusfrom saleor.order.models import Order, OrderEvent@@ -113,7 +112,7 @@ def test_order_query(content = get_graphql_content(response)order_data = content['data']['orders']['edges'][0]['node']assert order_data['number'] == str(order.pk)- assert order_data['canFinalize'] == True+ assert order_data['canFinalize'] is Trueassert order_data['status'] == order.status.upper()assert order_data['statusDisplay'] == order.get_status_display()assert order_data['paymentStatus'] == order.get_last_payment_status()@@ -356,32 +355,54 @@ def test_draft_order_delete(order.refresh_from_db()-def test_can_finalize_order(order_with_lines):- assert can_finalize_order(order_with_lines) == True+ORDER_CAN_FINALIZE_QUERY = """+ query OrderQuery($id: ID!){+ order(id: $id){+ canFinalize+ }+ }+"""++def test_can_finalize_order(+ staff_api_client, permission_manage_orders, order_with_lines):+ order_id = graphene.Node.to_global_id('Order', order_with_lines.id)+ variables = {'id': order_id}+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(+ ORDER_CAN_FINALIZE_QUERY, variables)+ content = get_graphql_content(response)+ assert content['data']['order']['canFinalize'] is True-def test_can_finalize_order_no_order_lines(order):- assert can_finalize_order(order) == False+def test_can_finalize_order_no_order_lines(+ staff_api_client, permission_manage_orders, order):+ order_id = graphene.Node.to_global_id('Order', order.id)+ variables = {'id': order_id}+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(+ ORDER_CAN_FINALIZE_QUERY, variables)+ content = get_graphql_content(response)+ assert content['data']['order']['canFinalize'] is False-def test_check_for_draft_order_errors(order_with_lines):- errors = check_for_draft_order_errors(order_with_lines, [])+def test_can_finalize_draft_order(order_with_lines):+ errors = can_finalize_draft_order(order_with_lines, [])assert not errors-def test_check_for_draft_order_errors_wrong_shipping(order_with_lines):+def test_can_finalize_draft_order_wrong_shipping(order_with_lines):order = order_with_linesshipping_zone = order.shipping_method.shipping_zoneshipping_zone.countries = ['DE']shipping_zone.save()assert order.shipping_address.country.code not in shipping_zone.countries- errors = check_for_draft_order_errors(order, [])+ errors = can_finalize_draft_order(order, [])msg = 'Shipping method is not valid for chosen shipping address'assert errors[0].message == msg-def test_check_for_draft_order_errors_no_order_lines(order):- errors = check_for_draft_order_errors(order, [])+def test_can_finalize_draft_order_no_order_lines(order):+ errors = can_finalize_draft_order(order, [])assert errors[0].message == 'Could not create order without any products.'