Skip to content

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

Add mutations for delete and update drafto order

Adam Janikebef4489bee2
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index a2f57c2454..36c5b68037 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -29,7 +29,8 @@ from .core.types import Shop
from .order.filters import OrderFilter
from .order.resolvers import resolve_order, resolve_orders
from .order.types import Order
-from .order.mutations import DraftOrderCreate
+from .order.mutations import (
+ DraftOrderCreate, DraftOrderDelete, DraftOrderUpdate)
from .page.resolvers import resolve_pages, resolve_page
from .page.types import Page
from .page.mutations import PageCreate, PageDelete, PageUpdate
@@ -278,6 +279,8 @@ class Mutations(graphene.ObjectType):
menu_item_update = MenuItemUpdate.Field()
draft_order_create = DraftOrderCreate.Field()
+ draft_order_delete = DraftOrderDelete.Field()
+ draft_order_update = DraftOrderUpdate.Field()
page_create = PageCreate.Field()
page_delete = PageDelete.Field()
diff --git a/saleor/graphql/order/mutations.py b/saleor/graphql/order/mutations.py
index 20ec455695..1f225c0b6d 100644
--- a/saleor/graphql/order/mutations.py
+++ b/saleor/graphql/order/mutations.py
@@ -1,7 +1,8 @@
import graphene
from graphene.types import InputObjectType
-from ..core.mutations import BaseMutation
+from ...order import OrderStatus, models
+from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.types import Decimal
@@ -31,7 +32,7 @@ class LineInput(graphene.InputObjectType):
class DraftOrderInput(InputObjectType):
billing_address = AddressInput(
description='Address associated with the payment.')
- customer_id = graphene.ID(
+ user = graphene.ID(
descripton='Customer associated with the draft order.')
email = graphene.String(description='Email address of the customer.')
discount = Decimal(description='Discount amount for the order.')
@@ -41,16 +42,76 @@ class DraftOrderInput(InputObjectType):
and quantity of products.""")
shipping_address = AddressInput(
description='Address to where the order will be shipped.')
- shipping_method_id = graphene.ID('ID of a selected shipping method.')
+ shipping_method = graphene.ID(
+ description='ID of a selected shipping method.')
voucher = graphene.ID(
description='ID of the voucher associated with the order')
-class DraftOrderCreate(BaseMutation):
+class DraftOrderCreate(ModelMutation):
class Arguments:
- input = DraftOrderInput(required=True)
+ input = DraftOrderInput(
+ required=True,
+ description='Fields required to create an order.')
+ class Meta:
+ description = 'Creates a new variant for a product'
+ model = models.Order
+
+ @classmethod
+ def clean_input(cls, info, instance, input, errors):
+ cleaned_input = super().clean_input(info, instance, input, errors)
+ cleaned_input['status'] = OrderStatus.DRAFT
+ display_gross_prices = info.context.site.settings.display_gross_prices
+ cleaned_input['display_gross_prices'] = display_gross_prices
+
+ # Set up default addresses if possible
+ user = cleaned_input.get('user')
+ shipping_address = cleaned_input.get('shipping_address')
+ billing_address = cleaned_input.get('billing_address')
+ if user and not shipping_address:
+ cleaned_input[
+ 'shipping_address'] = user.default_shipping_address
+ if user and not billing_address:
+ cleaned_input[
+ 'billing_address'] = user.default_billing_address
+ return cleaned_input
+
+ @classmethod
+ def construct_instance(cls, instance, cleaned_data):
+ instance = super().construct_instance(instance, cleaned_data)
+ instance.shipping_address = cleaned_data.get('shipping_address')
+ instance.billing_address = cleaned_data.get('billing_address')
+ return instance
+
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('order.edit_order')
+
+
+class DraftOrderUpdate(ModelMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of an order to update.')
+ input = DraftOrderInput(
+ required=True,
+ description='Fields required to update an order.')
+
+ class Meta:
+ description = 'Updates an order.'
+ model = models.Order
+
+
+class DraftOrderDelete(ModelDeleteMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of a draft order to delete.')
+
+ class Meta:
+ description = 'Deletes a draft order.'
+ model = models.Order
@classmethod
- def mutate(cls, root, info, input):
- pass
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('product.edit_product')
diff --git a/saleor/order/models.py b/saleor/order/models.py
index f06e04cd3f..348ed7099b 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -74,7 +74,7 @@ class Order(models.Model):
net_field='shipping_price_net', gross_field='shipping_price_gross')
shipping_method_name = models.CharField(
max_length=255, null=True, default=None, blank=True, editable=False)
- token = models.CharField(max_length=36, unique=True)
+ token = models.CharField(max_length=36, unique=True, blank=True)
total_net = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12,
decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)
@@ -83,7 +83,8 @@ class Order(models.Model):
decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)
total = TaxedMoneyField(net_field='total_net', gross_field='total_gross')
voucher = models.ForeignKey(
- Voucher, null=True, related_name='+', on_delete=models.SET_NULL)
+ Voucher, blank=True, null=True, related_name='+',
+ on_delete=models.SET_NULL)
discount_amount = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12,
decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)