Skip to content

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

Set up shipping mutations

Adam Janikccf8b2b6a58e
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 99cd6e17a1..058af9bd2e 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -48,6 +48,9 @@ from .product.types import (
Category, Collection, Product, ProductAttribute, ProductType)
from .shipping.resolvers import resolve_shipping_methods
from .shipping.types import ShippingMethod
+from .shipping.mutations import (
+ ShippingMethodCreate, ShippingMethodDelete, ShippingMethodUpdate,
+ ShippingPriceCreate, ShippingPriceDelete, ShippingPriceUpdate)
from .utils import get_node
@@ -283,5 +286,13 @@ class Mutations(graphene.ObjectType):
voucher_delete = VoucherDelete.Field()
voucher_update = VoucherUpdate.Field()
+ shipping_method_create = ShippingMethodCreate.Field()
+ shipping_method_delete = ShippingMethodDelete.Field()
+ shipping_method_update = ShippingMethodUpdate.Field()
+
+ shipping_price_create = ShippingPriceCreate.Field()
+ shipping_price_delete = ShippingPriceDelete.Field()
+ shipping_price_update = ShippingPriceUpdate.Field()
+
schema = graphene.Schema(Query, Mutations)
diff --git a/saleor/graphql/core/types.py b/saleor/graphql/core/types.py
index e35d5bec19..eac819f359 100644
--- a/saleor/graphql/core/types.py
+++ b/saleor/graphql/core/types.py
@@ -16,8 +16,7 @@ class Decimal(Scalar):
@staticmethod
def serialize(dec):
assert isinstance(dec, decimal.Decimal), (
- 'Received not compatible Decimal "{}"'.format(repr(dec))
- )
+ 'Received not compatible Decimal "{}"'.format(repr(dec)))
return str(dec)
@staticmethod
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
new file mode 100644
index 0000000000..c46613743d
--- /dev/null
+++ b/saleor/graphql/shipping/mutations.py
@@ -0,0 +1,109 @@
+import graphene
+
+from ...shipping import models
+from ..core.mutations import ModelDeleteMutation, ModelMutation
+from ..core.types import Decimal
+
+
+class ShippingPriceInput(graphene.InputObjectType):
+ country_code = graphene.String(description='Shipping specific country.')
+ price = Decimal(description='Shipping price to a specified country.')
+ shipping_method = graphene.ID(description='Related shipping method.')
+
+
+class ShippingMethodInput(graphene.InputObjectType):
+ name = graphene.String(description='Shipping method\'s name.')
+ description = graphene.String(
+ description='Optional short description of the method')
+
+
+class ShippingMethodCreate(ModelMutation):
+ class Arguments:
+ input = ShippingMethodInput(
+ description='Fields required to create a shipping method',
+ required=True)
+
+ class Meta:
+ description = 'Creates a new shipping method'
+ model = models.ShippingMethod
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('shipping.edit_shipping')
+
+
+class ShippingMethodUpdate(ModelMutation):
+ class Arguments:
+ id = graphene.ID(
+ description='ID of a shipping method to update.', required=True)
+ input = ShippingMethodInput(
+ description='Fields required to update a shipping method',
+ required=True)
+
+ class Meta:
+ description = 'Updates a new shipping method'
+ model = models.ShippingMethod
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('shipping.edit_shipping')
+
+
+class ShippingMethodDelete(ModelDeleteMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of a shipping method to delete.')
+
+ class Meta:
+ description = 'Deletes a shipping method.'
+ model = models.ShippingMethod
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('shipping.edit_shipping')
+
+
+class ShippingPriceCreate(ModelMutation):
+ class Arguments:
+ input = ShippingPriceInput(
+ description='Fields required to create a shipping price',
+ required=True)
+
+ class Meta:
+ description = 'Creates a new shipping price'
+ model = models.ShippingMethodCountry
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('shipping.edit_shipping')
+
+
+class ShippingPriceUpdate(ModelMutation):
+ class Arguments:
+ id = graphene.ID(
+ description='ID of a shipping price to update.', required=True)
+ input = ShippingPriceInput(
+ description='Fields required to update a shipping price',
+ required=True)
+
+ class Meta:
+ description = 'Updates a new shipping price'
+ model = models.ShippingMethodCountry
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('shipping.edit_shipping')
+
+
+class ShippingPriceDelete(ModelDeleteMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of a shipping price to delete.')
+
+ class Meta:
+ description = 'Deletes a shipping price.'
+ model = models.ShippingMethodCountry
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('shipping.edit_page')