Skip to content

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

Add swatch attribute input type (#6796)

Iga Karbowiaka4f8c0d7cd48
diff --git a/saleor/attribute/__init__.py b/saleor/attribute/__init__.py
index e2e17b6152..729b0d38bf 100644
--- a/saleor/attribute/__init__.py
+++ b/saleor/attribute/__init__.py
@@ -10,6 +10,7 @@ class AttributeInputType:
REFERENCE = "reference"
NUMERIC = "numeric"
RICH_TEXT = "rich-text"
+ SWATCH = "swatch"
CHOICES = [
(DROPDOWN, "Dropdown"),
@@ -18,27 +19,29 @@ class AttributeInputType:
(REFERENCE, "Reference"),
(NUMERIC, "Numeric"),
(RICH_TEXT, "Rich Text"),
+ (SWATCH, "Swatch"),
]
- # list of the input types that can be used in variant selection
- ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN]
+ ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN, SWATCH]
-# list of input types that are allowed for given attribute property
ATTRIBUTE_PROPERTIES_CONFIGURATION = {
"filterable_in_storefront": [
AttributeInputType.DROPDOWN,
AttributeInputType.MULTISELECT,
AttributeInputType.NUMERIC,
+ AttributeInputType.SWATCH,
],
"filterable_in_dashboard": [
AttributeInputType.DROPDOWN,
AttributeInputType.MULTISELECT,
AttributeInputType.NUMERIC,
+ AttributeInputType.SWATCH,
],
"available_in_grid": [
AttributeInputType.DROPDOWN,
AttributeInputType.MULTISELECT,
AttributeInputType.NUMERIC,
+ AttributeInputType.SWATCH,
],
"storefront_search_position": [
AttributeInputType.DROPDOWN,
diff --git a/saleor/attribute/migrations/0011_auto_20210429_1013.py b/saleor/attribute/migrations/0011_auto_20210429_1013.py
new file mode 100644
index 0000000000..4c064415b2
--- /dev/null
+++ b/saleor/attribute/migrations/0011_auto_20210429_1013.py
@@ -0,0 +1,35 @@
+# Generated by Django 3.1.8 on 2021-04-29 10:13
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("attribute", "0010_auto_20210412_0736"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="attribute",
+ name="input_type",
+ field=models.CharField(
+ choices=[
+ ("dropdown", "Dropdown"),
+ ("multiselect", "Multi Select"),
+ ("file", "File"),
+ ("reference", "Reference"),
+ ("numeric", "Numeric"),
+ ("rich-text", "Rich Text"),
+ ("swatch", "Swatch"),
+ ],
+ default="dropdown",
+ max_length=50,
+ ),
+ ),
+ migrations.AlterField(
+ model_name="attributevalue",
+ name="value",
+ field=models.CharField(blank=True, default="", max_length=9),
+ ),
+ ]
diff --git a/saleor/attribute/models/base.py b/saleor/attribute/models/base.py
index 0d82c43ab8..4ef3149c45 100644
--- a/saleor/attribute/models/base.py
+++ b/saleor/attribute/models/base.py
@@ -191,7 +191,8 @@ class AttributeTranslation(models.Model):
class AttributeValue(SortableModel):
name = models.CharField(max_length=250)
- value = models.CharField(max_length=100, blank=True, default="")
+ # keeps hex code color value in #RRGGBBAA format
+ value = models.CharField(max_length=9, blank=True, default="")
slug = models.SlugField(max_length=255, allow_unicode=True)
file_url = models.URLField(null=True, blank=True)
content_type = models.CharField(max_length=50, null=True, blank=True)
diff --git a/saleor/graphql/attribute/descriptions.py b/saleor/graphql/attribute/descriptions.py
index 9070e9c132..2ea433e47c 100644
--- a/saleor/graphql/attribute/descriptions.py
+++ b/saleor/graphql/attribute/descriptions.py
@@ -27,6 +27,10 @@ class AttributeValueDescriptions:
SLUG = "Internal representation of a value (unique per attribute)."
TYPE = "Type of value (used only when `value` field is set)."
FILE = "Represents file URL and content type (if attribute value is a file)."
+ VALUE = (
+ "Represent value of the attribute value "
+ "(e.g. color values for swatch attributes)."
+ )
VALUES_RANGE = "The range that the returned values should be in."
VALUE = "Represents the value of the attribute value."
RICH_TEXT = "Represents the text (JSON) of the attribute value."
diff --git a/saleor/graphql/attribute/mutations.py b/saleor/graphql/attribute/mutations.py
index 767094f2ce..bdcb90695f 100644
--- a/saleor/graphql/attribute/mutations.py
+++ b/saleor/graphql/attribute/mutations.py
@@ -185,10 +185,22 @@ class BaseReorderAttributeValuesMutation(BaseMutation):
return operations
-class AttributeValueCreateInput(graphene.InputObjectType):
- name = graphene.String(required=True, description=AttributeValueDescriptions.NAME)
+class AttributeValueInput(graphene.InputObjectType):
value = graphene.String(description=AttributeValueDescriptions.VALUE)
rich_text = graphene.JSONString(description=AttributeValueDescriptions.RICH_TEXT)
+ file_url = graphene.String(
+ required=False,
+ description="URL of the file attribute. Every time, a new value is created.",
+ )
+ content_type = graphene.String(required=False, description="File content type.")
+
+
+class AttributeValueCreateInput(AttributeValueInput):
+ name = graphene.String(required=True, description=AttributeValueDescriptions.NAME)
+
+
+class AttributeValueUpdateInput(AttributeValueInput):
+ name = graphene.String(required=False, description=AttributeValueDescriptions.NAME)
class AttributeCreateInput(graphene.InputObjectType):
@@ -232,7 +244,7 @@ class AttributeUpdateInput(graphene.InputObjectType):
description="IDs of values to be removed from this attribute.",
)
add_values = graphene.List(
- AttributeValueCreateInput,
+ AttributeValueUpdateInput,
name="addValues",
description="New values to be created for this attribute.",
)
@@ -258,38 +270,8 @@ class AttributeUpdateInput(graphene.InputObjectType):
class AttributeMixin:
- @classmethod
- def check_values_are_unique(cls, values_input, attribute):
- # Check values uniqueness in case of creating new attribute.
- existing_values = attribute.values.values_list("slug", flat=True)
- for value_data in values_input:
- slug = slugify(value_data["name"], allow_unicode=True)
- if slug in existing_values:
- msg = (
- "Value %s already exists within this attribute."
- % value_data["name"]
- )
- raise ValidationError(
- {
- cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
- msg, code=AttributeErrorCode.ALREADY_EXISTS
- )
- }
- )
-
- new_slugs = [
- slugify(value_data["name"], allow_unicode=True)
- for value_data in values_input
- ]
- if len(set(new_slugs)) != len(new_slugs):
- raise ValidationError(
- {
- cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
- "Provided values are not unique.",
- code=AttributeErrorCode.UNIQUE,
- )
- }
- )
+ # must be redefined by inheriting classes
+ ATTRIBUTE_VALUES_FIELD = None
@classmethod
def clean_values(cls, cleaned_input, attribute):
@@ -321,23 +303,56 @@ class AttributeMixin:
)
is_numeric_attr = attribute_input_type == AttributeInputType.NUMERIC
+ is_swatch_attr = attribute_input_type == AttributeInputType.SWATCH
for value_data in values_input:
- value = value_data["name"]
- if is_numeric_attr:
- cls.validate_numeric_value(value)
- slug_value = value if not is_numeric_attr else value.replace(".", "_")
- value_data["slug"] = slugify(slug_value, allow_unicode=True)
+ cls.validate_value(attribute, value_data, is_numeric_attr, is_swatch_attr)
- attribute_value = models.AttributeValue(**value_data, attribute=attribute)
- try:
- attribute_value.full_clean()
- except ValidationError as validation_errors:
- for field, err in validation_errors.error_dict.items():
- if field == "attribute":
- continue
- raise ValidationError({cls.ATTRIBUTE_VALUES_FIELD: err})
cls.check_values_are_unique(values_input, attribute)
+ @classmethod
+ def validate_value(
+ cls,
+ attribute: models.Attribute,
+ value_data: dict,
+ is_numeric_attr: bool,
+ is_swatch_attr: bool,
+ ):
+ value = value_data["name"]
+ cls.clean_value_input_data(value_data, is_swatch_attr)
+
+ if is_numeric_attr:
+ cls.validate_numeric_value(value)
+ elif is_swatch_attr:
+ cls.validate_swatch_attr_value(value_data)
+
+ slug_value = value if not is_numeric_attr else value.replace(".", "_")
+ value_data["slug"] = slugify(slug_value, allow_unicode=True)
+
+ attribute_value = models.AttributeValue(**value_data, attribute=attribute)
+ try:
+ attribute_value.full_clean()
+ except ValidationError as validation_errors:
+ for field, err in validation_errors.error_dict.items():
+ if field == "attribute":
+ continue
+ raise ValidationError({cls.ATTRIBUTE_VALUES_FIELD: err})
+
+ @classmethod
+ def clean_value_input_data(cls, value_data: dict, is_swatch_attr: bool):
+ swatch_fields = ["file_url", "content_type", "value"]
+ if not is_swatch_attr and any(
+ [value_data.get(field) for field in swatch_fields]
+ ):
+ raise ValidationError(
+ {
+ cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
+ "Cannot define value, file and contentType fields "
+ "for not swatch attribute.",
+ code=AttributeErrorCode.INVALID.value,
+ )
+ }
+ )
+
@classmethod
def validate_numeric_value(cls, value):
try:
@@ -347,7 +362,52 @@ class AttributeMixin:
{
cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
"Value of numeric attribute must be numeric.",
- code=AttributeErrorCode.INVALID,
+ code=AttributeErrorCode.INVALID.value,
+ )
+ }
+ )
+
+ @classmethod
+ def validate_swatch_attr_value(cls, value_data: dict):
+ if value_data.get("value") and value_data.get("file_url"):
+ raise ValidationError(
+ {
+ cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
+ "Cannot specify both value and file for swatch attribute.",
+ code=AttributeErrorCode.INVALID.value,
+ )
+ }
+ )
+
+ @classmethod
+ def check_values_are_unique(cls, values_input: dict, attribute: models.Attribute):
+ # Check values uniqueness in case of creating new attribute.
+ existing_values = attribute.values.values_list("slug", flat=True)
+ for value_data in values_input:
+ slug = slugify(value_data["name"], allow_unicode=True)
+ if slug in existing_values:
+ msg = (
+ "Value %s already exists within this attribute."
+ % value_data["name"]
+ )
+ raise ValidationError(
+ {
+ cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
+ msg, code=AttributeErrorCode.ALREADY_EXISTS.value
+ )
+ }
+ )
+
+ new_slugs = [
+ slugify(value_data["name"], allow_unicode=True)
+ for value_data in values_input
+ ]
+ if len(set(new_slugs)) != len(new_slugs):
+ raise ValidationError(
+ {
+ cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
+ "Provided values are not unique.",
+ code=AttributeErrorCode.UNIQUE.value,
)
}
)
@@ -547,7 +607,9 @@ def validate_value_is_unique(attribute: models.Attribute, value: models.Attribut
)
-class AttributeValueCreate(ModelMutation):
+class AttributeValueCreate(AttributeMixin, ModelMutation):
+ ATTRIBUTE_VALUES_FIELD = "input"
+
attribute = graphene.Field(Attribute, description="The updated attribute.")
class Arguments:
@@ -570,7 +632,29 @@ class AttributeValueCreate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
- cleaned_input["slug"] = slugify(cleaned_input["name"], allow_unicode=True)
+ if "name" in cleaned_input:
+ cleaned_input["slug"] = slugify(cleaned_input["name"], allow_unicode=True)
+ input_type = instance.attribute.input_type
+
+ is_swatch_attr = input_type == AttributeInputType.SWATCH
+ only_swatch_fields = ["file_url", "content_type", "value"]
+ errors = {}
+ if not is_swatch_attr:
+ for field in only_swatch_fields:
+ if cleaned_input.get(field):
+ errors[field] = ValidationError(
+ f"The field {field} can be defined only for swatch attributes.",
+ code=AttributeErrorCode.INVALID.value,
+ )
+ else:
+ try:
+ cls.validate_swatch_attr_value(cleaned_input)
+ except ValidationError as error:
+ errors["value"] = error.error_dict[cls.ATTRIBUTE_VALUES_FIELD]
+ errors["fileUrl"] = error.error_dict[cls.ATTRIBUTE_VALUES_FIELD]
+ if errors:
+ raise ValidationError(errors)
+
return cleaned_input
@classmethod
@@ -591,14 +675,14 @@ class AttributeValueCreate(ModelMutation):
return AttributeValueCreate(attribute=attribute, attributeValue=instance)
-class AttributeValueUpdate(ModelMutation):
+class AttributeValueUpdate(AttributeValueCreate):
attribute = graphene.Field(Attribute, description="The updated attribute.")
class Arguments:
id = graphene.ID(
required=True, description="ID of an AttributeValue to update."
)
- input = AttributeValueCreateInput(
+ input = AttributeValueUpdateInput(
required=True, description="Fields required to update an AttributeValue."
)
@@ -612,14 +696,16 @@ class AttributeValueUpdate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
- if "name" in cleaned_input:
- cleaned_input["slug"] = slugify(cleaned_input["name"], allow_unicode=True)
+ if cleaned_input.get("value"):
+ cleaned_input["file_url"] = ""
+ cleaned_input["content_type"] = ""
+ elif cleaned_input.get("file_url"):
+ cleaned_input["value"] = ""
return cleaned_input
@classmethod
- def clean_instance(cls, info, instance):
- validate_value_is_unique(instance.attribute, instance)
- super().clean_instance(info, instance)
+ def perform_mutation(cls, _root, info, **data):
+ return super(AttributeValueCreate, cls).perform_mutation(_root, info, **data)
@classmethod
def success_response(cls, instance):
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_create.py b/saleor/graphql/attribute/tests/mutations/test_attribute_create.py
index 65aa0bb167..26643a4e40 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_create.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_create.py
@@ -31,6 +31,10 @@ CREATE_ATTRIBUTE_MUTATION = """
name
slug
value
+ file {
+ url
+ contentType
+ }
}
productTypes(first: 10) {
edges {
@@ -55,11 +59,10 @@ def test_create_attribute_and_attribute_values(
attribute_name = "Example name"
name = "Value name"
- value = "Value"
variables = {
"input": {
"name": attribute_name,
- "values": [{"name": name, "value": value}],
+ "values": [{"name": name}],
"type": AttributeTypeEnum.PRODUCT_TYPE.name,
}
}
@@ -93,7 +96,6 @@ def test_create_attribute_and_attribute_values(
assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
assert data["attribute"]["values"][0]["name"] == name
assert data["attribute"]["values"][0]["slug"] == slugify(name)
- assert data["attribute"]["values"][0]["value"] == value
def test_create_numeric_attribute_and_attribute_values(
@@ -200,6 +202,284 @@ def test_create_numeric_attribute_and_attribute_values_not_numeric_value_provide
assert errors[0]["code"] == AttributeErrorCode.INVALID.name
+def test_create_swatch_attribute_and_attribute_values_only_name_provided(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Pink"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ assert not content["data"]["attributeCreate"]["errors"]
+ data = content["data"]["attributeCreate"]
+
+ # Check if the attribute was correctly created
+ assert data["attribute"]["name"] == attribute_name
+ assert data["attribute"]["slug"] == slugify(
+ attribute_name
+ ), "The default slug should be the slugified name"
+ assert (
+ data["attribute"]["productTypes"]["edges"] == []
+ ), "The attribute should not have been assigned to a product type"
+
+ # Check if the attribute values were correctly created
+ assert len(data["attribute"]["values"]) == 1
+ assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
+ assert data["attribute"]["unit"] is None
+ assert data["attribute"]["inputType"] == AttributeInputTypeEnum.SWATCH.name
+ assert data["attribute"]["filterableInStorefront"] is True
+ assert data["attribute"]["filterableInDashboard"] is True
+ assert data["attribute"]["availableInGrid"] is True
+ assert data["attribute"]["storefrontSearchPosition"] == 0
+ assert data["attribute"]["values"][0]["name"] == name
+ assert data["attribute"]["values"][0]["slug"] == slugify(name)
+
+
+def test_create_swatch_attribute_and_attribute_values_with_file(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Logo"
+ file_url = "http://mirumee.com/test_media/test_logo.png"
+ content_type = "image/png"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [
+ {"name": name, "fileUrl": file_url, "contentType": content_type}
+ ],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ assert not content["data"]["attributeCreate"]["errors"]
+ data = content["data"]["attributeCreate"]
+
+ # Check if the attribute was correctly created
+ assert data["attribute"]["name"] == attribute_name
+ assert data["attribute"]["slug"] == slugify(
+ attribute_name
+ ), "The default slug should be the slugified name"
+ assert (
+ data["attribute"]["productTypes"]["edges"] == []
+ ), "The attribute should not have been assigned to a product type"
+
+ # Check if the attribute values were correctly created
+ assert len(data["attribute"]["values"]) == 1
+ assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
+ assert data["attribute"]["unit"] is None
+ assert data["attribute"]["inputType"] == AttributeInputTypeEnum.SWATCH.name
+ assert data["attribute"]["filterableInStorefront"] is True
+ assert data["attribute"]["filterableInDashboard"] is True
+ assert data["attribute"]["availableInGrid"] is True
+ assert data["attribute"]["storefrontSearchPosition"] == 0
+ assert data["attribute"]["values"][0]["name"] == name
+ assert data["attribute"]["values"][0]["slug"] == slugify(name)
+ assert data["attribute"]["values"][0]["file"] == {
+ "url": file_url,
+ "contentType": content_type,
+ }
+
+
+def test_create_swatch_attribute_and_attribute_values_with_value(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Pink"
+ value = "#ffc0cb"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name, "value": value}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ assert not content["data"]["attributeCreate"]["errors"]
+ data = content["data"]["attributeCreate"]
+
+ # Check if the attribute was correctly created
+ assert data["attribute"]["name"] == attribute_name
+ assert data["attribute"]["slug"] == slugify(
+ attribute_name
+ ), "The default slug should be the slugified name"
+ assert (
+ data["attribute"]["productTypes"]["edges"] == []
+ ), "The attribute should not have been assigned to a product type"
+
+ # Check if the attribute values were correctly created
+ assert len(data["attribute"]["values"]) == 1
+ assert data["attribute"]["type"] == AttributeTypeEnum.PRODUCT_TYPE.name
+ assert data["attribute"]["unit"] is None
+ assert data["attribute"]["inputType"] == AttributeInputTypeEnum.SWATCH.name
+ assert data["attribute"]["filterableInStorefront"] is True
+ assert data["attribute"]["filterableInDashboard"] is True
+ assert data["attribute"]["availableInGrid"] is True
+ assert data["attribute"]["storefrontSearchPosition"] == 0
+ assert data["attribute"]["values"][0]["name"] == name
+ assert data["attribute"]["values"][0]["slug"] == slugify(name)
+ assert data["attribute"]["values"][0]["file"] is None
+ assert data["attribute"]["values"][0]["value"] == value
+
+
+def test_create_swatch_attribute_and_attribute_values_file_and_value_provided(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Pink"
+ file_url = "http://mirumee.com/test_media/test_file.jpeg"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name, "value": "#A8A8A8", "fileUrl": file_url}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.SWATCH.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeCreate"]
+ errors = data["errors"]
+ assert not data["attribute"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "values"
+ assert errors[0]["code"] == AttributeErrorCode.INVALID.name
+
+
+ "field, value", [("fileUrl", "test.jpg"), ("value", "blue"), ("contentType", "jpg")]
+)
+def test_create_not_swatch_attribute_provide_not_valid_data(
+ field,
+ value,
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_ATTRIBUTE_MUTATION
+
+ attribute_name = "Example numeric attribute name"
+ name = "Test"
+ variables = {
+ "input": {
+ "name": attribute_name,
+ "values": [{"name": name, field: value}],
+ "type": AttributeTypeEnum.PRODUCT_TYPE.name,
+ "inputType": AttributeInputTypeEnum.DROPDOWN.name,
+ "filterableInStorefront": True,
+ "filterableInDashboard": True,
+ "availableInGrid": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[
+ permission_manage_product_types_and_attributes,
+ permission_manage_products,
+ ],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeCreate"]
+ errors = data["errors"]
+ assert not data["attribute"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "values"
+ assert errors[0]["code"] == AttributeErrorCode.INVALID.name
+
+
def test_create_attribute_with_file_input_type(
staff_api_client,
permission_manage_product_types_and_attributes,
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
index 261e9d324f..27745675d8 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
@@ -27,9 +27,13 @@ def test_validate_value_is_unique(color_attribute):
CREATE_ATTRIBUTE_VALUE_MUTATION = """
mutation createAttributeValue(
- $attributeId: ID!, $name: String!, $value: String) {
+ $attributeId: ID!, $name: String!,
+ $value: String, $fileUrl: String, $contentType: String
+ ) {
attributeValueCreate(
- attribute: $attributeId, input: {name: $name, value: $value}) {
+ attribute: $attributeId, input: {
+ name: $name, value: $value, fileUrl: $fileUrl, contentType: $contentType
+ }) {
errors {
field
message
@@ -39,6 +43,10 @@ CREATE_ATTRIBUTE_VALUE_MUTATION = """
values {
name
value
+ file {
+ url
+ contentType
+ }
}
}
attributeValue {
@@ -58,7 +66,33 @@ def test_create_attribute_value(
query = CREATE_ATTRIBUTE_VALUE_MUTATION
attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
name = "test name"
- value = "test value"
+ variables = {"name": name, "attributeId": attribute_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+ assert not data["errors"]
+
+ attr_data = data["attributeValue"]
+ assert attr_data["name"] == name
+ assert attr_data["slug"] == slugify(name)
+ assert name in [value["name"] for value in data["attribute"]["values"]]
+
+
+def test_create_swatch_attribute_value_with_value(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ # given
+ attribute = swatch_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ value = "#ffffff"
variables = {"name": name, "value": value, "attributeId": attribute_id}
# when
@@ -78,6 +112,110 @@ def test_create_attribute_value(
assert value in [value["value"] for value in data["attribute"]["values"]]
+def test_create_swatch_attribute_value_with_file(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ # given
+ attribute = swatch_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ file = "http://mirumee.com/test_media/test_file.jpeg"
+ content_type = "image/jpeg"
+ variables = {
+ "name": name,
+ "fileUrl": file,
+ "contentType": content_type,
+ "attributeId": attribute_id,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+ assert not data["errors"]
+
+ attr_data = data["attributeValue"]
+ assert attr_data["name"] == name
+ assert attr_data["slug"] == slugify(name)
+ assert name in [value["name"] for value in data["attribute"]["values"]]
+ assert {"url": file, "contentType": content_type} in [
+ value["file"] for value in data["attribute"]["values"]
+ ]
+
+
+def test_create_swatch_attribute_value_with_value_and_file(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ # given
+ attribute = swatch_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ value = "#ffffff"
+ file_url = "http://mirumee.com/test_media/test_file.jpeg"
+ variables = {
+ "name": name,
+ "value": value,
+ "fileUrl": file_url,
+ "attributeId": attribute_id,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+
+ assert not data["attributeValue"]
+ assert len(data["errors"]) == 2
+ assert {error["code"] for error in data["errors"]} == {
+ AttributeErrorCode.INVALID.name,
+ AttributeErrorCode.INVALID.name,
+ }
+ assert {error["field"] for error in data["errors"]} == {"fileUrl", "value"}
+
+
+ "field, value",
+ [
+ ("value", "#ffffff"),
+ ("fileUrl", "http://mirumee.com/test_media/test_file.jpeg"),
+ ("contentType", "jpeg"),
+ ],
+)
+def test_create_attribute_value_provide_not_allowed_input_data(
+ field, value, staff_api_client, color_attribute, permission_manage_products
+):
+ # given
+ attribute = color_attribute
+ query = CREATE_ATTRIBUTE_VALUE_MUTATION
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ name = "test name"
+ variables = {"name": name, field: value, "attributeId": attribute_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+
+ assert not data["attributeValue"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == AttributeErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == field
+
+
def test_create_attribute_value_not_unique_name(
staff_api_client, color_attribute, permission_manage_products
):
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py b/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
index 3fccb08644..f5c17dcfe7 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
@@ -1,4 +1,5 @@
import graphene
+import pytest
from django.utils.text import slugify
from .....attribute.error_codes import AttributeErrorCode
@@ -6,9 +7,9 @@ from ....tests.utils import get_graphql_content
UPDATE_ATTRIBUTE_VALUE_MUTATION = """
mutation AttributeValueUpdate(
- $id: ID!, $name: String!) {
+ $id: ID!, $input: AttributeValueUpdateInput!) {
attributeValueUpdate(
- id: $id, input: {name: $name}) {
+ id: $id, input: $input) {
errors {
field
message
@@ -17,6 +18,11 @@ mutation AttributeValueUpdate(
attributeValue {
name
slug
+ value
+ file {
+ url
+ contentType
+ }
}
attribute {
values {
@@ -38,7 +44,7 @@ def test_update_attribute_value(
value = pink_attribute_value
node_id = graphene.Node.to_global_id("AttributeValue", value.id)
name = "Crimson name"
- variables = {"name": name, "id": node_id}
+ variables = {"input": {"name": name}, "id": node_id}
# when
response = staff_api_client.post_graphql(
@@ -65,7 +71,7 @@ def test_update_attribute_value_name_not_unique(
name="Example Name", slug="example-name", value="#RED"
)
node_id = graphene.Node.to_global_id("AttributeValue", value.id)
- variables = {"name": pink_attribute_value.name, "id": node_id}
+ variables = {"input": {"name": pink_attribute_value.name}, "id": node_id}
# when
response = staff_api_client.post_graphql(
@@ -79,3 +85,128 @@ def test_update_attribute_value_name_not_unique(
assert data["errors"][0]["message"]
assert data["errors"][0]["field"] == "name"
assert data["errors"][0]["code"] == AttributeErrorCode.ALREADY_EXISTS.name
+
+
+def test_update_swatch_attribute_value(
+ staff_api_client,
+ swatch_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = swatch_attribute.values.filter(value__isnull=False).first()
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ name = "New name"
+ variables = {"input": {"name": name, "value": "", "fileUrl": ""}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert data["attributeValue"]["name"] == value.name
+ assert data["attributeValue"]["slug"] == value.slug
+ assert data["attributeValue"]["value"] == ""
+ assert data["attributeValue"]["file"] is None
+ assert value.name in [value["name"] for value in data["attribute"]["values"]]
+
+
[email protected]("additional_field", [{"value": ""}, {"value": None}, {}])
+def test_update_swatch_attribute_value_clear_value(
+ additional_field,
+ staff_api_client,
+ swatch_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = swatch_attribute.values.filter(value__isnull=False).first()
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ file_url = "http://mirumee.com/test_media/test_file.jpeg"
+ variables = {"input": {"fileUrl": file_url, **additional_field}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert data["attributeValue"]["name"] == value.name
+ assert data["attributeValue"]["slug"] == value.slug
+ assert data["attributeValue"]["value"] == ""
+ assert data["attributeValue"]["file"]["url"] == file_url
+ assert value.name in [value["name"] for value in data["attribute"]["values"]]
+
+
[email protected]("additional_field", [{"fileUrl": ""}, {"fileUrl": None}, {}])
+def test_update_swatch_attribute_value_clear_file_value(
+ additional_field,
+ staff_api_client,
+ swatch_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = swatch_attribute.values.filter(file_url__isnull=False).first()
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ input_value = "#ffffff"
+ variables = {"input": {"value": input_value, **additional_field}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert data["attributeValue"]["name"] == value.name
+ assert data["attributeValue"]["slug"] == value.slug
+ assert data["attributeValue"]["value"] == input_value
+ assert data["attributeValue"]["file"] is None
+ assert value.name in [value["name"] for value in data["attribute"]["values"]]
+
+
+ "field, input_value",
+ [
+ ("value", "#ffffff"),
+ ("fileUrl", "http://mirumee.com/test_media/test_file.jpeg"),
+ ("contentType", "jpeg"),
+ ],
+)
+def test_update_attribute_value_invalid_input_data(
+ field,
+ input_value,
+ staff_api_client,
+ pink_attribute_value,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION
+ value = pink_attribute_value
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ name = "Crimson name"
+ variables = {"input": {"name": name, field: input_value}, "id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueUpdate"]
+ value.refresh_from_db()
+ assert not data["attributeValue"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == AttributeErrorCode.INVALID.name
+ assert data["errors"][0]["field"] == field
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_query.py b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
index c43c45f434..500aff30a5 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_query.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
@@ -69,6 +69,7 @@ query($id: ID!) {
unit
values {
slug
+ value
inputType
file {
url
@@ -241,6 +242,7 @@ def test_get_single_product_attribute_with_file_value(
for value in file_attribute.values.all():
data = {
"slug": value.slug,
+ "value": "",
"inputType": value.input_type.upper(),
"file": {"url": value.file_url, "contentType": value.content_type},
}
@@ -342,6 +344,66 @@ def test_get_single_numeric_attribute_by_staff(
)
+def test_get_single_swatch_attribute_by_staff(
+ staff_api_client, swatch_attribute, permission_manage_products
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ attribute_gql_id = graphene.Node.to_global_id("Attribute", swatch_attribute.id)
+ query = QUERY_ATTRIBUTE
+ content = get_graphql_content(
+ staff_api_client.post_graphql(query, {"id": attribute_gql_id})
+ )
+
+ assert content["data"]["attribute"], "Should have found an attribute"
+ assert content["data"]["attribute"]["id"] == attribute_gql_id
+ assert content["data"]["attribute"]["slug"] == swatch_attribute.slug
+ assert (
+ content["data"]["attribute"]["inputType"] == swatch_attribute.input_type.upper()
+ )
+ assert content["data"]["attribute"]["unit"] is None
+ assert (
+ content["data"]["attribute"]["valueRequired"] == swatch_attribute.value_required
+ )
+ assert (
+ content["data"]["attribute"]["visibleInStorefront"]
+ == swatch_attribute.visible_in_storefront
+ )
+ assert (
+ content["data"]["attribute"]["filterableInStorefront"]
+ == swatch_attribute.filterable_in_storefront
+ )
+ assert (
+ content["data"]["attribute"]["filterableInDashboard"]
+ == swatch_attribute.filterable_in_dashboard
+ )
+ assert (
+ content["data"]["attribute"]["availableInGrid"]
+ == swatch_attribute.available_in_grid
+ )
+ assert (
+ content["data"]["attribute"]["storefrontSearchPosition"]
+ == swatch_attribute.storefront_search_position
+ )
+ assert (
+ len(content["data"]["attribute"]["values"])
+ == swatch_attribute.values.all().count()
+ )
+ attribute_value_data = []
+ for value in swatch_attribute.values.all():
+ data = {
+ "slug": value.slug,
+ "value": value.value,
+ "inputType": value.input_type.upper(),
+ "file": {"url": value.file_url, "contentType": value.content_type}
+ if value.file_url
+ else None,
+ }
+ attribute_value_data.append(data)
+
+ for data in attribute_value_data:
+ assert data in content["data"]["attribute"]["values"]
+
+
QUERY_ATTRIBUTES = """
query {
attributes(first: 20) {
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index e60d32d6e2..c50295a3f6 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -4026,6 +4026,116 @@ def test_create_product_with_numeric_attribute_existing_value(
assert numeric_attribute.values.count() == values_count + 1
+def test_create_product_with_swatch_attribute_new_attribute_value(
+ staff_api_client,
+ product_type,
+ category,
+ swatch_attribute,
+ permission_manage_products,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ values_count = swatch_attribute.values.count()
+ new_value = "Yellow"
+
+ # Add second attribute
+ product_type.product_attributes.set([swatch_attribute])
+ attr_id = graphene.Node.to_global_id("Attribute", swatch_attribute.id)
+
+ # test creating root product
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": attr_id, "values": [new_value]}],
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["errors"] == []
+ assert data["product"]["name"] == product_name
+ assert data["product"]["slug"] == product_slug
+ assert data["product"]["productType"]["name"] == product_type.name
+ assert data["product"]["category"]["name"] == category.name
+ assert len(data["product"]["attributes"]) == 1
+ assert (
+ data["product"]["attributes"][0]["attribute"]["slug"] == swatch_attribute.slug
+ )
+ values = data["product"]["attributes"][0]["values"]
+ assert len(values) == 1
+ assert values[0]["name"] == new_value
+ assert values[0]["slug"] == slugify(new_value)
+
+ swatch_attribute.refresh_from_db()
+ assert swatch_attribute.values.count() == values_count + 1
+
+
+def test_create_product_with_swatch_attribute_existing_value(
+ staff_api_client,
+ product_type,
+ category,
+ swatch_attribute,
+ permission_manage_products,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ values_count = swatch_attribute.values.count()
+
+ # Add second attribute
+ product_type.product_attributes.set([swatch_attribute])
+ attr_id = graphene.Node.to_global_id("Attribute", swatch_attribute.id)
+ existing_value = swatch_attribute.values.first()
+
+ # test creating root product
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": attr_id, "values": [existing_value.name]}],
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["errors"] == []
+ assert data["product"]["name"] == product_name
+ assert data["product"]["slug"] == product_slug
+ assert data["product"]["productType"]["name"] == product_type.name
+ assert data["product"]["category"]["name"] == category.name
+ assert len(data["product"]["attributes"]) == 1
+ assert (
+ data["product"]["attributes"][0]["attribute"]["slug"] == swatch_attribute.slug
+ )
+ values = data["product"]["attributes"][0]["values"]
+ assert len(values) == 1
+ assert values[0]["name"] == existing_value.name
+ assert values[0]["slug"] == existing_value.slug
+
+ swatch_attribute.refresh_from_db()
+ assert swatch_attribute.values.count() == values_count
+
+
def test_create_product_with_numeric_attribute_not_numeric_value_given(
staff_api_client,
product_type,
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 937c3e42c5..cae35803ef 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -2777,6 +2777,52 @@ def test_product_variant_bulk_create_by_attribute_id(
assert product_variant_created_webhook_mock.call_count == data["count"]
+def test_product_variant_bulk_create_with_swatch_attribute(
+ staff_api_client, product, swatch_attribute, permission_manage_products
+):
+ product_variant_count = ProductVariant.objects.count()
+ product.product_type.variant_attributes.set([swatch_attribute])
+ attribute_value_count = swatch_attribute.values.count()
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribute_id = graphene.Node.to_global_id("Attribute", swatch_attribute.pk)
+ attribute_value_1 = swatch_attribute.values.first()
+ attribute_value_2 = swatch_attribute.values.last()
+ sku = str(uuid4())[:12]
+ variants = [
+ {
+ "sku": sku,
+ "weight": 2.5,
+ "trackInventory": True,
+ "attributes": [{"id": attribute_id, "values": [attribute_value_1.name]}],
+ },
+ {
+ "sku": sku + "a",
+ "weight": 2.5,
+ "trackInventory": True,
+ "attributes": [{"id": attribute_id, "values": [attribute_value_2.name]}],
+ },
+ ]
+
+ variables = {"productId": product_id, "variants": variants}
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantBulkCreate"]
+ assert not data["errors"]
+ assert data["count"] == 2
+ assert {variant["name"] for variant in data["productVariants"]} == {
+ attribute_value_1.name,
+ attribute_value_2.name,
+ }
+ assert product_variant_count + 2 == ProductVariant.objects.count()
+ assert attribute_value_count == swatch_attribute.values.count()
+ product_variant = ProductVariant.objects.get(sku=sku)
+ product.refresh_from_db()
+ assert product.default_variant == product_variant
+
+
def test_product_variant_bulk_create_only_not_variant_selection_attributes(
staff_api_client, product, size_attribute, permission_manage_products
):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index c131e6a921..46462f2a5a 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -514,6 +514,7 @@ enum AttributeInputTypeEnum {
REFERENCE
NUMERIC
RICH_TEXT
+ SWATCH
}
type AttributeReorderValues {
@@ -574,7 +575,7 @@ input AttributeUpdateInput {
slug: String
unit: MeasurementUnitsEnum
removeValues: [ID]
- addValues: [AttributeValueCreateInput]
+ addValues: [AttributeValueUpdateInput]
valueRequired: Boolean
isVariantOnly: Boolean
visibleInStorefront: Boolean
@@ -610,9 +611,11 @@ type AttributeValueCreate {
}
input AttributeValueCreateInput {
- name: String!
value: String
richText: JSONString
+ fileUrl: String
+ contentType: String
+ name: String!
}
type AttributeValueDelete {
@@ -663,6 +666,14 @@ type AttributeValueUpdate {
attributeValue: AttributeValue
}
+input AttributeValueUpdateInput {
+ value: String
+ richText: JSONString
+ fileUrl: String
+ contentType: String
+ name: String
+}
+
input BulkAttributeValueInput {
id: ID
values: [String]!
@@ -2865,7 +2876,7 @@ type Mutation {
attributeValueBulkDelete(ids: [ID]!): AttributeValueBulkDelete
attributeValueCreate(attribute: ID!, input: AttributeValueCreateInput!): AttributeValueCreate
attributeValueDelete(id: ID!): AttributeValueDelete
- attributeValueUpdate(id: ID!, input: AttributeValueCreateInput!): AttributeValueUpdate
+ attributeValueUpdate(id: ID!, input: AttributeValueUpdateInput!): AttributeValueUpdate
attributeValueTranslate(id: ID!, input: AttributeValueTranslationInput!, languageCode: LanguageCodeEnum!): AttributeValueTranslate
attributeReorderValues(attributeId: ID!, moves: [ReorderInput]!): AttributeReorderValues
appCreate(input: AppInput!): AppCreate
diff --git a/saleor/product/tests/test_get_variant_selection_attributes.py b/saleor/product/tests/test_get_variant_selection_attributes.py
index 2f078ef212..aea0265b6a 100644
--- a/saleor/product/tests/test_get_variant_selection_attributes.py
+++ b/saleor/product/tests/test_get_variant_selection_attributes.py
@@ -5,6 +5,7 @@ from ..utils.variants import get_variant_selection_attributes
def test_get_variant_selection_attributes(
product_type_attribute_list,
numeric_attribute,
+ swatch_attribute,
file_attribute_with_file_input_type_without_values,
product_type_page_reference_attribute,
product_type_product_reference_attribute,
@@ -16,6 +17,7 @@ def test_get_variant_selection_attributes(
attrs = product_type_attribute_list + [
numeric_attribute,
+ swatch_attribute,
file_attribute_with_file_input_type_without_values,
product_type_page_reference_attribute,
product_type_product_reference_attribute,
@@ -25,4 +27,6 @@ def test_get_variant_selection_attributes(
result = get_variant_selection_attributes(attrs)
# then
- assert result == product_type_attribute_list[1:]
+ assert result == product_type_attribute_list[1:] + [
+ swatch_attribute,
+ ]
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 8f37477c98..976d8a0988 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -917,6 +917,33 @@ def file_attribute_with_file_input_type_without_values(db):
)
+def swatch_attribute(db):
+ attribute = Attribute.objects.create(
+ slug="T-shirt color",
+ name="t-shirt-color",
+ type=AttributeType.PRODUCT_TYPE,
+ input_type=AttributeInputType.SWATCH,
+ filterable_in_storefront=True,
+ filterable_in_dashboard=True,
+ available_in_grid=True,
+ )
+ AttributeValue.objects.create(
+ attribute=attribute, name="Red", slug="red", value="#ff0000"
+ )
+ AttributeValue.objects.create(
+ attribute=attribute, name="White", slug="whit", value="#fffff"
+ )
+ AttributeValue.objects.create(
+ attribute=attribute,
+ name="Logo",
+ slug="logo",
+ file_url="http://mirumee.com/test_media/test_file.jpeg",
+ content_type="image/jpeg",
+ )
+ return attribute
+
+
@pytest.fixture
def product_type_page_reference_attribute(db):
return Attribute.objects.create(