Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add product attributes mutations
akjanikcfa1f8bd9093
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex be3cd67254..78b3d22d4c 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -33,7 +33,11 @@ from .page.resolvers import resolve_pages, resolve_pagefrom .page.types import Pagefrom .page.mutations import PageCreate, PageDelete, PageUpdatefrom .product.filters import ProductFilterSet-from .product.mutations import (+from .product.mutations.attributes import (+ AttributeChoiceValueCreate, AttributeChoiceValueDelete,+ AttributeChoiceValueUpdate, ProductAttributeCreate, ProductAttributeDelete,+ ProductAttributeUpdate)+from .product.mutations.products import (CategoryCreateMutation, CategoryDelete, CategoryUpdateMutation,CollectionAddProducts, CollectionCreateMutation, CollectionDelete,CollectionRemoveProducts, CollectionUpdate, ProductCreateMutation,@@ -244,6 +248,10 @@ class Mutations(graphene.ObjectType):set_password = SetPassword.Field()+ attribute_choice_value_create = AttributeChoiceValueCreate.Field()+ attribute_choice_value_delete = AttributeChoiceValueDelete.Field()+ attribute_choice_value_update = AttributeChoiceValueUpdate.Field()+category_create = CategoryCreateMutation.Field()category_delete = CategoryDelete.Field()category_update = CategoryUpdateMutation.Field()@@ -272,6 +280,10 @@ class Mutations(graphene.ObjectType):page_delete = PageDelete.Field()page_update = PageUpdate.Field()+ product_attribute_create = ProductAttributeCreate.Field()+ product_attribute_delete = ProductAttributeDelete.Field()+ product_attribute_update = ProductAttributeUpdate.Field()+product_create = ProductCreateMutation.Field()product_delete = ProductDeleteMutation.Field()product_update = ProductUpdateMutation.Field()diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.pyindex e494fc0692..847e21fde4 100644--- a/saleor/graphql/core/mutations.py+++ b/saleor/graphql/core/mutations.py@@ -124,7 +124,7 @@ class ModelMutation(BaseMutation):cleaned_input[field_name] = instances# handle ID field- elif value is not None and field.type == graphene.ID:+ elif value is not None and (field.type == graphene.ID or field.type.of_type == graphene.ID):instance = get_node(info, value)cleaned_input[field_name] = instancediff --git a/saleor/graphql/product/mutations/__init__.py b/saleor/graphql/product/mutations/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.pynew file mode 100644index 0000000000..4818be89ce--- /dev/null+++ b/saleor/graphql/product/mutations/attributes.py@@ -0,0 +1,111 @@+import graphene++from ....product import models+from ...core.mutations import ModelDeleteMutation, ModelMutation+++class ProductAttributesInput(graphene.InputObjectType):+ slug = graphene.String(+ required=True, description='Internal name.')+ name = graphene.String(+ required=True, description='Attribute name.')+++class AttributeChoiceValueInput(graphene.InputObjectType):+ attribute = graphene.ID(+ required=True,+ description='Attribute to which value will be assigned.')+ slug = graphene.String(+ required=True, description='Internal name.')+ name = graphene.String(+ required=True, description='Value name.')+++class AttributeChoiceValueUpdateInput(graphene.InputObjectType):+ slug = graphene.String(+ required=True, description='Internal name.')+ name = graphene.String(+ required=True, description='Value name.')+++class ProductAttributeCreate(ModelMutation):+ class Arguments:+ input = ProductAttributesInput(+ required=True,+ description='Fields required to create a product attribute.')++ class Meta:+ description = 'Creates a product attribute.'+ model = models.ProductAttribute++ @classmethod+ def user_is_allowed(cls, user, input):+ return user.has_perm('product.edit_product')+++class ProductAttributeUpdate(ProductAttributeCreate):+ class Arguments:+ input = ProductAttributesInput(+ required=True,+ description='Fields required to update a product attribute.')++ class Meta:+ description = 'Updates a product attribute.'+ model = models.ProductAttribute+++class ProductAttributeDelete(ModelDeleteMutation):+ class Arguments:+ id = graphene.ID(+ required=True, description='ID of a product attribute to delete.')++ class Meta:+ description = 'Deletes a product attribute.'+ model = models.ProductAttribute++ @classmethod+ def user_is_allowed(cls, user, input):+ return user.has_perm('product.edit_product')+++class AttributeChoiceValueCreate(ModelMutation):+ class Arguments:+ input = AttributeChoiceValueInput(+ required=True,+ description='Fields required to create an attribute choice value.')++ class Meta:+ description = 'Creates an attribute choice value.'+ model = models.AttributeChoiceValue++ @classmethod+ def user_is_allowed(cls, user, input):+ return user.has_perm('product.edit_product')+++++class AttributeChoiceValueUpdate(AttributeChoiceValueCreate):+ class Arguments:+ input = AttributeChoiceValueUpdateInput(+ required=True,+ description='Fields required to update an attribute choice value.')++ class Meta:+ description = 'Updates an attribute choice value.'+ model = models.AttributeChoiceValue+++class AttributeChoiceValueDelete(ModelDeleteMutation):+ class Arguments:+ id = graphene.ID(+ required=True,+ description='ID of an attribute choice value to delete.')++ class Meta:+ description = 'Deletes an attribute choice value.'+ model = models.AttributeChoiceValue++ @classmethod+ def user_is_allowed(cls, user, input):+ return user.has_perm('product.edit_product')diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations/products.pysimilarity index 98%rename from saleor/graphql/product/mutations.pyrename to saleor/graphql/product/mutations/products.pyindex 88b40f44bf..1c1b1e4704 100644--- a/saleor/graphql/product/mutations.py+++ b/saleor/graphql/product/mutations/products.py@@ -4,11 +4,11 @@ from graphene.types import InputObjectTypefrom graphene_file_upload import Uploadfrom graphql_jwt.decorators import permission_required-from ...product import models-from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation-from ..core.types import Decimal, Error-from ..utils import get_attributes_dict_from_list, get_node, get_nodes-from .types import Collection, Product, ProductImage+from ....product import models+from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation+from ...core.types import Decimal, Error+from ...utils import get_attributes_dict_from_list, get_node, get_nodes+from ..types import Collection, Product, ProductImageclass CategoryInput(graphene.InputObjectType):