Skip to content

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

Move mutate logic to mixin

Adam Janik1e3c201b5275
diff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.py
index c81d768dfe..0091a8dfcb 100644
--- a/saleor/dashboard/graphql/product/mutations.py
+++ b/saleor/dashboard/graphql/product/mutations.py
@@ -6,8 +6,8 @@ from ....graphql.utils import get_node
from ....product import models
from ...category.forms import CategoryForm
from ..mutations import (
- ModelDeleteMutation, ModelFormMutation, ModelFormUpdateMutation,
- StaffMemberRequiredMutation)
+ BaseMutation, ModelDeleteMutation, ModelFormMutation,
+ ModelFormUpdateMutation, StaffMemberRequiredMutation)
from ..utils import get_attributes_dict_from_list
from .forms import ProductForm
@@ -53,7 +53,26 @@ class AttributeValueInput(InputObjectType):
value = graphene.String(required=True)
-class ProductCreateMutation(StaffMemberRequiredMutation, ModelFormMutation):
+class BaseProductMutateMixin(BaseMutation):
+ @classmethod
+ def mutate(cls, root, info, *args, **kwargs):
+ form_kwargs = cls.get_form_kwargs(root, info, **kwargs)
+ attributes = form_kwargs.get('data').pop('attributes', None)
+ if attributes:
+ form = cls._meta.form_class(**form_kwargs)
+ if form.is_valid():
+ pt_ids = form.\
+ instance.product_type.product_attributes.values_list('id')
+ form.instance.attributes = get_attributes_dict_from_list(
+ attributes=attributes, product_type_ids=pt_ids)
+ instance = form.save()
+ kwargs = {cls._meta.return_field_name: instance}
+ return cls(errors=[], **kwargs)
+ return super().mutate(root, info, *args, **kwargs)
+
+
+class ProductCreateMutation(BaseProductMutateMixin,
+ StaffMemberRequiredMutation, ModelFormMutation):
class Arguments:
product_type_id = graphene.ID()
category_id = graphene.ID()
@@ -76,20 +95,6 @@ class ProductCreateMutation(StaffMemberRequiredMutation, ModelFormMutation):
kwargs['data']['category'] = category.id
return kwargs
- @classmethod
- def mutate(cls, root, info, *args, **kwargs):
- form_kwargs = cls.get_form_kwargs(root, info, **kwargs)
- attributes = form_kwargs.get('data').pop('attributes', None)
- if attributes:
- form = cls._meta.form_class(**form_kwargs)
- if form.is_valid():
- form.instance.attributes = get_attributes_dict_from_list(
- attributes)
- instance = form.save()
- kwargs = {cls._meta.return_field_name: instance}
- return cls(errors=[], **kwargs)
- return super().mutate(root, info, *args, **kwargs)
-
class ProductDeleteMutation(StaffMemberRequiredMutation, ModelDeleteMutation):
@@ -97,7 +102,8 @@ class ProductDeleteMutation(StaffMemberRequiredMutation, ModelDeleteMutation):
model = models.Product
-class ProductUpdateMutation(StaffMemberRequiredMutation,
+class ProductUpdateMutation(BaseProductMutateMixin,
+ StaffMemberRequiredMutation,
ModelFormUpdateMutation):
class Arguments:
attributes = graphene.Argument(graphene.List(AttributeValueInput))
@@ -122,17 +128,3 @@ class ProductUpdateMutation(StaffMemberRequiredMutation,
else:
kwargs['data']['category'] = instance.category.id
return kwargs
-
- @classmethod
- def mutate(cls, root, info, *args, **kwargs):
- form_kwargs = cls.get_form_kwargs(root, info, **kwargs)
- attributes = form_kwargs.get('data').pop('attributes', None)
- if attributes:
- form = cls._meta.form_class(**form_kwargs)
- if form.is_valid():
- form.instance.attributes = get_attributes_dict_from_list(
- attributes)
- instance = form.save()
- kwargs = {cls._meta.return_field_name: instance}
- return cls(errors=[], **kwargs)
- return super().mutate(root, info, *args, **kwargs)
diff --git a/saleor/dashboard/graphql/utils.py b/saleor/dashboard/graphql/utils.py
index cc3386ea76..2e588a51e9 100644
--- a/saleor/dashboard/graphql/utils.py
+++ b/saleor/dashboard/graphql/utils.py
@@ -4,7 +4,7 @@ from django.utils.text import slugify
from ...product.models import AttributeChoiceValue, ProductAttribute
-def get_attributes_dict_from_list(attributes):
+def get_attributes_dict_from_list(attributes, product_type_ids):
"""
:param attributes: list
:return: dict
@@ -13,7 +13,8 @@ def get_attributes_dict_from_list(attributes):
"""
attr_ids = {}
attr_slug_id = dict(
- ProductAttribute.objects.values_list('slug', 'id'))
+ ProductAttribute.objects.filter(
+ id__in=product_type_ids).values_list('slug', 'id'))
value_slug_id = dict(
AttributeChoiceValue.objects.values_list('name', 'id'))
for attribute in attributes:
@@ -24,7 +25,7 @@ def get_attributes_dict_from_list(attributes):
value = attribute.get('value')
if value:
if not value_slug_id.get(value):
- attr = ProductAttribute.objects.get(slug='size')
+ attr = ProductAttribute.objects.get(slug=attr_slug)
value = AttributeChoiceValue(
attribute_id=attr.pk, name=value, slug=slugify(value))
value.save()