Skip to content

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

Change key in Attribute value

fowczarek8ab782f937a4
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 547ebf01a2..22606ec175 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -595,7 +595,9 @@ class AttributeAssignmentMixin:
"""Lazy-retrieve or create the database objects from the supplied raw values."""
get_or_create = attribute.values.get_or_create
return tuple(
- get_or_create(attribute=attribute, name=value, slug=slugify(value))[0]
+ get_or_create(
+ attribute=attribute, slug=slugify(value), defaults={"name": value}
+ )[0]
for value in values
)
diff --git a/saleor/product/migrations/0108_auto_20191003_0422.py b/saleor/product/migrations/0108_auto_20191003_0422.py
new file mode 100644
index 0000000000..83a8baa7a7
--- /dev/null
+++ b/saleor/product/migrations/0108_auto_20191003_0422.py
@@ -0,0 +1,14 @@
+# Generated by Django 2.2.4 on 2019-10-03 09:22
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("product", "0107_attributes_map_to_m2m")]
+
+ operations = [
+ migrations.AlterUniqueTogether(
+ name="attributevalue", unique_together={("slug", "attribute")}
+ )
+ ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 1a1be5e868..e45bbf4298 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -837,7 +837,7 @@ class AttributeValue(SortableModel):
class Meta:
ordering = ("sort_order", "id")
- unique_together = ("name", "attribute")
+ unique_together = ("slug", "attribute")
def __str__(self):
return self.name
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index c1c4d5ff78..5d1b794c29 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -792,9 +792,10 @@ CREATE_ATTRIBUTE_VALUE_QUERY = """
$attributeId: ID!, $name: String!) {
attributeValueCreate(
attribute: $attributeId, input: {name: $name}) {
- errors {
+ productErrors {
field
message
+ code
}
attribute {
values {
@@ -824,7 +825,7 @@ def test_create_attribute_value(
)
content = get_graphql_content(response)
data = content["data"]["attributeValueCreate"]
- assert not data["errors"]
+ assert not data["productErrors"]
attr_data = data["attributeValue"]
assert attr_data["name"] == name
@@ -846,9 +847,27 @@ def test_create_attribute_value_not_unique_name(
)
content = get_graphql_content(response)
data = content["data"]["attributeValueCreate"]
- assert data["errors"]
- assert data["errors"][0]["message"]
- assert data["errors"][0]["field"] == "name"
+ assert data["productErrors"]
+ assert data["productErrors"][0]["code"] == ProductErrorCode.ALREADY_EXISTS.name
+ assert data["productErrors"][0]["field"] == "name"
+
+
+def test_create_attribute_value_capitalized_name(
+ staff_api_client, color_attribute, permission_manage_products
+):
+ attribute = color_attribute
+ query = CREATE_ATTRIBUTE_VALUE_QUERY
+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ value_name = attribute.values.first().name
+ variables = {"name": value_name.upper(), "attributeId": attribute_id}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["attributeValueCreate"]
+ assert data["productErrors"]
+ assert data["productErrors"][0]["code"] == ProductErrorCode.ALREADY_EXISTS.name
+ assert data["productErrors"][0]["field"] == "name"
UPDATE_ATTRIBUTE_VALUE_QUERY = """