Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix attribute value matching during attribute update (#10881)
Jakuba7a2bf4146ba
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex afc612443e..38b69f9c37 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,7 @@ All notable, unreleased changes to this project will be documented in this file.- Deprecated `slug` in filter for `menus`. Use `slugs` instead- Add ability to filter payments by list of ids. #10821 by @kadewu- Add ability to filter customers by ids. #10694 by @kadewu+- Use `AttributeValue.name` instead of `AttributeValue.slug` to determine uniqueness of a value instance for dropdown and multiselect attributes. - #10881 by @jakubkuc### Other changesdiff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.pyindex b9e00496ae..b3e03315c2 100644--- a/saleor/graphql/attribute/utils.py+++ b/saleor/graphql/attribute/utils.py@@ -332,19 +332,22 @@ class AttributeAssignmentMixin:cls, attribute: attribute_models.Attribute, attr_values: AttrValuesInput):"""Lazy-retrieve or create the database objects from the supplied raw values."""- get_or_create = attribute.values.get_or_create-+ result = []if not attr_values.values:return tuple()-- return tuple(- get_or_create(- attribute=attribute,- slug=slugify(unidecode(value)),- defaults={"name": value},- )[0]- for value in attr_values.values- )+ for value in attr_values.values:+ value_obj = attribute.values.filter(name=value).first()+ if value_obj:+ result.append(value_obj)+ else:+ instance = attribute_models.AttributeValue(+ attribute=attribute, name=value+ )+ slug = generate_unique_slug(instance, value) # type: ignore+ instance.slug = slug+ instance.save()+ result.append(instance)+ return tuple(result)@classmethoddef _pre_save_numeric_values(diff --git a/saleor/graphql/page/tests/deprecated/test_page_create.py b/saleor/graphql/page/tests/deprecated/test_page_create.pyindex 29e9b9b165..36fab64e3b 100644--- a/saleor/graphql/page/tests/deprecated/test_page_create.py+++ b/saleor/graphql/page/tests/deprecated/test_page_create.py@@ -64,6 +64,7 @@ def test_page_create_mutation_with_publication_date(# Default attributes defined in product_type fixturetag_attr = page_type.page_attributes.get(name="tag")tag_value_slug = tag_attr.values.first().slug+ tag_value_name = tag_attr.values.first().nametag_attr_id = graphene.Node.to_global_id("Attribute", tag_attr.id)# Add second attribute@@ -81,7 +82,7 @@ def test_page_create_mutation_with_publication_date("slug": page_slug,"pageType": page_type_id,"attributes": [- {"id": tag_attr_id, "values": [tag_value_slug]},+ {"id": tag_attr_id, "values": [tag_value_name]},{"id": size_attr_id, "values": [non_existent_attr_value]},],}diff --git a/saleor/graphql/page/tests/mutations/test_page_create.py b/saleor/graphql/page/tests/mutations/test_page_create.pyindex fba032838f..1b870f470e 100644--- a/saleor/graphql/page/tests/mutations/test_page_create.py+++ b/saleor/graphql/page/tests/mutations/test_page_create.py@@ -71,6 +71,7 @@ def test_page_create_mutation(staff_api_client, permission_manage_pages, page_tytag_attr = page_type.page_attributes.get(name="tag")tag_value_slug = tag_attr.values.first().slugtag_attr_id = graphene.Node.to_global_id("Attribute", tag_attr.id)+ tag_value_name = tag_attr.values.first().name# Add second attributesize_attr = page_type.page_attributes.get(name="Page size")@@ -86,7 +87,7 @@ def test_page_create_mutation(staff_api_client, permission_manage_pages, page_ty"slug": page_slug,"pageType": page_type_id,"attributes": [- {"id": tag_attr_id, "values": [tag_value_slug]},+ {"id": tag_attr_id, "values": [tag_value_name]},{"id": size_attr_id, "values": [non_existent_attr_value]},],}@@ -126,6 +127,7 @@ def test_page_create_mutation_with_published_at_date(# Default attributes defined in product_type fixturetag_attr = page_type.page_attributes.get(name="tag")tag_value_slug = tag_attr.values.first().slug+ tag_value_name = tag_attr.values.first().nametag_attr_id = graphene.Node.to_global_id("Attribute", tag_attr.id)# Add second attribute@@ -143,7 +145,7 @@ def test_page_create_mutation_with_published_at_date("slug": page_slug,"pageType": page_type_id,"attributes": [- {"id": tag_attr_id, "values": [tag_value_slug]},+ {"id": tag_attr_id, "values": [tag_value_name]},{"id": size_attr_id, "values": [non_existent_attr_value]},],}diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 1651a3d149..7ee456b616 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -4634,6 +4634,7 @@ def test_create_product(# Default attribute defined in product_type fixturecolor_attr = product_type.product_attributes.get(name="Color")color_value_slug = color_attr.values.first().slug+ color_value_name = color_attr.values.first().namecolor_attr_id = graphene.Node.to_global_id("Attribute", color_attr.id)# Add second attribute@@ -4652,7 +4653,7 @@ def test_create_product("chargeTaxes": product_charge_taxes,"taxCode": product_tax_rate,"attributes": [- {"id": color_attr_id, "values": [color_value_slug]},+ {"id": color_attr_id, "values": [color_value_name]},{"id": size_attr_id, "values": [non_existent_attr_value]},],}diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.pyindex 963d63b69e..8a79144128 100644--- a/saleor/graphql/product/tests/test_variant.py+++ b/saleor/graphql/product/tests/test_variant.py@@ -2662,8 +2662,8 @@ def test_update_product_variant_with_current_attribute("id": variant_id,"sku": sku,"attributes": [- {"id": color_attribute_id, "values": ["red"]},- {"id": size_attribute_id, "values": ["small"]},+ {"id": color_attribute_id, "values": ["Red"]},+ {"id": size_attribute_id, "values": ["Small"]},],}@@ -2682,6 +2682,48 @@ def test_update_product_variant_with_current_attribute(assert variant.attributes.last().values.first().slug == "small"+def test_update_product_variant_with_matching_slugs_different_values(+ staff_api_client,+ product_with_variant_with_two_attributes,+ color_attribute,+ size_attribute,+ permission_manage_products,+):+ product = product_with_variant_with_two_attributes+ variant = product.variants.first()+ sku = str(uuid4())[:12]+ assert not variant.sku == sku+ assert variant.attributes.first().values.first().slug == "red"+ assert variant.attributes.last().values.first().slug == "small"++ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)+ color_attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)+ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)+ color_new_value = "r.ed"+ size_new_value = "SmaLL"+ variables = {+ "id": variant_id,+ "sku": sku,+ "attributes": [+ {"id": color_attribute_id, "values": [color_new_value]},+ {"id": size_attribute_id, "values": [size_new_value]},+ ],+ }++ response = staff_api_client.post_graphql(+ QUERY_UPDATE_VARIANT_ATTRIBUTES,+ variables,+ permissions=[permission_manage_products],+ )+ content = get_graphql_content(response)+ data = content["data"]["productVariantUpdate"]+ assert not data["errors"]+ variant.refresh_from_db()+ assert variant.sku == sku+ assert variant.attributes.first().values.first().slug == "red-2"+ assert variant.attributes.last().values.first().slug == "small-2"++@patch("saleor.plugins.manager.PluginsManager.product_variant_updated")def test_update_variant_with_boolean_attribute(product_variant_updated,@@ -3126,8 +3168,8 @@ def test_update_product_variant_with_new_attribute("id": variant_id,"sku": sku,"attributes": [- {"id": color_attribute_id, "values": ["red"]},- {"id": size_attribute_id, "values": ["big"]},+ {"id": color_attribute_id, "values": ["Red"]},+ {"id": size_attribute_id, "values": ["Big"]},],}