Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add product update mutation
Adam Janik1c4097fc12c3
diff --git a/saleor/dashboard/graphql/api.py b/saleor/dashboard/graphql/api.pyindex 42c695e1ed..c3a54495f0 100644--- a/saleor/dashboard/graphql/api.py+++ b/saleor/dashboard/graphql/api.py@@ -14,7 +14,7 @@ from .page.types import resolve_all_pagesfrom .product.filters import ProductFilterfrom .product.mutations import (CategoryCreateMutation, CategoryDelete, CategoryUpdateMutation,- ProductCreateMutation, ProductDeleteMutation)+ ProductCreateMutation, ProductDeleteMutation, ProductUpdateMutation)class Query(graphene.ObjectType):@@ -73,6 +73,7 @@ class Mutations(graphene.ObjectType):product_create = ProductCreateMutation.Field()product_delete = ProductDeleteMutation.Field()+ product_update = ProductUpdateMutation.Field()token_auth = graphql_jwt.ObtainJSONWebToken.Field()verify_token = graphql_jwt.Verify.Field()diff --git a/saleor/dashboard/graphql/mutations.py b/saleor/dashboard/graphql/mutations.pyindex 85a989c22c..eb6463fcff 100644--- a/saleor/dashboard/graphql/mutations.py+++ b/saleor/dashboard/graphql/mutations.py@@ -9,6 +9,7 @@ from graphql_jwt.decorators import staff_member_requiredfrom ...graphql.core.types import Errorfrom ...graphql.utils import get_node+from .utils import get_attributes_dict_from_listregistry = get_global_registry()@@ -147,8 +148,11 @@ class ModelFormMutation(BaseMutation):@classmethoddef mutate(cls, root, info, **kwargs):form_kwargs = cls.get_form_kwargs(root, info, **kwargs)+ attributes = form_kwargs.get('data').pop('attributes', None)form = cls._meta.form_class(**form_kwargs)if form.is_valid():+ if attributes:+ form.instance.attributes = get_attributes_dict_from_list(attributes)instance = form.save()kwargs = {cls._meta.return_field_name: instance}return cls(errors=[], **kwargs)diff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.pyindex 9a62a00430..98a554e8fc 100644--- a/saleor/dashboard/graphql/product/mutations.py+++ b/saleor/dashboard/graphql/product/mutations.py@@ -75,38 +75,27 @@ class ProductCreateMutation(ModelFormMutation):kwargs['data']['category'] = category.idreturn kwargs- @classmethod- def mutate(cls, root, info, **kwargs):- form_kwargs = cls.get_form_kwargs(root, info, **kwargs)- attributes = form_kwargs.get('data').pop('attributes', None)- if attributes:- attr_ids = {}- attr_name_id = dict(- models.ProductAttribute.objects.values_list('name', 'id'))- value_name_id = dict(- models.AttributeChoiceValue.objects.values_list('name', 'id'))- for attribute in attributes:- attr_name = attribute.get('name')- if attr_name not in attr_name_id:- raise ValueError(- 'Unknown attribute name: %r' % (attr_name,))- attr_value = attribute.get('value')- attr_ids[attr_name_id.get(- attr_name)] = value_name_id.get(attr_value)-- form = cls._meta.form_class(**form_kwargs)- if form.is_valid():- instance = form.instance- instance.attributes = attr_ids- instance.save()- kwargs = {cls._meta.return_field_name: instance}- return cls(errors=[], **kwargs)- errors = convert_form_errors(form)- return cls(errors=errors)- return super().mutate(root, info, **kwargs)-class ProductDeleteMutation(ModelDeleteMutation):class Meta:model = models.Product+++class ProductUpdateMutation(ModelFormUpdateMutation):+ class Arguments:+ attributes = graphene.Argument(graphene.List(ValuesInput))++ class Meta:+ form_class = ProductForm+ # Exclude from input form fields+ # that are being overwritten by arguments+ exclude = ['product_type', 'category', 'attributes']++ @classmethod+ def get_form_kwargs(cls, root, info, **input):+ kwargs = super().get_form_kwargs(root, info, **input)+ instance = kwargs.get('instance')+ kwargs['data']['product_type'] = instance.product_type.id+ kwargs['data']['category'] = instance.category.id+ return kwargsdiff --git a/saleor/dashboard/graphql/utils.py b/saleor/dashboard/graphql/utils.pynew file mode 100644index 0000000000..50133808fa--- /dev/null+++ b/saleor/dashboard/graphql/utils.py@@ -0,0 +1,24 @@+from saleor.product import models++def get_attributes_dict_from_list(attributes):+ """+ :param attributes: list+ :return: dict+ Takes list on form [{"name": "attr_name", "value": "attr_value"}, {...}]+ and converts into dictionary {attr_pk: value_pk}+ """++ attr_ids = {}+ attr_name_id = dict(+ models.ProductAttribute.objects.values_list('name', 'id'))+ value_name_id = dict(+ models.AttributeChoiceValue.objects.values_list('name', 'id'))+ for attribute in attributes:+ attr_name = attribute.get('name')+ if attr_name not in attr_name_id:+ raise ValueError(+ 'Unknown attribute name: %r' % (attr_name,))+ attr_value = attribute.get('value')+ attr_ids[attr_name_id.get(+ attr_name)] = value_name_id.get(attr_value)+ return attr_ids