Skip to content

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

WIP

Pacu23b77039f1d95
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index 16c7ea862b..3fa13736c3 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -6,25 +6,18 @@ from ....dashboard.order.utils import fulfill_order_line
from ....order import OrderEvents, OrderEventsEmails, models
from ....order.emails import send_fulfillment_confirmation
from ....order.utils import cancel_fulfillment, update_order_status
-from ...core.mutations import BaseMutation, ModelMutation
+from ...core.mutations import BaseMutation
from ...order.types import Fulfillment, Order
-from ...utils import get_nodes
from ..types import OrderLine
-def clean_lines_quantities(order_lines, quantities):
- errors = []
- for order_line, quantity in zip(order_lines, quantities):
- if quantity > order_line.quantity_unfulfilled:
- msg = npgettext_lazy(
- 'Fulfill order line mutation error',
- '%(quantity)d item remaining to fulfill.',
- '%(quantity)d items remaining to fulfill.',
- 'quantity') % {
- 'quantity': order_line.quantity_unfulfilled,
- 'order_line': order_line}
- errors.append((order_line.variant.name, msg))
- return errors
+def send_fulfillment_confirmation_to_customer(order, fulfillment, user):
+ send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)
+ order.events.create(
+ parameters={
+ 'email': order.get_user_current_email(),
+ 'email_type': OrderEventsEmails.FULFILLMENT.value},
+ type=OrderEvents.EMAIL_SENT.value, user=user)
class FulfillmentLineInput(graphene.InputObjectType):
@@ -40,7 +33,8 @@ class FulfillmentCreateInput(graphene.InputObjectType):
notify_customer = graphene.Boolean(
description='If true, send an email notification to the customer.')
lines = graphene.List(
- FulfillmentLineInput, description='Item line to be fulfilled.')
+ FulfillmentLineInput, required=True,
+ description='Item line to be fulfilled.')
class FulfillmentUpdateTrackingInput(graphene.InputObjectType):
@@ -54,7 +48,7 @@ class FulfillmentCancelInput(graphene.InputObjectType):
restock = graphene.Boolean(description='Whether item lines are restocked.')
-class FulfillmentCreate(ModelMutation):
+class FulfillmentCreate(BaseMutation):
fulfillment = graphene.Field(
Fulfillment, description='A created fulfillment.')
order = graphene.Field(
@@ -69,76 +63,85 @@ class FulfillmentCreate(ModelMutation):
class Meta:
description = 'Creates a new fulfillment for an order.'
- model = models.Fulfillment
@classmethod
- def clean_input(cls, info, instance, input, errors):
- lines = input.pop('lines', None)
- cleaned_input = super().clean_input(info, instance, input, errors)
- if lines:
- lines_ids = [line.get('order_line_id') for line in lines]
- quantities = [line.get('quantity') for line in lines]
- order_lines = get_nodes(
- ids=lines_ids, graphene_type=OrderLine)
- line_errors = clean_lines_quantities(order_lines, quantities)
- if line_errors:
- for err in line_errors:
- cls.add_error(errors, field=err[0], message=err[1])
- else:
- cleaned_input['order_lines'] = order_lines
- cleaned_input['quantities'] = quantities
- return cleaned_input
+ def clean_lines(cls, order_lines, quantities, errors):
+ for order_line, quantity in zip(order_lines, quantities):
+ if quantity <= 0:
+ cls.add_error(
+ errors, order_line,
+ 'Quantity must be larger than 0.')
+ elif quantity > order_line.quantity_unfulfilled:
+ msg = npgettext_lazy(
+ 'Fulfill order line mutation error',
+ '%(quantity)d item remaining to fulfill.',
+ '%(quantity)d items remaining to fulfill.',
+ 'quantity') % {
+ 'quantity': order_line.quantity_unfulfilled,
+ 'order_line': order_line}
+ cls.add_error(errors, order_line.variant.name, msg)
+ return errors
@classmethod
- def user_is_allowed(cls, user, input):
- return user.has_perm('order.manage_orders')
-
- @classmethod
- def construct_instance(cls, instance, cleaned_data):
- instance.order = cleaned_data.get('order') or instance.order
- return super().construct_instance(instance, cleaned_data)
+ def clean_input(cls, input, errors):
+ lines = input['lines']
+ quantities = [line['quantity'] for line in lines]
+ lines_ids = [line['order_line_id'] for line in lines]
+ order_lines = cls.get_nodes_or_error(
+ lines_ids, errors, 'lines', OrderLine)
+
+ cls.clean_lines(order_lines, quantities, errors)
+ if errors:
+ return cls(errors=errors)
+ else:
+ input['order_lines'] = order_lines
+ input['quantities'] = quantities
+ return input
@classmethod
- def save(cls, info, instance, cleaned_input):
+ def save(cls, user, fulfillment, order, cleaned_input):
+ fulfillment.save()
order_lines = cleaned_input.get('order_lines')
quantities = cleaned_input.get('quantities')
- super().save(info, instance, cleaned_input)
- order = instance.order
- if order_lines and quantities:
- quantity_fulfilled = 0
- lines_to_fulfill = [
- (order_line, quantity) for order_line, quantity
- in zip(order_lines, quantities) if quantity > 0]
- for line in lines_to_fulfill:
- order_line = line[0]
- quantity = line[1]
- fulfill_order_line(order_line, quantity)
- quantity_fulfilled += quantity
- fulfillment_lines = [
+
+ fulfillment_lines = []
+ for order_line, quantity in zip(order_lines, quantities):
+ fulfill_order_line(order_line, quantity)
+ fulfillment_lines.append(
models.FulfillmentLine(
- order_line=line[0],
- fulfillment=instance,
- quantity=line[1]) for line in lines_to_fulfill]
- models.FulfillmentLine.objects.bulk_create(fulfillment_lines)
- update_order_status(order)
- order.events.create(
- parameters={'quantity': quantity_fulfilled},
- type=OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value,
- user=info.context.user)
- super().save(info, instance, cleaned_input)
+ order_line=order_line, fulfillment=fulfillment,
+ quantity=quantity))
+
+ fulfillment.lines.bulk_create(fulfillment_lines)
+ update_order_status(order)
+ order.events.create(
+ parameters={'quantity': sum(quantities)},
+ type=OrderEvents.FULFILLMENT_FULFILLED_ITEMS.value,
+ user=user)
if cleaned_input.get('notify_customer'):
- send_fulfillment_confirmation.delay(order.pk, instance.pk)
- order.events.create(
- parameters={
- 'email': order.get_user_current_email(),
- 'email_type': OrderEventsEmails.FULFILLMENT.value},
- type=OrderEvents.EMAIL_SENT.value,
- user=info.context.user)
- return FulfillmentCreate(fulfillment=instance, order=order)
+ send_fulfillment_confirmation_to_customer(
+ order, fulfillment, user)
+ return FulfillmentCreate(fulfillment=fulfillment, order=order)
+
+ @classmethod
+ @permission_required('order.manage_orders')
+ def mutate(cls, root, info, order, input):
+ errors = []
+ order = cls.get_node_or_error(
+ info, order, errors, 'order', Order)
+ if errors:
+ return cls(errors=errors)
+ fulfillment = models.Fulfillment(
+ tracking_number=input.pop('tracking_number') or '', order=order)
+ cleaned_input = cls.clean_input(input, errors)
+ if errors:
+ return cls(errors=errors)
+ cls.save(info.context.user, fulfillment, order, cleaned_input)
+ return FulfillmentCreate(fulfillment=fulfillment, order=order)
-class FulfillmentUpdateTracking(FulfillmentCreate):
+class FulfillmentUpdateTracking(BaseMutation):
fulfillment = graphene.Field(
Fulfillment, description='A fulfillment with updated tracking.')
order = graphene.Field(
@@ -153,7 +156,26 @@ class FulfillmentUpdateTracking(FulfillmentCreate):
class Meta:
description = 'Updates a fulfillment for an order.'
- model = models.Fulfillment
+
+ @classmethod
+ @permission_required('order.manage_orders')
+ def mutate(cls, root, info, id, input):
+ errors = []
+ fulfillment = cls.get_node_or_error(
+ info, id, errors, 'id', Fulfillment)
+ if errors:
+ return cls(errors=errors)
+ tracking_number = input.get('tracking_number')
+ fulfillment.tracking_number = input.get('tracking_number')
+ fulfillment.save()
+ order = fulfillment.order
+ order.events.create(
+ parameters={
+ 'tracking_numner': tracking_number,
+ 'fulfillment': fulfillment.composed_id},
+ type=OrderEvents.TRACKING_UPDATED.value,
+ user=info.context.user)
+ return FulfillmentUpdateTracking(fulfillment=fulfillment, order=order)
class FulfillmentCancel(BaseMutation):
diff --git a/saleor/order/__init__.py b/saleor/order/__init__.py
index 09591c9a49..cc684df2eb 100644
--- a/saleor/order/__init__.py
+++ b/saleor/order/__init__.py
@@ -80,7 +80,7 @@ class OrderEvents(Enum):
FULFILLMENT_CANCELED = 'fulfillment_canceled'
FULFILLMENT_RESTOCKED_ITEMS = 'restocked_items'
FULFILLMENT_FULFILLED_ITEMS = 'fulfilled_items'
-
+ TRACKING_UPDATED = 'tracking_updated'
NOTE_ADDED = 'note_added'
# Used mostly for importing legacy data from before Enum-based events
@@ -191,6 +191,14 @@ def display_order_event(order_event):
'Dashboard message related to an order',
'Order details were updated by %(user_name)s' % {
'user_name': order_event.user})
+ if event_type == OrderEvents.TRACKING_UPDATED.value:
+ return pgettext_lazy(
+ 'Fulfillment #%(fulfillment)s tracking was updated to'
+ ' %(tracking_number)s by %(user_name)s') % {
+ 'fulfillment': params['composed_id'],
+ 'tracking_number': params['tracking_number'],
+ 'user_name': order_event.user}
+
if event_type == OrderEvents.OTHER.value:
return order_event.parameters['message']
raise ValueError('Not supported event type: %s' % (event_type))
diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.py
index 27dc175138..22971a936c 100644
--- a/tests/api/test_fulfillment.py
+++ b/tests/api/test_fulfillment.py
@@ -10,22 +10,31 @@ from tests.utils import get_graphql_content
def test_create_fulfillment(admin_api_client, order_with_lines, admin_user):
order = order_with_lines
query = """
- mutation fulfillOrder(
- $order: ID, $lines: [FulfillmentLineInput], $tracking: String,
- $notify: Boolean) {
+ mutation fulfillOrder(
+ $order: ID, $lines: [FulfillmentLineInput]!, $tracking: String,
+ $notify: Boolean
+ ) {
orderFulfillmentCreate(
- order: $order, input: {lines: $lines,
- trackingNumber: $tracking, notifyCustomer: $notify}) {
- fulfillment {
- fulfillmentOrder
- status
- trackingNumber
- lines {
- totalCount
- }
- }
+ order: $order,
+ input: {
+ lines: $lines, trackingNumber: $tracking,
+ notifyCustomer: $notify}
+ ) {
+ errors {
+ field
+ message
+ }
+ fulfillment {
+ fulfillmentOrder
+ status
+ trackingNumber
+ lines {
+ totalCount
}
+ }
}
+ }
+
"""
order_id = graphene.Node.to_global_id('Order', order.id)
order_line = order.lines.first()
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index e039bb0cbf..8756a10a91 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -1232,7 +1232,9 @@ def test_order_event_display(admin_user, type, order):
'quantity': 12,
'email_type': OrderEventsEmails.PAYMENT.value,
'email': '[email protected]',
- 'amount': '80.00', 'composed_id': 12}
+ 'amount': '80.00',
+ 'composed_id': 12,
+ 'tracking_number': '5421AB'}
event = OrderEvent(
user=admin_user, order=order, parameters=parameters, type=type)
event.get_event_display()