Skip to content

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

Update mutations with slug

IKarbowiaka5c074be049f
diff --git a/saleor/graphql/page/mutations.py b/saleor/graphql/page/mutations.py
index a29b05b461..341b59a213 100644
--- a/saleor/graphql/page/mutations.py
+++ b/saleor/graphql/page/mutations.py
@@ -40,7 +40,7 @@ class PageCreate(ModelMutation):
cleaned_input = super().clean_input(info, instance, data)
slug = cleaned_input.get("slug", "")
title = cleaned_input.get("title", "")
- if title and not slug:
+ if not instance.slug and not slug and title:
cleaned_input["slug"] = generate_unique_slug(instance, slugable_value=title)
clean_seo_fields(cleaned_input)
return cleaned_input
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 857ad5880c..628766c32e 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -166,7 +166,7 @@ class AttributeMixin:
@classmethod
def clean_attribute(cls, instance, cleaned_input):
input_slug = cleaned_input.get("slug", None)
- if input_slug is None:
+ if not instance.slug and input_slug is None:
cleaned_input["slug"] = generate_unique_slug(
instance, slugable_value=cleaned_input["name"]
)
@@ -180,7 +180,8 @@ class AttributeMixin:
}
)
- query = models.Attribute.objects.filter(slug=cleaned_input["slug"])
+ slug = input_slug if "slug" in cleaned_input else instance.slug
+ query = models.Attribute.objects.filter(slug=slug)
if instance.pk:
query = query.exclude(pk=instance.pk)
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 00c013bdd4..c72f5b7846 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -95,7 +95,11 @@ class CategoryCreate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
- if "slug" not in cleaned_input and "name" in cleaned_input:
+ if (
+ not instance.slug
+ and "slug" not in cleaned_input
+ and "name" in cleaned_input
+ ):
cleaned_input["slug"] = generate_unique_slug(
instance, slugable_value=cleaned_input["name"]
)
@@ -207,7 +211,11 @@ class CollectionCreate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
- if "slug" not in cleaned_input and "name" in cleaned_input:
+ if (
+ not instance.slug
+ and "slug" not in cleaned_input
+ and "name" in cleaned_input
+ ):
cleaned_input["slug"] = generate_unique_slug(
instance, slugable_value=cleaned_input["name"]
)
@@ -1334,7 +1342,11 @@ class ProductTypeCreate(ModelMutation):
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
- if "slug" not in cleaned_input and "name" in cleaned_input:
+ if (
+ not instance.slug
+ and "slug" not in cleaned_input
+ and "name" in cleaned_input
+ ):
cleaned_input["slug"] = generate_unique_slug(
instance, slugable_value=cleaned_input["name"]
)
diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.py
index 80d9cbb157..df0dd9e63a 100644
--- a/saleor/graphql/warehouse/mutations.py
+++ b/saleor/graphql/warehouse/mutations.py
@@ -27,7 +27,11 @@ class WarehouseMixin:
@classmethod
def clean_input(cls, info, instance, data, input_cls=None):
cleaned_input = super().clean_input(info, instance, data)
- if "slug" not in cleaned_input and "name" in cleaned_input:
+ if (
+ not instance.slug
+ and "slug" not in cleaned_input
+ and "name" in cleaned_input
+ ):
cleaned_input["slug"] = generate_unique_slug(
instance, slugable_value=cleaned_input["name"]
)
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 56c66f7301..ee2ff63c01 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -559,7 +559,7 @@ def test_create_attribute_and_attribute_values_errors(
assert product_errors[0]["code"] == error_code.name
-UPDATE_ATTRIBUTE_QUERY = """
+UPDATE_ATTRIBUTE_MUTATION = """
mutation updateAttribute(
$id: ID!, $name: String!, $addValues: [AttributeValueCreateInput]!,
$removeValues: [ID]!) {
@@ -600,9 +600,10 @@ UPDATE_ATTRIBUTE_QUERY = """
def test_update_attribute_name(
staff_api_client, color_attribute, permission_manage_products
):
- query = UPDATE_ATTRIBUTE_QUERY
+ query = UPDATE_ATTRIBUTE_MUTATION
attribute = color_attribute
name = "Wings name"
+ slug = attribute.slug
node_id = graphene.Node.to_global_id("Attribute", attribute.id)
variables = {"name": name, "id": node_id, "addValues": [], "removeValues": []}
response = staff_api_client.post_graphql(
@@ -612,13 +613,14 @@ def test_update_attribute_name(
attribute.refresh_from_db()
data = content["data"]["attributeUpdate"]
assert data["attribute"]["name"] == name == attribute.name
+ assert data["attribute"]["slug"] == slug == attribute.slug
assert data["attribute"]["productTypes"]["edges"] == []
def test_update_attribute_remove_and_add_values(
staff_api_client, color_attribute, permission_manage_products
):
- query = UPDATE_ATTRIBUTE_QUERY
+ query = UPDATE_ATTRIBUTE_MUTATION
attribute = color_attribute
name = "Wings name"
attribute_value_name = "Red Color"
@@ -646,7 +648,7 @@ def test_update_attribute_remove_and_add_values(
def test_update_empty_attribute_and_add_values(
staff_api_client, color_attribute_without_values, permission_manage_products
):
- query = UPDATE_ATTRIBUTE_QUERY
+ query = UPDATE_ATTRIBUTE_MUTATION
attribute = color_attribute_without_values
name = "Wings name"
attribute_value_name = "Yellow Color"
@@ -666,6 +668,52 @@ def test_update_empty_attribute_and_add_values(
assert attribute.values.filter(name=attribute_value_name).exists()
+def test_update_attribute_slug(
+ staff_api_client, color_attribute, permission_manage_products
+):
+ query = """
+ mutation updateAttribute(
+ $id: ID!, $slug: String) {
+ attributeUpdate(
+ id: $id,
+ input: {
+ slug: $slug}) {
+ errors {
+ field
+ message
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ attribute {
+ name
+ slug
+ }
+ }
+ }
+ """
+
+ attribute = color_attribute
+ name = attribute.name
+ old_slug = attribute.slug
+ new_slug = "test-slug"
+
+ assert new_slug != old_slug
+
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"slug": new_slug, "id": node_id}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ attribute.refresh_from_db()
+ data = content["data"]["attributeUpdate"]
+ assert data["attribute"]["name"] == name == attribute.name
+ assert data["attribute"]["slug"] == new_slug == attribute.slug
+
+
@pytest.mark.parametrize(
"name_1, name_2, error_msg, error_code",
(
@@ -692,7 +740,7 @@ def test_update_attribute_and_add_attribute_values_errors(
color_attribute,
permission_manage_products,
):
- query = UPDATE_ATTRIBUTE_QUERY
+ query = UPDATE_ATTRIBUTE_MUTATION
attribute = color_attribute
node_id = graphene.Node.to_global_id("Attribute", attribute.id)
variables = {
@@ -717,7 +765,7 @@ def test_update_attribute_and_add_attribute_values_errors(
def test_update_attribute_and_remove_others_attribute_value(
staff_api_client, color_attribute, size_attribute, permission_manage_products
):
- query = UPDATE_ATTRIBUTE_QUERY
+ query = UPDATE_ATTRIBUTE_MUTATION
attribute = color_attribute
node_id = graphene.Node.to_global_id("Attribute", attribute.id)
size_attribute = size_attribute.values.first()
@@ -725,7 +773,6 @@ def test_update_attribute_and_remove_others_attribute_value(
variables = {
"name": "Example name",
"id": node_id,
- "slug": "example-slug",
"addValues": [],
"removeValues": [attr_id],
}
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 8a65c5edf4..3a706b4b21 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1948,6 +1948,7 @@ def test_product_type_update_mutation(
}
"""
product_type_name = "test type updated"
+ slug = product_type.slug
has_variants = True
require_shipping = False
product_type_id = graphene.Node.to_global_id("ProductType", product_type.id)
@@ -1973,7 +1974,7 @@ def test_product_type_update_mutation(
content = get_graphql_content(response)
data = content["data"]["productTypeUpdate"]["productType"]
assert data["name"] == product_type_name
- assert data["slug"] == "test-type-updated"
+ assert data["slug"] == slug
assert data["hasVariants"] == has_variants
assert data["isShippingRequired"] == require_shipping
assert not data["productAttributes"]
diff --git a/tests/api/test_warehouse.py b/tests/api/test_warehouse.py
index 5996608e03..12a0efb600 100644
--- a/tests/api/test_warehouse.py
+++ b/tests/api/test_warehouse.py
@@ -366,6 +366,7 @@ def test_mutation_update_warehouse(
staff_api_client.user.user_permissions.add(permission_manage_products)
warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)
warehouse_old_name = warehouse.name
+ warehouse_slug = warehouse.slug
warehouse_old_company_name = warehouse.company_name
variables = {
"id": warehouse_id,
@@ -380,7 +381,7 @@ def test_mutation_update_warehouse(
assert not (warehouse.name == warehouse_old_name)
assert not (warehouse.company_name == warehouse_old_company_name)
assert warehouse.name == "New name"
- assert warehouse.slug == "new-name"
+ assert warehouse.slug == warehouse_slug
assert warehouse.company_name == "New name for company"
assert warehouse.shipping_zones.count() == 0
diff --git a/tests/conftest.py b/tests/conftest.py
index 78f18ac34b..3d197ba93e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1855,7 +1855,7 @@ def warehouse(address, shipping_zone):
warehouse = Warehouse.objects.create(
address=address,
name="Example Warehouse",
- slug="example=warehouse",
+ slug="example-warehouse",
)
warehouse.shipping_zones.add(shipping_zone)