Skip to content

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

WIP Add mutations to manage variant images

Marcin Gębala03c7df00eb41
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 72379a04e0..0268a544d7 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -49,7 +49,7 @@ from .product.mutations.products import (
ProductTypeDelete, ProductImageCreate, ProductImageDelete,
ProductImageReorder, ProductImageUpdate, ProductTypeUpdate,
ProductVariantCreate, ProductVariantDelete,
- ProductVariantUpdate)
+ ProductVariantUpdate, VariantImageAssign, VariantImageDelete)
from .product.resolvers import (
resolve_attributes, resolve_categories, resolve_collections,
resolve_products, resolve_product_types)
@@ -336,5 +336,8 @@ class Mutations(graphene.ObjectType):
shipping_price_delete = ShippingPriceDelete.Field()
shipping_price_update = ShippingPriceUpdate.Field()
+ variant_image_assign = VariantImageAssign.Field()
+ variant_image_delete = VariantImageDelete.Field()
+
schema = graphene.Schema(Query, Mutations)
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 56348839bd..87ec31c162 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -476,6 +476,10 @@ class ProductImageUpdate(ModelMutation):
description = 'Updates a product image.'
model = models.ProductImage
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('product.manage_products')
+
class ProductImageReorder(BaseMutation):
class Arguments:
@@ -522,3 +526,51 @@ class ProductImageDelete(ModelDeleteMutation):
@classmethod
def user_is_allowed(cls, user, input):
return user.has_perm('product.manage_products')
+
+
+class VariantImageAssignInput(graphene.InputObjectType):
+ variant = graphene.ID(
+ name='variant',
+ description='ID of a product variant to assign the image to.')
+ image = graphene.ID(
+ name='image',
+ description='ID of a product image to assign to the variant.')
+
+
+class VariantImageAssign(ModelMutation):
+ class Arguments:
+ input = VariantImageAssignInput(
+ required=True,
+ description='''
+ Fields required to assign an image to a product variant.''')
+
+ class Meta:
+ description = 'Assign an image to a product variant'
+ model = models.VariantImage
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('product.manage_products')
+
+ @classmethod
+ def clean_input(cls, info, instance, input, errors):
+ cleaned_input = super().clean_input(info, instance, input, errors)
+ image = cleaned_input.get('image')
+ variant = cleaned_input.get('variant')
+ if image and variant and not variant.product.images.filter(pk=image.pk).first():
+ cls.add_error(errors, 'image', 'Image must be for this product')
+ return cleaned_input
+
+
+class VariantImageDelete(ModelDeleteMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of a variant image to delete.')
+
+ class Meta:
+ description = 'Unassign an image from a variant.'
+ model = models.VariantImage
+
+ @classmethod
+ def user_is_allowed(cls, user, input):
+ return user.has_perm('product.manage_products')
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 92f383cdfa..dfe23b8328 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -273,3 +273,10 @@ class ProductImage(CountableDjangoObjectType):
if size:
return get_thumbnail(self.image, size, 'crop')
return self.image.url
+
+
+class VariantImage(CountableDjangoObjectType):
+ class Meta:
+ description = 'Represents a variant image'
+ interfaces = [relay.Node]
+ model = models.VariantImage
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 94083bf3bf..65806d9ad0 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -10,9 +10,9 @@ from tests.utils import (
create_image, create_pdf_file_with_image_ext, get_graphql_content)
from saleor.product.models import (
- Category, Collection, Product, ProductAttribute, ProductType)
+ Category, Collection, Product, ProductAttribute, ProductType, ProductImage)
-from .utils import get_multipart_request_body
+from .utils import assert_no_permission, get_multipart_request_body
def test_fetch_all_products(user_api_client, product):
@@ -1111,3 +1111,50 @@ def test_remove_products_to_collection(
data = content['data']['collectionRemoveProducts']['collection']
assert data[
'products']['totalCount'] == no_products_before - len(product_ids)
+
+
+
+def test_assign_variant_image(admin_api_client, user_api_client, product_with_image):
+ query = """
+ mutation assignVariantImageMutation($variant: ID!, $image: ID!) {
+ variantImageAssign(input: {
+ variant: $variant
+ image: $image
+ }) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ variant = product_with_image.variants.first()
+ image = product_with_image.images.first()
+
+ variables = json.dumps({
+ 'variant': to_global_id('ProductVariant', variant.pk),
+ 'image': to_global_id('ProductImage', image.pk)})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ variant.refresh_from_db()
+ assert variant.images.first() == image
+
+ # check that assigning an image from a different product is not allowed
+ product_with_image.pk = None
+ product_with_image.save()
+
+ image_2 = ProductImage.objects.create(product=product_with_image)
+ variables = json.dumps({
+ 'variant': to_global_id('ProductVariant', variant.pk),
+ 'image': to_global_id('ProductImage', image_2.pk)})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert content['data']['variantImageAssign']['errors'][0]['field'] == 'image'
+
+ # check permissions
+ response = user_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ assert_no_permission(response)