Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Allow values of different attributes to share the same slug (#10138)
Iga Karbowiak834d9500b29c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3759b4a542..91320c10ee 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,9 @@ All notable, unreleased changes to this project will be documented in this file.# 3.6.0 [Unreleased]+### Other changes+- Allow values of different attributes to share the same slug - #10138 by @IKarbowiak+# 3.5.0### GraphQL APIdiff --git a/saleor/core/tests/test_core.py b/saleor/core/tests/test_core.pyindex 2738c759a0..60891cda0b 100644--- a/saleor/core/tests/test_core.py+++ b/saleor/core/tests/test_core.py@@ -13,6 +13,7 @@ from django_countries.fields import Countryfrom ...account.models import Address, Userfrom ...account.utils import create_superuser+from ...attribute.models import AttributeValuefrom ...channel.models import Channelfrom ...discount.models import Sale, SaleChannelListing, Voucher, VoucherChannelListingfrom ...giftcard.models import GiftCard, GiftCardEvent@@ -317,6 +318,42 @@ def test_generate_unique_slug_non_slugable_value_and_slugable_field(category):generate_unique_slug(category)+def test_generate_unique_slug_with_additional_lookup_slug_not_changed(+ color_attribute, attribute_without_values+):+ # given+ value_1 = color_attribute.values.first()++ # when+ value_2 = AttributeValue(name=value_1.name, attribute=attribute_without_values)++ # then+ result = generate_unique_slug(+ value_2,+ value_2.name,+ additional_search_lookup={"attribute": value_2.attribute_id},+ )++ assert result == value_1.slug+++def test_generate_unique_slug_with_additional_lookup_slug_changed(color_attribute):+ # given+ value_1 = color_attribute.values.first()++ # when+ value_2 = AttributeValue(name=value_1.name, attribute=color_attribute)++ # then+ result = generate_unique_slug(+ value_2,+ value_2.name,+ additional_search_lookup={"attribute": value_2.attribute_id},+ )++ assert result == f"{value_1.slug}-2"++@override_settings(DEBUG=False)def test_cleardb_exits_with_debug_off():with pytest.raises(CommandError):diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex bd47291874..75e9a83b8d 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -119,6 +119,8 @@ def generate_unique_slug(instance: Type[Model],slugable_value: str,slug_field_name: str = "slug",+ *,+ additional_search_lookup=None,) -> str:"""Create unique slug for model instance.@@ -130,6 +132,8 @@ def generate_unique_slug(instance: model instance for which slug is createdslugable_value: value used to create slugslug_field_name: name of slug field in instance model+ additional_search_lookup: when provided, it will be used to find the instances+ with the same slug that passed also additional conditions"""slug = slugify(slugable_value, allow_unicode=True)@@ -140,8 +144,12 @@ def generate_unique_slug(search_field = f"{slug_field_name}__iregex"pattern = rf"{slug}-\d+$|{slug}$"+ lookup = {search_field: pattern}+ if additional_search_lookup:+ lookup.update(additional_search_lookup)+slug_values = (- ModelClass._default_manager.filter(**{search_field: pattern}) # type: ignore+ ModelClass._default_manager.filter(**lookup) # type: ignore.exclude(pk=instance.pk).values_list(slug_field_name, flat=True))diff --git a/saleor/graphql/attribute/mutations.py b/saleor/graphql/attribute/mutations.pyindex 326e5113c9..ac77d7ad6c 100644--- a/saleor/graphql/attribute/mutations.py+++ b/saleor/graphql/attribute/mutations.py@@ -656,7 +656,9 @@ class AttributeValueCreate(AttributeMixin, ModelMutation):cleaned_input = super().clean_input(info, instance, data)if "name" in cleaned_input:cleaned_input["slug"] = generate_unique_slug(- instance, cleaned_input["name"]+ instance,+ cleaned_input["name"],+ additional_search_lookup={"attribute_id": instance.attribute_id},)input_type = instance.attribute.input_typediff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.pyindex 4c5a2d10da..eabe6d9fd8 100644--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py@@ -166,6 +166,42 @@ def test_create_attribute_value_trigger_webhooks(assert attribute_value_created_call in mocked_webhook_trigger.call_args_list+def test_create_attribute_value_with_the_same_name_as_different_attribute_value(+ staff_api_client,+ attribute_without_values,+ color_attribute,+ permission_manage_products,+):+ """Ensure the attribute value with the same slug as value of different attribute+ can be created."""+ # given+ attribute = attribute_without_values+ query = CREATE_ATTRIBUTE_VALUE_MUTATION+ attribute_id = graphene.Node.to_global_id("Attribute", attribute.id)++ existing_value = color_attribute.values.first()++ name = existing_value.name+ 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"] == existing_value.slug+ assert name in [+ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]+ ]++def test_create_swatch_attribute_value_with_value(staff_api_client, swatch_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.pyindex 70eb16bfe7..9c38ec0891 100644--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py@@ -170,6 +170,40 @@ def test_update_attribute_value_name_not_unique(assert data["attributeValue"]["slug"] == "pink-2"+def test_update_attribute_value_the_same_name_as_different_attribute_value(+ staff_api_client,+ size_attribute,+ color_attribute,+ permission_manage_product_types_and_attributes,+):+ """Ensure the attribute value with the same slug as value of different attribute+ can be set."""+ # given+ query = UPDATE_ATTRIBUTE_VALUE_MUTATION++ value = size_attribute.values.first()+ based_value = color_attribute.values.first()++ node_id = graphene.Node.to_global_id("AttributeValue", value.id)+ name = based_value.name+ variables = {"input": {"name": name}, "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"] == name == value.name+ assert data["attributeValue"]["slug"] == based_value.slug+ assert name in [+ value["node"]["name"] for value in data["attribute"]["choices"]["edges"]+ ]++def test_update_attribute_value_product_search_document_updated(staff_api_client,pink_attribute_value,