<commit_before>import graphene
from graphene.types import InputObjectType

from ....graphql.product.types import Category, ProductType
from ....graphql.utils import get_node
from ....product import models
from ...category.forms import CategoryForm
from ..mutations import (
    BaseMutation, ModelDeleteMutation, ModelFormMutation,
    ModelFormUpdateMutation, StaffMemberRequiredMutation, convert_form_errors)
from ..utils import get_attributes_dict_from_list
from .forms import ProductForm


class CategoryCreateMutation(StaffMemberRequiredMutation, ModelFormMutation):
    class Arguments:
        parent_id = graphene.ID()

    class Meta:
        form_class = CategoryForm

    @classmethod
    def get_form_kwargs(cls, root, info, **input):
        parent_id = input.pop('parent_id', None)
        kwargs = super().get_form_kwargs(root, info, **input)
        if parent_id:
            parent = get_node(info, parent_id, only_type=Category)
        else:
            parent = None
        kwargs['parent_pk'] = parent.pk if parent else None
        return kwargs


class CategoryUpdateMutation(
        StaffMemberRequiredMutation, ModelFormUpdateMutation):
    class Meta:
        form_class = CategoryForm

    @classmethod
    def get_form_kwargs(cls, root, info, **input):
        kwargs = super().get_form_kwargs(root, info, **input)
        kwargs['parent_pk'] = kwargs['instance'].parent_id
        return kwargs


class CategoryDelete(StaffMemberRequiredMutation, ModelDeleteMutation):
    class Meta:
        model = models.Category


class AttributeValueInput(InputObjectType):
    slug = graphene.String(required=True)
    value = graphene.String(required=True)


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)
        form = cls._meta.form_class(**form_kwargs)
        if form.is_valid():
            if attributes:
                attr_slug_id = dict(
                    form.instance.product_type.product_attributes.values_list(
                        'slug','id'))
                form.instance.attributes = get_attributes_dict_from_list(
                    attributes=attributes, attr_slug_id=attr_slug_id)<commit_msg>Fix whitespace<commit_after>import graphene
from graphene.types import InputObjectType

from ....graphql.product.types import Category, ProductType
from ....graphql.utils import get_node
from ....product import models
from ...category.forms import CategoryForm
from ..mutations import (
    BaseMutation, ModelDeleteMutation, ModelFormMutation,
    ModelFormUpdateMutation, StaffMemberRequiredMutation, convert_form_errors)
from ..utils import get_attributes_dict_from_list
from .forms import ProductForm


class CategoryCreateMutation(StaffMemberRequiredMutation, ModelFormMutation):
    class Arguments:
        parent_id = graphene.ID()

    class Meta:
        form_class = CategoryForm

    @classmethod
    def get_form_kwargs(cls, root, info, **input):
        parent_id = input.pop('parent_id', None)
        kwargs = super().get_form_kwargs(root, info, **input)
        if parent_id:
            parent = get_node(info, parent_id, only_type=Category)
        else:
            parent = None
        kwargs['parent_pk'] = parent.pk if parent else None
        return kwargs


class CategoryUpdateMutation(
        StaffMemberRequiredMutation, ModelFormUpdateMutation):
    class Meta:
        form_class = CategoryForm

    @classmethod
    def get_form_kwargs(cls, root, info, **input):
        kwargs = super().get_form_kwargs(root, info, **input)
        kwargs['parent_pk'] = kwargs['instance'].parent_id
        return kwargs


class CategoryDelete(StaffMemberRequiredMutation, ModelDeleteMutation):
    class Meta:
        model = models.Category


class AttributeValueInput(InputObjectType):
    slug = graphene.String(required=True)
    value = graphene.String(required=True)


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)
        form = cls._meta.form_class(**form_kwargs)
        if form.is_valid():
            if attributes:
                attr_slug_id = dict(
                    form.instance.product_type.product_attributes.values_list(
                        'slug', 'id'))
                form.instance.attributes = get_attributes_dict_from_list(
                    attributes=attributes, attr_slug_id=attr_slug_id)
{'fraction_non_alphanumeric': 0.09013242204186245, 'fraction_numerical': 0.0, 'mean_word_length': 3.2, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3739611', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1270, 'n_words': 293}