Skip to content

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

Slug generated from nase shouldnt use unicode (#10744)

Filip Owczarekcc5008ad7c06
diff --git a/saleor/core/tests/test_core.py b/saleor/core/tests/test_core.py
index 220c4350a9..e9515fe74a 100644
--- a/saleor/core/tests/test_core.py
+++ b/saleor/core/tests/test_core.py
@@ -242,8 +242,10 @@ def test_delete_sort_order_with_null_value(menu_item):
("Shirt", "shirt"),
("40.5", "405-2"),
("FM1+", "fm1-2"),
- ("زيوت", "زيوت"),
- ("わたし-わ にっぽん です", "わたし-わ-にっぽん-です-2"),
+ ("Ładny", "ladny"),
+ ("زيوت", "zywt"),
+ ("わたし-わ にっぽん です", "watasi-wa-nitupon-desu-2"),
+ ("Салеор", "saleor-2"),
],
)
def test_generate_unique_slug_with_slugable_field(
@@ -253,9 +255,10 @@ def test_generate_unique_slug_with_slugable_field(
("Paint", "paint"),
("Paint blue", "paint-blue"),
("Paint test", "paint-2"),
+ ("Saleor", "saleor"),
("405", "405"),
("FM1", "fm1"),
- ("わたし わ にっぽん です", "わたし-わ-にっぽん-です"),
+ ("わたし わ にっぽん です", "watasi-wa-nitupon-desu"),
]
for name, slug in product_names_and_slugs:
ProductType.objects.create(
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 246f14411b..e048e16c5b 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -12,6 +12,7 @@ from django.utils.encoding import iri_to_uri
from django.utils.text import slugify
from django_prices_openexchangerates import exchange_currency
from prices import MoneyRange
+from text_unidecode import unidecode
task_logger = get_task_logger(__name__)
@@ -114,7 +115,7 @@ def generate_unique_slug(
with the same slug that passed also additional conditions
"""
- slug = slugify(slugable_value, allow_unicode=True)
+ slug = slugify(unidecode(slugable_value))
unique_slug: Union["SafeText", str] = slug
ModelClass = instance.__class__
diff --git a/saleor/graphql/attribute/mutations.py b/saleor/graphql/attribute/mutations.py
index ba16b0faec..3093dc19b5 100644
--- a/saleor/graphql/attribute/mutations.py
+++ b/saleor/graphql/attribute/mutations.py
@@ -5,6 +5,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
from django.db.models import Exists, OuterRef, Q, Subquery
from django.utils.text import slugify
+from text_unidecode import unidecode
from ...attribute import ATTRIBUTE_PROPERTIES_CONFIGURATION, AttributeInputType
from ...attribute import models as models
@@ -332,7 +333,7 @@ class AttributeMixin:
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)
+ value_data["slug"] = slugify(unidecode(slug_value))
attribute_value = models.AttributeValue(**value_data, attribute=attribute)
try:
@@ -390,7 +391,7 @@ class AttributeMixin:
# 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)
+ slug = slugify(unidecode(value_data["name"]))
if slug in existing_values:
msg = (
"Value %s already exists within this attribute."
@@ -405,8 +406,7 @@ class AttributeMixin:
)
new_slugs = [
- slugify(value_data["name"], allow_unicode=True)
- for value_data in values_input
+ slugify(unidecode(value_data["name"])) for value_data in values_input
]
if len(set(new_slugs)) != len(new_slugs):
raise ValidationError(
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index ac0413ad98..b9e00496ae 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -13,6 +13,7 @@ from django.template.defaultfilters import truncatechars
from django.utils import timezone
from django.utils.text import slugify
from graphql.error import GraphQLError
+from text_unidecode import unidecode
from ...attribute import AttributeEntityType, AttributeInputType, AttributeType
from ...attribute import models as attribute_models
@@ -339,7 +340,7 @@ class AttributeAssignmentMixin:
return tuple(
get_or_create(
attribute=attribute,
- slug=slugify(value, allow_unicode=True),
+ slug=slugify(unidecode(value)),
defaults={"name": value},
)[0]
for value in attr_values.values
@@ -404,7 +405,7 @@ class AttributeAssignmentMixin:
boolean = bool(attr_values.boolean)
value, _ = get_or_create(
attribute=attribute,
- slug=slugify(f"{attribute.id}_{boolean}", allow_unicode=True),
+ slug=slugify(unidecode(f"{attribute.id}_{boolean}")),
defaults={
"name": f"{attribute.name}: {'Yes' if boolean else 'No'}",
"boolean": boolean,
@@ -446,7 +447,7 @@ class AttributeAssignmentMixin:
value_defaults: dict,
):
update_or_create = attribute.values.update_or_create
- slug = slugify(f"{instance.id}_{attribute.id}", allow_unicode=True)
+ slug = slugify(unidecode(f"{instance.id}_{attribute.id}"))
value, _created = update_or_create(
attribute=attribute,
slug=slug,
@@ -481,10 +482,7 @@ class AttributeAssignmentMixin:
reference_list.append(
get_or_create(
attribute=attribute,
- slug=slugify(
- f"{instance.id}_{ref.id}", # type: ignore
- allow_unicode=True,
- ),
+ slug=slugify(unidecode(f"{instance.id}_{ref.id}")), # type: ignore
defaults={"name": name},
**{attr_value_field: ref},
)[0]
diff --git a/saleor/graphql/product/tests/test_category.py b/saleor/graphql/product/tests/test_category.py
index f4fd0858c3..c03cac64b3 100644
--- a/saleor/graphql/product/tests/test_category.py
+++ b/saleor/graphql/product/tests/test_category.py
@@ -543,7 +543,7 @@ def test_create_category_name_with_unicode(
data = content["data"]["categoryCreate"]
assert not data["errors"]
assert data["category"]["name"] == name
- assert data["category"]["slug"] == "わたし-わ-にっぽん-です"
+ assert data["category"]["slug"] == "watasi-wa-nitupon-desu"
def test_category_create_mutation_without_background_image(
diff --git a/saleor/graphql/product/tests/test_collection.py b/saleor/graphql/product/tests/test_collection.py
index cafe847c9b..479370dbac 100644
--- a/saleor/graphql/product/tests/test_collection.py
+++ b/saleor/graphql/product/tests/test_collection.py
@@ -600,7 +600,7 @@ def test_create_collection_name_with_unicode(
data = content["data"]["collectionCreate"]
assert not data["errors"]
assert data["collection"]["name"] == name
- assert data["collection"]["slug"] == "わたし-わ-にっぽん-です"
+ assert data["collection"]["slug"] == "watasi-wa-nitupon-desu"
@patch("saleor.plugins.manager.PluginsManager.collection_updated")
diff --git a/saleor/graphql/product/tests/test_product_type_create.py b/saleor/graphql/product/tests/test_product_type_create.py
index da6d8aedbf..4a735344b7 100644
--- a/saleor/graphql/product/tests/test_product_type_create.py
+++ b/saleor/graphql/product/tests/test_product_type_create.py
@@ -468,7 +468,7 @@ def test_create_product_type_with_unicode_in_name(
data = content["data"]["productTypeCreate"]
assert not data["errors"]
assert data["productType"]["name"] == name
- assert data["productType"]["slug"] == "わたし-わ-にっぽん-です"
+ assert data["productType"]["slug"] == "watasi-wa-nitupon-desu"
assert data["productType"]["kind"] == kind