Skip to content

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

Update Attribtue and AttributeValue mutations

Pacu288dff3b8f337
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 2f88c288cd..1a3254dbf3 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -1,37 +1,32 @@
import graphene
+from django.core.exceptions import ValidationError
+from django.db.models import Q
+from django.template.defaultfilters import slugify
+from graphql_jwt.decorators import permission_required
from ....product import models
-from ...core.mutations import ModelDeleteMutation, ModelMutation
-from django.core.exceptions import ValidationError
+from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
+from ..types import Attribute, AttributeValue
-class AttributeValueCreateInput(graphene.InputObjectType):
- attribute = graphene.ID(
- required=False,
- description='Attribute to which value will be assigned.',
- name='attribute')
- slug = graphene.String(
- required=True, description='Internal name.')
+class AttributeCreateValueInput(graphene.InputObjectType):
name = graphene.String(
required=True, description='Name displayed in the interface.')
value = graphene.String(
required=True, description='Real value eg. HEX color.')
-class AttributeInput(graphene.InputObjectType):
- slug = graphene.String(
- required=True, description='Internal name.')
+class AttributeCreateInput(graphene.InputObjectType):
name = graphene.String(
required=True, description='Name displayed in the interface.')
-
-
-class AttributeCreateInput(AttributeInput):
values = graphene.List(
- AttributeValueCreateInput,
+ AttributeCreateValueInput,
description='Attribute values to be created for this attribute.')
class AttributeCreate(ModelMutation):
+ ATTRIBUTE_VALUES_FIELD = 'values'
+
class Arguments:
input = AttributeCreateInput(
required=True,
@@ -42,45 +37,92 @@ class AttributeCreate(ModelMutation):
model = models.Attribute
@classmethod
- def user_is_allowed(cls, user, input):
- return user.has_perm('product.manage_products')
+ def clean_attribute_value_uniqueness(cls, values, errors, error_msg):
+ """Checks if all provided values are unique."""
+ if len(set(values)) != len(values):
+ cls.add_error(errors, cls.ATTRIBUTE_VALUES_FIELD, error_msg)
+ return errors
@classmethod
- def clean_input(cls, info, instance, input, errors):
- cleaned_input = super().clean_input(info, instance, input, errors)
-
- values = cleaned_input.get('values', [])
- names = [value['name'] for value in values]
- if len(set(names)) != len(names):
- cls.add_error(
- errors, 'values', 'Duplicated attribute value names provided.')
+ def clean_attribute_values(cls, values, errors):
+ """Validates if a valid AttributeValue instance can be created using
+ the data provided.
+ """
for value_data in values:
+ value_data['slug'] = slugify(value_data['name'])
attribute_value = models.AttributeValue(**value_data)
try:
attribute_value.full_clean()
except ValidationError as validation_errors:
for field in validation_errors.message_dict:
- # Attribute instance is not created yet so we cannot add it
if field == 'attribute':
continue
for message in validation_errors.message_dict[field]:
- error_field = 'values:%(field)s' % {'field': field}
+ error_field = '%(values_field)s:%(field)s' % {
+ 'values_field': cls.ATTRIBUTE_VALUES_FIELD,
+ 'field': field}
cls.add_error(errors, error_field, message)
+ return errors
+
+ @classmethod
+ def clean_input(cls, info, instance, input, errors):
+ cleaned_input = super().clean_input(info, instance, input, errors)
+ slug = slugify(cleaned_input['name'])
+ cleaned_input['slug'] = slug
+
+ # Slugs are created automatically from the names, but those should be
+ # unique.
+ attribute_with_same_slug_exists = instance._meta.model.objects.filter(
+ slug=slug).exclude(pk=getattr(instance, 'pk', None)).exists()
+ if attribute_with_same_slug_exists:
+ cls.add_error(errors, 'name', 'Attribute\'s name is not unique.')
+
+ values = cleaned_input.get(cls.ATTRIBUTE_VALUES_FIELD, [])
+ if not values:
+ return cleaned_input
+
+ # All provided names should be unique to each other
+ cls.clean_attribute_value_uniqueness(
+ [v['name'] for v in values], errors,
+ 'Duplicated AttributeValue names provided.')
+
+ slugs = []
+ for value in values:
+ value['slug'] = slugify(value['name'])
+ slugs.append(value['slug'])
+ # All provided names should resolve to unique slugs
+ cls.clean_attribute_value_uniqueness(
+ slugs, errors, 'Provided AttributeValue names are not unique.')
+
+ cls.clean_attribute_values(values, errors)
return cleaned_input
@classmethod
def _save_m2m(cls, info, instance, cleaned_data):
super()._save_m2m(info, instance, cleaned_data)
- values = cleaned_data.get('values', [])
+ values = cleaned_data.get(cls.ATTRIBUTE_VALUES_FIELD, [])
for value in values:
instance.values.create(**value)
+class AttributeUpdateInput(graphene.InputObjectType):
+ name = graphene.String(
+ description='Name displayed in the interface.')
+ remove_values = graphene.List(
+ graphene.ID, name='removeValues', required=True,
+ description='List of attributes to be removed from this attribute.')
+ add_values = graphene.List(
+ AttributeCreateValueInput, name='addValues', required=True,
+ description='Attribute values to be created for this attribute.')
+
+
class AttributeUpdate(AttributeCreate):
+ ATTRIBUTE_VALUES_FIELD = 'add_values'
+
class Arguments:
id = graphene.ID(
required=True, description='ID of an attribute to update.')
- input = AttributeInput(
+ input = AttributeUpdateInput(
required=True,
description='Fields required to update an attribute.')
@@ -88,6 +130,57 @@ class AttributeUpdate(AttributeCreate):
description = 'Updates attribute.'
model = models.Attribute
+ @classmethod
+ def clean_remove_values(cls, cleaned_input, instance, errors):
+ """Check if AttributeValues to be removed are assigned to given
+ Attribute.
+ """
+ remove_values = cleaned_input.get('remove_values', [])
+ if not remove_values:
+ return []
+
+ attribute_values = cls.get_nodes_or_error(
+ ids=remove_values, errors=errors,
+ only_type=AttributeValue, field=cls.ATTRIBUTE_VALUES_FIELD)
+ if attribute_values:
+ for value in attribute_values:
+ if value.attribute != instance:
+ cls.add_error(
+ errors, 'remove_values:%s' % value,
+ 'AttributeValue does not belong to this Attribute.')
+ return attribute_values
+
+ @classmethod
+ def clean_add_values(cls, cleaned_input, instance, errors):
+ """Check if AttributeValue with the same slug or name already exists.
+ """
+ existing_attribute_values = instance.values.values_list('slug', 'name')
+ existing_slugs, existing_names = zip(*existing_attribute_values)
+ for value in cleaned_input.get('add_values', []):
+ if value['name'] in existing_names:
+ cls.add_error(
+ errors, 'add_values:%s' % value['name'],
+ 'AttributeValue with given name already exists.')
+ if value['slug'] in existing_slugs:
+ cls.add_error(
+ errors, 'add_values:%s' % value['slug'],
+ 'AttributeValue name is not unique.')
+ return errors
+
+ @classmethod
+ def clean_input(cls, info, instance, input, errors):
+ cleaned_input = super().clean_input(info, instance, input, errors)
+ cleaned_input['remove_values'] = cls.clean_remove_values(
+ cleaned_input, instance, errors)
+ cls.clean_add_values(cleaned_input, instance, errors)
+ return cleaned_input
+
+ @classmethod
+ def _save_m2m(cls, info, instance, cleaned_data):
+ super()._save_m2m(info, instance, cleaned_data)
+ for attribute_value in cleaned_data.get('remove_values', []):
+ attribute_value.delete()
+
class AttributeDelete(ModelDeleteMutation):
class Arguments:
@@ -103,43 +196,106 @@ class AttributeDelete(ModelDeleteMutation):
return user.has_perm('product.manage_products')
-class AttributeValueCreate(ModelMutation):
+class AttributeValueCreateInput(graphene.InputObjectType):
+ name = graphene.String(
+ required=True, description='Name displayed in the interface.')
+ value = graphene.String(
+ required=True, description='Real value eg. HEX color.')
+
+
+class AttributeValueCreate(BaseMutation):
+ attribute = graphene.Field(
+ Attribute, description='A related Attribute.')
+ attribute_value = graphene.Field(
+ AttributeValue, description='Created AttributeValue.')
+
class Arguments:
+ id = graphene.ID(
+ required=True, name='attribute',
+ description='Attribute to which value will be assigned.')
input = AttributeValueCreateInput(
required=True,
- description='Fields required to create an attribute choice value.')
+ description='Fields required to create an AttributeValue.')
class Meta:
- description = 'Creates an attribute choice value.'
- model = models.AttributeValue
+ description = 'Creates an AttributeValue.'
@classmethod
- def user_is_allowed(cls, user, input):
- return user.has_perm('product.manage_products')
+ def clean_input(
+ cls, name, slug, errors, attribute, attribute_value_pk=None):
+ other_values = attribute.values.exclude(pk=attribute_value_pk)
+ duplicated_values_exists = other_values.filter(
+ Q(name=name) | Q(slug=slug)).exists()
+ if duplicated_values_exists:
+ cls.add_error(errors, 'name', 'Provided name is not unique.')
+ return errors
+
+ @classmethod
+ @permission_required('product.manage_products')
+ def mutate(cls, root, info, id, input):
+ errors = []
+ attribute = cls.get_node_or_error(info, id, errors, 'id', Attribute)
+ if not attribute:
+ return AttributeValueCreate(errors=errors)
+
+ name = input['name']
+ slug = slugify(name)
+ cls.clean_input(name, slug, errors, attribute, attribute_value_pk=None)
+ if errors:
+ return AttributeValueCreate(errors=errors)
+
+ attribute_value = attribute.values.create(
+ name=name, value=input['value'], slug=slug)
+ return AttributeValueCreate(
+ attribute=attribute, attribute_value=attribute_value)
class AttributeValueUpdate(AttributeValueCreate):
class Arguments:
id = graphene.ID(
required=True,
- description='ID of an attribute choice value to update.')
- input = AttributeInput(
+ description='ID of an AttributeValue to update.')
+ input = AttributeValueCreateInput(
required=True,
- description='Fields required to update an attribute choice value.')
+ description='Fields required to update an AttributeValue.')
class Meta:
- description = 'Updates an attribute choice value.'
- model = models.AttributeValue
+ description = 'Updates an AttributeValue.'
+
+ @classmethod
+ @permission_required('product.manage_products')
+ def mutate(cls, root, info, id, input):
+ errors = []
+ attribute_value = cls.get_node_or_error(
+ info, id, errors, 'id', AttributeValue)
+ if not attribute_value:
+ return AttributeValueUpdate(errors=errors)
+
+ attribute = attribute_value.attribute
+ name = input['name']
+ slug = slugify(name)
+ cls.clean_input(
+ name, slug, errors, attribute,
+ attribute_value_pk=attribute_value.pk)
+ if errors:
+ return AttributeValueUpdate(errors=errors)
+
+ attribute_value.name = name
+ attribute_value.value = input['value']
+ attribute_value.slug = slug
+ attribute_value.save(update_fields=['slug', 'name', 'value'])
+ return AttributeValueUpdate(
+ attribute=attribute, attribute_value=attribute_value)
class AttributeValueDelete(ModelDeleteMutation):
class Arguments:
id = graphene.ID(
required=True,
- description='ID of an attribute choice value to delete.')
+ description='ID of an AttributeValue to delete.')
class Meta:
- description = 'Deletes an attribute choice value.'
+ description = 'Deletes an AttributeValue.'
model = models.AttributeValue
@classmethod