Skip to content

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

Add flag to attribute determining whether it is a variant attribute only

NyanKiyoshidb471c3093e7
diff --git a/saleor/graphql/product/descriptions.py b/saleor/graphql/product/descriptions.py
index cafb077aca..4dd4c1c61c 100644
--- a/saleor/graphql/product/descriptions.py
+++ b/saleor/graphql/product/descriptions.py
@@ -3,6 +3,7 @@ class AttributeDescriptions:
NAME = "Name of an attribute displayed in the interface."
SLUG = "Internal representation of an attribute name."
VALUES = "List of attribute's values."
+ IS_VARIANT_ONLY = "Whether the attribute is for variants only"
class AttributeValueDescriptions:
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 59c591d2a8..46610e2dd4 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -32,6 +32,9 @@ class AttributeCreateInput(graphene.InputObjectType):
values = graphene.List(
AttributeValueCreateInput, description=AttributeDescriptions.VALUES
)
+ is_variant_only = graphene.Boolean(
+ required=False, description=AttributeDescriptions.IS_VARIANT_ONLY
+ )
class AttributeUpdateInput(graphene.InputObjectType):
@@ -47,13 +50,14 @@ class AttributeUpdateInput(graphene.InputObjectType):
name="addValues",
description="New values to be created for this attribute.",
)
+ is_variant_only = graphene.Boolean(
+ required=False, description=AttributeDescriptions.IS_VARIANT_ONLY
+ )
class AttributeAssignInput(graphene.InputObjectType):
- attribute_id = graphene.ID(
- required=True, description="The ID of the attribute to assign"
- )
- attribute_type = AttributeTypeEnum(
+ id = graphene.ID(required=True, description="The ID of the attribute to assign")
+ type = AttributeTypeEnum(
required=True, description="The attribute type to be assigned as."
)
@@ -224,7 +228,7 @@ class AttributeUpdate(AttributeMixin, ModelMutation):
return AttributeUpdate(attribute=instance)
-class BaseAttributeAssignmentMutation(BaseMutation):
+class AttributeAssign(BaseMutation):
product_type = graphene.Field(ProductType, description="The updated product type.")
class Arguments:
@@ -239,7 +243,7 @@ class BaseAttributeAssignmentMutation(BaseMutation):
)
class Meta:
- abstract = True
+ description = "Assign attributes to a given product type."
@classmethod
def check_permissions(cls, user):
@@ -253,24 +257,19 @@ class BaseAttributeAssignmentMutation(BaseMutation):
for operation in operations:
pk = from_global_id_strict_type(
- info, operation.attribute_id, only_type=Attribute, field="operations"
+ info, operation.id, only_type=Attribute, field="operations"
)
- if operation.attribute_type == AttributeTypeEnum.PRODUCT:
+ if operation.type == AttributeTypeEnum.PRODUCT:
product_attrs_pks.append(pk)
else:
variant_attrs_pks.append(pk)
return product_attrs_pks, variant_attrs_pks
-
-class AttributeAssign(BaseAttributeAssignmentMutation):
- class Meta:
- description = "Assign attributes to a given product type."
-
@classmethod
- def clean_operations(cls, product_type, product_attrs_pks, variant_attrs_pks):
- """Ensures the requested attributes are not already assigned
- to that product type."""
+ def check_operations_not_assigned_already(
+ cls, product_type, product_attrs_pks, variant_attrs_pks
+ ):
qs = (
models.Attribute.objects.get_assigned_attributes(product_type.pk)
.values_list("name", "slug")
@@ -305,6 +304,26 @@ class AttributeAssign(BaseAttributeAssignmentMutation):
}
)
+ @classmethod
+ def check_product_operations_are_assignable(cls, product_attrs_pks):
+ contains_restricted_attributes = models.Attribute.objects.filter(
+ pk__in=product_attrs_pks, is_variant_only=True
+ ).exists()
+
+ if contains_restricted_attributes:
+ raise ValidationError(
+ {"operations": ("Cannot assign variant only attributes.")}
+ )
+
+ @classmethod
+ def clean_operations(cls, product_type, product_attrs_pks, variant_attrs_pks):
+ """Ensures the requested attributes are not already assigned
+ to that product type."""
+ cls.check_product_operations_are_assignable(product_attrs_pks)
+ cls.check_operations_not_assigned_already(
+ product_type, product_attrs_pks, variant_attrs_pks
+ )
+
@classmethod
def save_field_values(cls, product_type, field, pks):
"""Add in bulk the PKs to assign to a given product type."""
@@ -337,10 +356,27 @@ class AttributeAssign(BaseAttributeAssignmentMutation):
return cls(product_type=product_type)
-class AttributeUnassign(BaseAttributeAssignmentMutation):
+class AttributeUnassign(BaseMutation):
+ product_type = graphene.Field(ProductType, description="The updated product type.")
+
+ class Arguments:
+ product_type_id = graphene.ID(
+ required=True,
+ description="ID of the product type to assign the attributes into.",
+ )
+ attribute_ids = graphene.List(
+ graphene.ID,
+ required=True,
+ description="The IDs of the attributes to assign",
+ )
+
class Meta:
description = "Un-assign attributes from a given product type."
+ @classmethod
+ def check_permissions(cls, user):
+ return user.has_perm("product.manage_products")
+
@classmethod
def save_field_values(cls, product_type, field, pks):
"""Add in bulk the PKs to assign to a given product type."""
@@ -348,7 +384,7 @@ class AttributeUnassign(BaseAttributeAssignmentMutation):
@classmethod
def perform_mutation(
- cls, _root, info, product_type_id: str, operations: List[AttributeAssignInput]
+ cls, _root, info, product_type_id: str, attribute_ids: List[str]
):
# Retrieve the requested product type
product_type = graphene.Node.get_node_from_global_id(
@@ -356,11 +392,16 @@ class AttributeUnassign(BaseAttributeAssignmentMutation):
) # type: models.ProductType
# Resolve all the passed IDs to ints
- product_attrs_pks, variant_attrs_pks = cls.get_operations(info, operations)
+ attribute_pks = [
+ from_global_id_strict_type(
+ info, attribute_id, only_type=Attribute, field="attribute_id"
+ )
+ for attribute_id in attribute_ids
+ ]
# Commit
- cls.save_field_values(product_type, "product_attributes", product_attrs_pks)
- cls.save_field_values(product_type, "variant_attributes", variant_attrs_pks)
+ cls.save_field_values(product_type, "product_attributes", attribute_pks)
+ cls.save_field_values(product_type, "variant_attributes", attribute_pks)
return cls(product_type=product_type)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 0508c74a16..0db55953e8 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -159,8 +159,8 @@ type AttributeAssign {
}
input AttributeAssignInput {
- attributeId: ID!
- attributeType: AttributeTypeEnum!
+ id: ID!
+ type: AttributeTypeEnum!
}
type AttributeBulkDelete {
@@ -198,6 +198,7 @@ input AttributeCreateInput {
name: String!
slug: String
values: [AttributeValueCreateInput]
+ isVariantOnly: Boolean
}
type AttributeDelete {
@@ -243,6 +244,7 @@ input AttributeUpdateInput {
slug: String
removeValues: [ID]
addValues: [AttributeValueCreateInput]
+ isVariantOnly: Boolean
}
type AttributeUpdateMeta {
@@ -1709,7 +1711,7 @@ type Mutations {
attributeDelete(id: ID!): AttributeDelete
attributeBulkDelete(ids: [ID]!): AttributeBulkDelete
attributeAssign(operations: [AttributeAssignInput]!, productTypeId: ID!): AttributeAssign
- attributeUnassign(operations: [AttributeAssignInput]!, productTypeId: ID!): AttributeUnassign
+ attributeUnassign(attributeIds: [ID]!, productTypeId: ID!): AttributeUnassign
attributeUpdate(id: ID!, input: AttributeUpdateInput!): AttributeUpdate
attributeTranslate(id: ID!, input: NameTranslationInput!, languageCode: LanguageCodeEnum!): AttributeTranslate
attributeUpdateMetadata(id: ID!, input: MetaInput!): AttributeUpdateMeta
diff --git a/saleor/product/migrations/0096_migrate_attrs_to_m2m.py b/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
index f779e6eb69..fba518eedc 100644
--- a/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
+++ b/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
@@ -24,6 +24,11 @@ class Migration(migrations.Migration):
dependencies = [("product", "0095_auto_20190618_0842")]
operations = [
+ migrations.AddField(
+ model_name="attribute",
+ name="is_variant_only",
+ field=models.BooleanField(default=False),
+ ),
# Rename the foreign keys to backup them before overriding and processing them
migrations.RenameField(
model_name="attribute", old_name="product_type", new_name="product_type_old"
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 84cf586a9f..1361984f12 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -510,6 +510,8 @@ class Attribute(ModelWithMetadata):
through_fields=["attribute", "product_type"],
)
+ is_variant_only = models.BooleanField(default=False)
+
objects = AttributeQuerySet.as_manager()
translated = TranslationProxy()
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index ec73366d02..72b3635946 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -720,18 +720,12 @@ def test_assign_attributes_to_product_type(
for attr in product_attributes:
operations.append(
- {
- "attributeType": "PRODUCT",
- "attributeId": graphene.Node.to_global_id("Attribute", attr.pk),
- }
+ {"type": "PRODUCT", "id": graphene.Node.to_global_id("Attribute", attr.pk)}
)
for attr in variant_attributes:
operations.append(
- {
- "attributeType": "VARIANT",
- "attributeId": graphene.Node.to_global_id("Attribute", attr.pk),
- }
+ {"type": "VARIANT", "id": graphene.Node.to_global_id("Attribute", attr.pk)}
)
content = get_graphql_content(
@@ -774,10 +768,7 @@ def test_assign_variant_attribute_to_product_type_with_disabled_variants(
query = ASSIGN_ATTR_QUERY
operations = [
- {
- "attributeType": "VARIANT",
- "attributeId": graphene.Node.to_global_id("Attribute", attribute.pk),
- }
+ {"type": "VARIANT", "id": graphene.Node.to_global_id("Attribute", attribute.pk)}
]
variables = {"productTypeId": product_type_global_id, "operations": operations}
@@ -866,8 +857,8 @@ def test_assign_attribute_to_product_type_having_already_that_attribute(
query = ASSIGN_ATTR_QUERY
operations = [
{
- "attributeType": gql_attribute_type.value,
- "attributeId": graphene.Node.to_global_id("Attribute", attribute.pk),
+ "type": gql_attribute_type.value,
+ "id": graphene.Node.to_global_id("Attribute", attribute.pk),
}
]
variables = {"productTypeId": product_type_global_id, "operations": operations}
@@ -885,9 +876,9 @@ def test_assign_attribute_to_product_type_having_already_that_attribute(
UNASSIGN_ATTR_QUERY = """
mutation unAssignAttribute(
- $productTypeId: ID!, $operations: [AttributeAssignInput]!
+ $productTypeId: ID!, $attributeIds: [ID]!
) {
- attributeUnassign(productTypeId: $productTypeId, operations: $operations) {
+ attributeUnassign(productTypeId: $productTypeId, attributeIds: $attributeIds) {
errors {
field
message
@@ -921,15 +912,12 @@ def test_unassign_attributes_from_product_type(
)
query = UNASSIGN_ATTR_QUERY
- operations = [
- {
- "attributeType": "PRODUCT",
- "attributeId": graphene.Node.to_global_id(
- "Attribute", product_attributes[0].pk
- ),
- }
- ]
- variables = {"productTypeId": product_type_global_id, "operations": operations}
+ variables = {
+ "productTypeId": product_type_global_id,
+ "attributeIds": [
+ graphene.Node.to_global_id("Attribute", product_attributes[0].pk)
+ ],
+ }
content = get_graphql_content(
staff_api_client.post_graphql(
@@ -960,15 +948,12 @@ def test_unassign_attributes_not_in_product_type(
product_type_global_id = graphene.Node.to_global_id("ProductType", product_type.pk)
query = UNASSIGN_ATTR_QUERY
- operations = [
- {
- "attributeType": "VARIANT",
- "attributeId": graphene.Node.to_global_id(
- "Attribute", color_attribute_without_values.pk
- ),
- }
- ]
- variables = {"productTypeId": product_type_global_id, "operations": operations}
+ variables = {
+ "productTypeId": product_type_global_id,
+ "attributeIds": [
+ graphene.Node.to_global_id("Attribute", color_attribute_without_values.pk)
+ ],
+ }
content = get_graphql_content(staff_api_client.post_graphql(query, variables))[
"data"