Skip to content

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

Add slug to ProductType and Warehouse mutations

IKarbowiakbee319b7402b
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 104a5c55fc..7fc1e970fa 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1271,6 +1271,7 @@ class ProductVariantClearPrivateMeta(ClearMetaBaseMutation):
class ProductTypeInput(graphene.InputObjectType):
name = graphene.String(description="Name of the product type.")
+ slug = graphene.String(description="Product type slug.")
has_variants = graphene.Boolean(
description=(
"Determines if product of this type has multiple variants. This option "
@@ -1318,6 +1319,9 @@ 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:
+ cleaned_input["slug"] = slugify(cleaned_input["name"])
+
# FIXME tax_rate logic should be dropped after we remove tax_rate from input
tax_rate = cleaned_input.pop("tax_rate", "")
if tax_rate:
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 2c847a8eea..65e9e090b8 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -653,6 +653,7 @@ class ProductType(CountableDjangoObjectType, MetadataObjectType):
"is_digital",
"is_shipping_required",
"name",
+ "slug",
"weight",
"tax_type",
]
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1695048d52..63535ad89f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3300,6 +3300,7 @@ type ProductTranslation implements Node {
type ProductType implements Node {
id: ID!
name: String!
+ slug: String!
hasVariants: Boolean!
isShippingRequired: Boolean!
isDigital: Boolean!
@@ -3374,6 +3375,7 @@ input ProductTypeFilterInput {
input ProductTypeInput {
name: String
+ slug: String
hasVariants: Boolean
productAttributes: [ID]
variantAttributes: [ID]
@@ -4703,6 +4705,7 @@ type VoucherUpdate {
type Warehouse implements Node {
id: ID!
name: String!
+ slug: String!
companyName: String!
shippingZones(before: String, after: String, first: Int, last: Int): ShippingZoneCountableConnection!
address: Address!
@@ -4739,6 +4742,7 @@ type WarehouseCreate {
input WarehouseCreateInput {
name: String!
+ slug: String
companyName: String
shippingZones: [ID]
email: String
@@ -4778,6 +4782,7 @@ type WarehouseUpdate {
input WarehouseUpdateInput {
name: String!
+ slug: String
companyName: String
shippingZones: [ID]
email: String
diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.py
index b6e65f0c5f..4a2aa50833 100644
--- a/saleor/graphql/warehouse/mutations.py
+++ b/saleor/graphql/warehouse/mutations.py
@@ -1,5 +1,6 @@
import graphene
from django.core.exceptions import ValidationError
+from django.utils.text import slugify
from ...core.permissions import ProductPermissions
from ...warehouse import models
@@ -26,6 +27,8 @@ 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:
+ cleaned_input["slug"] = slugify(cleaned_input["name"])
shipping_zones = cleaned_input.get("shipping_zones", [])
if not validate_warehouse_count(shipping_zones, instance):
msg = "Shipping zone can be assigned only to one warehouse."
diff --git a/saleor/graphql/warehouse/types.py b/saleor/graphql/warehouse/types.py
index 78933f7b03..47ba3fdd5a 100644
--- a/saleor/graphql/warehouse/types.py
+++ b/saleor/graphql/warehouse/types.py
@@ -21,6 +21,7 @@ class WarehouseAddressInput(graphene.InputObjectType):
class WarehouseInput(graphene.InputObjectType):
name = graphene.String(description="Warehouse name.", required=True)
+ slug = graphene.String(description="Waregouse slug.")
company_name = graphene.String(description="Company name.")
shipping_zones = graphene.List(
graphene.ID, description="Shipping zones supported by the warehouse."
@@ -48,6 +49,7 @@ class Warehouse(CountableDjangoObjectType):
only_fields = [
"id",
"name",
+ "slug",
"company_name",
"shipping_zones",
"address",
diff --git a/setup.cfg b/setup.cfg
index 6ac798e6f1..2788e7ba91 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -78,3 +78,6 @@ django_settings_module = saleor.settings
[mypy-saleor.*.migrations.*]
ignore_errors = True
+
+[mypy-tests.*]
+ignore_errors = True
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index 76a0cdd0e8..016952f645 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -134,7 +134,7 @@ def test_category_create_mutation(
"name": category_name,
"description": category_description,
"parentId": parent_id,
- "slug": category_slug,
+ "slug": f"{category_slug}-2",
}
response = staff_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index eb1d049734..ebd14bf605 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -231,7 +231,10 @@ def test_products_query_with_filter_attributes(
):
product_type = ProductType.objects.create(
- name="Custom Type", has_variants=True, is_shipping_required=True
+ name="Custom Type",
+ slug="custom-type",
+ has_variants=True,
+ is_shipping_required=True,
)
attribute = Attribute.objects.create(slug="new_attr", name="Attr")
attribute.product_types.add(product_type)
@@ -263,7 +266,10 @@ def test_products_query_with_filter_product_type(
query_products_with_filter, staff_api_client, product, permission_manage_products
):
product_type = ProductType.objects.create(
- name="Custom Type", has_variants=True, is_shipping_required=True
+ name="Custom Type",
+ slug="custom-type",
+ has_variants=True,
+ is_shipping_required=True,
)
second_product = product
second_product.id = None
@@ -1685,6 +1691,7 @@ def test_product_type_create_mutation(
query = """
mutation createProductType(
$name: String!,
+ $slug: String!,
$taxCode: String!,
$hasVariants: Boolean!,
$isShippingRequired: Boolean!,
@@ -1693,6 +1700,7 @@ def test_product_type_create_mutation(
productTypeCreate(
input: {
name: $name,
+ slug: $slug,
taxCode: $taxCode,
hasVariants: $hasVariants,
isShippingRequired: $isShippingRequired,
@@ -1700,6 +1708,7 @@ def test_product_type_create_mutation(
variantAttributes: $variantAttributes}) {
productType {
name
+ slug
taxRate
isShippingRequired
hasVariants
@@ -1720,6 +1729,7 @@ def test_product_type_create_mutation(
}
"""
product_type_name = "test type"
+ slug = "test-type"
has_variants = True
require_shipping = True
product_attributes = product_type.product_attributes.all()
@@ -1733,6 +1743,7 @@ def test_product_type_create_mutation(
variables = {
"name": product_type_name,
+ "slug": slug,
"hasVariants": has_variants,
"taxCode": "wine",
"isShippingRequired": require_shipping,
@@ -1747,6 +1758,7 @@ def test_product_type_create_mutation(
assert ProductType.objects.count() == initial_count + 1
data = content["data"]["productTypeCreate"]["productType"]
assert data["name"] == product_type_name
+ assert data["slug"] == slug
assert data["hasVariants"] == has_variants
assert data["isShippingRequired"] == require_shipping
@@ -1790,6 +1802,7 @@ def test_product_type_update_mutation(
}) {
productType {
name
+ slug
isShippingRequired
hasVariants
variantAttributes {
@@ -1828,6 +1841,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["hasVariants"] == has_variants
assert data["isShippingRequired"] == require_shipping
assert not data["productAttributes"]
@@ -2628,13 +2642,13 @@ def test_categories_query_with_sort(
)
Category.objects.create(
name="SubCat",
- slug="slug_subcategory",
+ slug="slug_subcategory1",
parent=Category.objects.get(name="Cat1"),
description="Subcategory_description of cat1",
)
subsubcat = Category.objects.create(
name="SubSubCat",
- slug="slug_subcategory",
+ slug="slug_subcategory2",
parent=Category.objects.get(name="SubCat"),
description="Subcategory_description of cat1",
)
@@ -2683,18 +2697,21 @@ def test_product_type_query_with_filter(
[
ProductType(
name="Digital Type",
+ slug="digital-type",
has_variants=True,
is_shipping_required=False,
is_digital=True,
),
ProductType(
name="Tools",
+ slug="tools",
has_variants=True,
is_shipping_required=True,
is_digital=False,
),
ProductType(
name="Books",
+ slug="books",
has_variants=False,
is_shipping_required=True,
is_digital=False,
@@ -2756,18 +2773,21 @@ def test_product_type_query_with_sort(
[
ProductType(
name="Digital",
+ slug="digital",
has_variants=True,
is_shipping_required=False,
is_digital=True,
),
ProductType(
name="Tools",
+ slug="tools",
has_variants=True,
is_shipping_required=True,
is_digital=False,
),
ProductType(
name="Subscription",
+ slug="subscription",
has_variants=False,
is_shipping_required=False,
is_digital=False,
@@ -2893,7 +2913,10 @@ def test_product_type_get_unassigned_attributes(
):
query = QUERY_AVAILABLE_ATTRIBUTES
target_product_type, ignored_product_type = ProductType.objects.bulk_create(
- [ProductType(name="Type 1"), ProductType(name="Type 2")]
+ [
+ ProductType(name="Type 1", slug="type-1"),
+ ProductType(name="Type 2", slug="type-2"),
+ ]
)
unassigned_attributes = list(
@@ -2998,9 +3021,9 @@ def test_filter_product_types_by_custom_search_value(
ProductType.objects.bulk_create(
[
- ProductType(name="The best juices"),
- ProductType(name="The best beers"),
- ProductType(name="The worst beers"),
+ ProductType(name="The best juices", slug="best-juices"),
+ ProductType(name="The best beers", slug="best-beers"),
+ ProductType(name="The worst beers", slug="worst-beers"),
]
)
diff --git a/tests/api/test_product_sorting_attributes.py b/tests/api/test_product_sorting_attributes.py
index 54e97b411a..5a052d249d 100644
--- a/tests/api/test_product_sorting_attributes.py
+++ b/tests/api/test_product_sorting_attributes.py
@@ -55,9 +55,15 @@ def products_structures(category):
pt_apples, pt_oranges, pt_other = list(
product_models.ProductType.objects.bulk_create(
[
- product_models.ProductType(name="Apples", has_variants=False),
- product_models.ProductType(name="Oranges", has_variants=False),
- product_models.ProductType(name="Other attributes", has_variants=False),
+ product_models.ProductType(
+ name="Apples", slug="apples", has_variants=False
+ ),
+ product_models.ProductType(
+ name="Oranges", slug="oranges", has_variants=False
+ ),
+ product_models.ProductType(
+ name="Other attributes", slug="other", has_variants=False
+ ),
]
)
)
@@ -469,8 +475,12 @@ def test_sort_product_not_having_attribute_data(api_client, category, count_quer
}
# Create two product types, with one forced to be at the bottom (no such attribute)
- product_type = product_models.ProductType.objects.create(name="Apples")
- other_product_type = product_models.ProductType.objects.create(name="Chocolates")
+ product_type = product_models.ProductType.objects.create(
+ name="Apples", slug="apples"
+ )
+ other_product_type = product_models.ProductType.objects.create(
+ name="Chocolates", slug="chocolates"
+ )
# Assign an attribute to the product type
attribute = product_models.Attribute.objects.create(name="Kind", slug="kind")
diff --git a/tests/api/test_warehouse.py b/tests/api/test_warehouse.py
index 74d6fa9b72..5996608e03 100644
--- a/tests/api/test_warehouse.py
+++ b/tests/api/test_warehouse.py
@@ -94,6 +94,7 @@ mutation createWarehouse($input: WarehouseCreateInput!) {
warehouse {
id
name
+ slug
companyName
address {
id
@@ -113,6 +114,7 @@ mutation updateWarehouse($input: WarehouseUpdateInput!, $id: ID!) {
}
warehouse {
name
+ slug
companyName
address {
id
@@ -252,6 +254,7 @@ def test_mutation_create_warehouse_requires_permission(staff_api_client):
variables = {
"input": {
"name": "Test warehouse",
+ "slug": "test-warhouse",
"companyName": "Amazing Company Inc",
"email": "[email protected]",
"address": {
@@ -279,6 +282,7 @@ def test_mutation_create_warehouse(
variables = {
"input": {
"name": "Test warehouse",
+ "slug": "test-warhouse",
"companyName": "Amazing Company Inc",
"email": "[email protected]",
"address": {
@@ -304,6 +308,7 @@ def test_mutation_create_warehouse(
"Warehouse", warehouse.id
)
assert created_warehouse["name"] == warehouse.name
+ assert created_warehouse["slug"] == warehouse.slug
def test_create_warehouse_creates_address(
@@ -375,6 +380,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.company_name == "New name for company"
assert warehouse.shipping_zones.count() == 0
diff --git a/tests/conftest.py b/tests/conftest.py
index 7de30ceddb..445bf87689 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -575,7 +575,10 @@ def permission_manage_service_accounts():
@pytest.fixture
def product_type(color_attribute, size_attribute):
product_type = ProductType.objects.create(
- name="Default Type", has_variants=True, is_shipping_required=True
+ name="Default Type",
+ slug="default-type",
+ has_variants=True,
+ is_shipping_required=True,
)
product_type.product_attributes.add(color_attribute)
product_type.variant_attributes.add(size_attribute)
@@ -585,7 +588,7 @@ def product_type(color_attribute, size_attribute):
@pytest.fixture
def product_type_without_variant():
product_type = ProductType.objects.create(
- name="Type", has_variants=False, is_shipping_required=True
+ name="Type", slug="type", has_variants=False, is_shipping_required=True
)
return product_type
@@ -675,7 +678,10 @@ def product_with_variant_with_two_attributes(
color_attribute, size_attribute, category, warehouse
):
product_type = ProductType.objects.create(
- name="Type with two variants", has_variants=True, is_shipping_required=True
+ name="Type with two variants",
+ slug="two-variants",
+ has_variants=True,
+ is_shipping_required=True,
)
product_type.variant_attributes.add(color_attribute)
product_type.variant_attributes.add(size_attribute)
@@ -766,7 +772,10 @@ def product_variant_list(product):
@pytest.fixture
def product_without_shipping(category, warehouse):
product_type = ProductType.objects.create(
- name="Type with no shipping", has_variants=False, is_shipping_required=False
+ name="Type with no shipping",
+ slug="no-shipping",
+ has_variants=False,
+ is_shipping_required=False,
)
product = Product.objects.create(
name="Test product",
@@ -1586,6 +1595,7 @@ def payment_dummy(db, order_with_lines):
def digital_content(category, media_root, warehouse) -> DigitalContent:
product_type = ProductType.objects.create(
name="Digital Type",
+ slug="digital-type",
has_variants=True,
is_shipping_required=False,
is_digital=True,
@@ -1827,7 +1837,10 @@ def customer_wishlist_item_with_two_variants(
@pytest.fixture
def warehouse(address, shipping_zone):
warehouse = Warehouse.objects.create(
- address=address, name="Example Warehouse", email="[email protected]"
+ address=address,
+ name="Example Warehouse",
+ slug="example=warehouse",
)
warehouse.shipping_zones.add(shipping_zone)
warehouse.save()
diff --git a/tests/test_product.py b/tests/test_product.py
index 5bef5af653..17e8e0b095 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -18,11 +18,11 @@ from saleor.product.utils.digital_products import increment_download_count
def test_filtering_by_attribute(db, color_attribute, category, settings):
product_type_a = models.ProductType.objects.create(
- name="New class", has_variants=True
+ name="New class", slug="new-class1", has_variants=True
)
product_type_a.product_attributes.add(color_attribute)
product_type_b = models.ProductType.objects.create(
- name="New class", has_variants=True
+ name="New class", slug="new-class2", has_variants=True
)
product_type_b.variant_attributes.add(color_attribute)
product_a = models.Product.objects.create(