Skip to content

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

Add reorder and delete mutation

Adam Janika4cb07eea64d
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 058af9bd2e..ecc5a9992b 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -38,9 +38,10 @@ from .product.mutations import (
CollectionAddProducts, CollectionCreateMutation, CollectionDelete,
CollectionRemoveProducts, CollectionUpdate, ProductCreateMutation,
ProductDeleteMutation, ProductUpdateMutation, ProductTypeCreateMutation,
- ProductTypeDeleteMutation, ProductImageCreateMutation,
- ProductTypeUpdateMutation, ProductVariantCreateMutation,
- ProductVariantDeleteMutation, ProductVariantUpdateMutation)
+ ProductTypeDeleteMutation, ProductImageCreate, ProductImageDelete,
+ ProductImageReorder, ProductTypeUpdateMutation,
+ ProductVariantCreateMutation, ProductVariantDeleteMutation,
+ ProductVariantUpdateMutation)
from .product.resolvers import (
resolve_attributes, resolve_categories, resolve_collections,
resolve_products, resolve_product_types)
@@ -268,7 +269,10 @@ class Mutations(graphene.ObjectType):
product_delete = ProductDeleteMutation.Field()
product_update = ProductUpdateMutation.Field()
- product_image_create = ProductImageCreateMutation.Field()
+ product_image_create = ProductImageCreate.Field()
+ product_image_reorder = ProductImageReorder.Field()
+ product_image_delete = ProductImageDelete.Field()
+
product_type_create = ProductTypeCreateMutation.Field()
product_type_update = ProductTypeUpdateMutation.Field()
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.py
index 10bebb4a58..6cb0a13de9 100644
--- a/saleor/graphql/product/mutations.py
+++ b/saleor/graphql/product/mutations.py
@@ -8,7 +8,7 @@ from ...product import models
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.types import Decimal, Money
from ..utils import get_attributes_dict_from_list, get_node, get_nodes
-from .types import Collection, Product
+from .types import Collection, Product, ProductImage
class CategoryInput(graphene.InputObjectType):
@@ -384,7 +384,7 @@ class ProductImageInput(graphene.InputObjectType):
product = graphene.ID(description='ID of an product.')
-class ProductImageCreateMutation(ModelMutation):
+class ProductImageCreate(ModelMutation):
class Arguments:
input = ProductImageInput(
required=True,
@@ -397,3 +397,39 @@ class ProductImageCreateMutation(ModelMutation):
@classmethod
def user_is_allowed(cls, user, input):
return user.has_perm('product.edit_product')
+
+
+class ProductImageReorder(BaseMutation):
+ class Arguments:
+ images_ids = graphene.List(
+ graphene.ID, required=True,
+ description='IDs of a product images in the desired order.')
+
+ class Meta:
+ description = 'Changes ordering of the product image.'
+
+ product_images = graphene.List(
+ ProductImage, description='Product image which sort order will be altered.')
+
+ @permission_required('product.edit_product')
+ def mutate(self, info, images_ids):
+ product_images = get_nodes(images_ids, ProductImage)
+ for order, image_id in enumerate(images_ids):
+ image = get_node(info, image_id, only_type=ProductImage)
+ image.sort_order = order
+ image.save()
+ return ProductImageReorder(product_images=product_images)
+
+
+class ProductImageDelete(ModelDeleteMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of a product image to delete.')
+
+ class Meta:
+ description = 'Deletes a product image.'
+ model = models.ProductImage
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('product.edit_product')