Skip to content

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

Allow assigning category and collection as reference (#17903)

Iga Karbowiak223f354da4f2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58d2b76a97..8df9596d95 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -78,6 +78,7 @@ All notable, unreleased changes to this project will be documented in this file.
- `category.products`
- `collection.products`
- `pageType.availableAttributes`
+- Extend `AttributeEntityType` with `CATEGORY` and `COLLECTION`. You can now assign category and collection as a attribute reference.
### Webhooks
diff --git a/saleor/attribute/__init__.py b/saleor/attribute/__init__.py
index 4dca2edc1b..d5e85f36f9 100644
--- a/saleor/attribute/__init__.py
+++ b/saleor/attribute/__init__.py
@@ -109,9 +109,13 @@ class AttributeEntityType:
PAGE = "Page"
PRODUCT = "Product"
PRODUCT_VARIANT = "ProductVariant"
+ CATEGORY = "Category"
+ COLLECTION = "Collection"
CHOICES = [
(PAGE, "Page"),
(PRODUCT, "Product"),
(PRODUCT_VARIANT, "Product Variant"),
+ (CATEGORY, "Category"),
+ (COLLECTION, "Collection"),
]
diff --git a/saleor/attribute/migrations/0049_attributevalue_references_attr_entity_type.py b/saleor/attribute/migrations/0049_attributevalue_references_attr_entity_type.py
new file mode 100644
index 0000000000..717dfa3e83
--- /dev/null
+++ b/saleor/attribute/migrations/0049_attributevalue_references_attr_entity_type.py
@@ -0,0 +1,52 @@
+# Generated by Django 5.2.1 on 2025-06-30 09:25
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("attribute", "0048_alter_attribute_metadata_and_more"),
+ ("product", "0200_merge_20250527_1210"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="attributevalue",
+ name="reference_category",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="references",
+ to="product.category",
+ ),
+ ),
+ migrations.AddField(
+ model_name="attributevalue",
+ name="reference_collection",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="references",
+ to="product.collection",
+ ),
+ ),
+ migrations.AlterField(
+ model_name="attribute",
+ name="entity_type",
+ field=models.CharField(
+ blank=True,
+ choices=[
+ ("Page", "Page"),
+ ("Product", "Product"),
+ ("ProductVariant", "Product Variant"),
+ ("Category", "Category"),
+ ("Collection", "Collection"),
+ ],
+ max_length=50,
+ null=True,
+ ),
+ ),
+ ]
diff --git a/saleor/attribute/models/base.py b/saleor/attribute/models/base.py
index d1aa54e840..574b5ece25 100644
--- a/saleor/attribute/models/base.py
+++ b/saleor/attribute/models/base.py
@@ -12,7 +12,7 @@ from ...core.utils.translations import Translation
from ...page.models import Page, PageType
from ...permission.enums import PageTypePermissions, ProductTypePermissions
from ...permission.utils import has_one_of_permissions
-from ...product.models import Product, ProductType, ProductVariant
+from ...product.models import Category, Collection, Product, ProductType, ProductVariant
from .. import AttributeEntityType, AttributeInputType, AttributeType
if TYPE_CHECKING:
@@ -372,9 +372,26 @@ class AttributeValue(ModelWithExternalReference):
blank=True,
)
+ reference_collection = models.ForeignKey(
+ Collection,
+ related_name="references",
+ on_delete=models.CASCADE,
+ null=True,
+ blank=True,
+ )
+
+ reference_category = models.ForeignKey(
+ Category,
+ related_name="references",
+ on_delete=models.CASCADE,
+ null=True,
+ blank=True,
+ )
+
reference_page = models.ForeignKey(
Page, related_name="references", on_delete=models.CASCADE, null=True, blank=True
)
+
sort_order = models.IntegerField(editable=False, db_index=True, null=True)
objects = AttributeValueManager()
diff --git a/saleor/attribute/tests/fixtures/attribute.py b/saleor/attribute/tests/fixtures/attribute.py
index 4dcc2738f9..061faf102b 100644
--- a/saleor/attribute/tests/fixtures/attribute.py
+++ b/saleor/attribute/tests/fixtures/attribute.py
@@ -636,6 +636,50 @@ def page_type_variant_reference_attribute(db):
)
+def product_type_category_reference_attribute(db):
+ return Attribute.objects.create(
+ slug="category-reference",
+ name="Category reference",
+ type=AttributeType.PRODUCT_TYPE,
+ input_type=AttributeInputType.REFERENCE,
+ entity_type=AttributeEntityType.CATEGORY,
+ )
+
+
+def page_type_category_reference_attribute(db):
+ return Attribute.objects.create(
+ slug="category-reference",
+ name="Category reference",
+ type=AttributeType.PAGE_TYPE,
+ input_type=AttributeInputType.REFERENCE,
+ entity_type=AttributeEntityType.CATEGORY,
+ )
+
+
+def product_type_collection_reference_attribute(db):
+ return Attribute.objects.create(
+ slug="collection-reference",
+ name="Collection reference",
+ type=AttributeType.PRODUCT_TYPE,
+ input_type=AttributeInputType.REFERENCE,
+ entity_type=AttributeEntityType.COLLECTION,
+ )
+
+
+def page_type_collection_reference_attribute(db):
+ return Attribute.objects.create(
+ slug="collection-reference",
+ name="Collection reference",
+ type=AttributeType.PAGE_TYPE,
+ input_type=AttributeInputType.REFERENCE,
+ entity_type=AttributeEntityType.COLLECTION,
+ )
+
+
@pytest.fixture
def size_page_attribute(db):
attribute = Attribute.objects.create(
diff --git a/saleor/csv/tests/export/products_data/test_get_products_data.py b/saleor/csv/tests/export/products_data/test_get_products_data.py
index d267d25bba..00040270f1 100644
--- a/saleor/csv/tests/export/products_data/test_get_products_data.py
+++ b/saleor/csv/tests/export/products_data/test_get_products_data.py
@@ -251,6 +251,8 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
product_type_page_reference_attribute,
product_type_product_reference_attribute,
product_type_variant_reference_attribute,
+ product_type_category_reference_attribute,
+ product_type_collection_reference_attribute,
numeric_attribute,
product_with_image,
product_with_variant_with_two_attributes,
@@ -261,6 +263,8 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
date_time_attribute,
variant_with_many_stocks,
swatch_attribute,
+ category,
+ collection,
):
# given
product.variants.add(variant_with_many_stocks)
@@ -269,6 +273,8 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
product_type_page_reference_attribute,
product_type_product_reference_attribute,
product_type_variant_reference_attribute,
+ product_type_category_reference_attribute,
+ product_type_collection_reference_attribute,
numeric_attribute,
rich_text_attribute,
swatch_attribute,
@@ -281,6 +287,8 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
product_type_page_reference_attribute,
product_type_product_reference_attribute,
product_type_variant_reference_attribute,
+ product_type_category_reference_attribute,
+ product_type_collection_reference_attribute,
numeric_attribute,
rich_text_attribute,
swatch_attribute,
@@ -396,6 +404,58 @@ def test_get_products_data_for_specified_warehouses_channels_and_attributes(
{product_type_variant_reference_attribute.pk: [product_variant_ref_value]},
)
+ # add category reference attribute
+ product_category_ref_value = AttributeValue.objects.create(
+ attribute=product_type_category_reference_attribute,
+ reference_category=category,
+ slug=f"product_{product.pk}_category_{category.pk}",
+ name=category.name,
+ )
+ variant_category_ref_value = AttributeValue.objects.create(
+ attribute=product_type_category_reference_attribute,
+ reference_category=category,
+ slug=f"variant_{variant_with_many_stocks.pk}_category_{category.pk}",
+ name=category.name,
+ )
+ associate_attribute_values_to_instance(
+ variant_with_many_stocks,
+ {product_type_category_reference_attribute.pk: [variant_category_ref_value]},
+ )
+ associate_attribute_values_to_instance(
+ product,
+ {product_type_category_reference_attribute.pk: [product_category_ref_value]},
+ )
+
+ # add collection reference attribute
+ product_collection_ref_value = AttributeValue.objects.create(
+ attribute=product_type_collection_reference_attribute,
+ reference_collection=collection,
+ slug=f"product_{product.pk}_collection_{collection.pk}",
+ name=collection.name,
+ )
+ variant_collection_ref_value = AttributeValue.objects.create(
+ attribute=product_type_collection_reference_attribute,
+ reference_collection=collection,
+ slug=f"variant_{variant_with_many_stocks.pk}_collection_{collection.pk}",
+ name=collection.name,
+ )
+ associate_attribute_values_to_instance(
+ variant_with_many_stocks,
+ {
+ product_type_collection_reference_attribute.pk: [
+ variant_collection_ref_value
+ ]
+ },
+ )
+ associate_attribute_values_to_instance(
+ product,
+ {
+ product_type_collection_reference_attribute.pk: [
+ product_collection_ref_value
+ ]
+ },
+ )
+
# add numeric attribute
numeric_value_1 = numeric_attribute.values.first()
numeric_value_2 = numeric_attribute.values.last()
diff --git a/saleor/csv/tests/export/products_data/test_handle_relations_data.py b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
index 91e0d4647c..364ab72d2f 100644
--- a/saleor/csv/tests/export/products_data/test_handle_relations_data.py
+++ b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
@@ -84,7 +84,11 @@ def test_get_products_relations_data_attribute_ids(
file_attribute,
product_type_page_reference_attribute,
product_type_product_reference_attribute,
+ product_type_collection_reference_attribute,
+ product_type_category_reference_attribute,
page,
+ collection,
+ category,
):
# given
product = product_list[0]
@@ -92,6 +96,8 @@ def test_get_products_relations_data_attribute_ids(
file_attribute,
product_type_page_reference_attribute,
product_type_product_reference_attribute,
+ product_type_collection_reference_attribute,
+ product_type_category_reference_attribute,
)
associate_attribute_values_to_instance(
product,
@@ -115,6 +121,22 @@ def test_get_products_relations_data_attribute_ids(
product,
{product_type_product_reference_attribute.id: [product_ref_value]},
)
+ collection_ref_value = AttributeValue.objects.create(
+ attribute=product_type_collection_reference_attribute,
+ reference_collection=collection,
+ )
+ associate_attribute_values_to_instance(
+ product,
+ {product_type_collection_reference_attribute.id: [collection_ref_value]},
+ )
+ category_ref_value = AttributeValue.objects.create(
+ attribute=product_type_category_reference_attribute,
+ reference_category=category,
+ )
+ associate_attribute_values_to_instance(
+ product,
+ {product_type_category_reference_attribute.id: [category_ref_value]},
+ )
qs = Product.objects.all()
export_fields = {"name", "description"}
diff --git a/saleor/csv/tests/export/products_data/test_prepare_headers.py b/saleor/csv/tests/export/products_data/test_prepare_headers.py
index 1101335877..ba1e357907 100644
--- a/saleor/csv/tests/export/products_data/test_prepare_headers.py
+++ b/saleor/csv/tests/export/products_data/test_prepare_headers.py
@@ -44,7 +44,10 @@ def test_get_export_fields_and_headers_no_fields():
def test_get_attributes_headers(
- product_with_multiple_values_attributes, product_type_without_variant
+ product_with_multiple_values_attributes,
+ product_type_without_variant,
+ product_type_category_reference_attribute,
+ product_type_collection_reference_attribute,
):
# given
attribute_ids = Attribute.objects.values_list("id", flat=True)
@@ -52,7 +55,11 @@ def test_get_attributes_headers(
product_type = product_with_multiple_values_attributes.product_type
product_attribute = product_type.product_attributes.first()
- product_type_without_variant.product_attributes.add(product_attribute)
+ product_type_without_variant.product_attributes.add(
+ product_attribute,
+ product_type_category_reference_attribute,
+ product_type_collection_reference_attribute,
+ )
# when
attributes_headers = get_attributes_headers(export_info)
diff --git a/saleor/csv/utils/__init__.py b/saleor/csv/utils/__init__.py
index fab0e991ae..f175d8e24b 100644
--- a/saleor/csv/utils/__init__.py
+++ b/saleor/csv/utils/__init__.py
@@ -42,6 +42,8 @@ class ProductExportFields:
"reference_page": "attributevalues__value__reference_page",
"reference_product": "attributevalues__value__reference_product",
"reference_variant": "attributevalues__value__reference_variant",
+ "reference_category": "attributevalues__value__reference_category",
+ "reference_collection": "attributevalues__value__reference_collection",
}
PRODUCT_CHANNEL_LISTING_FIELDS = {
@@ -77,6 +79,8 @@ class ProductExportFields:
"reference_page": "values__reference_page",
"reference_product": "values__reference_product",
"reference_variant": "values__reference_variant",
+ "reference_category": "values__reference_category",
+ "reference_collection": "values__reference_collection",
}
VARIANT_CHANNEL_LISTING_FIELDS = {
diff --git a/saleor/csv/utils/products_data.py b/saleor/csv/utils/products_data.py
index 2baedce76d..d5aaa2c06e 100644
--- a/saleor/csv/utils/products_data.py
+++ b/saleor/csv/utils/products_data.py
@@ -369,6 +369,8 @@ class AttributeData:
reference_page: str | None = None
reference_product: str | None = None
reference_variant: str | None = None
+ reference_category: str | None = None
+ reference_collection: str | None = None
def handle_attribute_data(
@@ -514,15 +516,23 @@ def _get_reference_value(attribute_data):
attribute_data.reference_page,
attribute_data.reference_product,
attribute_data.reference_variant,
+ attribute_data.reference_category,
+ attribute_data.reference_collection,
]
):
return None
+
if attribute_data.reference_page:
reference_id = attribute_data.reference_page
elif attribute_data.reference_product:
reference_id = attribute_data.reference_product
- else:
+ elif attribute_data.reference_variant:
reference_id = attribute_data.reference_variant
+ elif attribute_data.reference_category:
+ reference_id = attribute_data.reference_category
+ else:
+ reference_id = attribute_data.reference_collection
+
return f"{attribute_data.entity_type}_{reference_id}"
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index 75a9f3cd6b..f84e2ede74 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -107,6 +107,12 @@ class AttributeAssignmentMixin:
AttributeEntityType.PRODUCT_VARIANT: EntityTypeData(
product_models.ProductVariant, "name", "reference_variant"
),
+ AttributeEntityType.CATEGORY: EntityTypeData(
+ product_models.Category, "name", "reference_category"
+ ),
+ AttributeEntityType.COLLECTION: EntityTypeData(
+ product_models.Collection, "name", "reference_collection"
+ ),
}
@classmethod
diff --git a/saleor/graphql/page/tests/deprecated/test_page_update.py b/saleor/graphql/page/tests/deprecated/test_page_update.py
index 7e359577f0..6545fa0b03 100644
--- a/saleor/graphql/page/tests/deprecated/test_page_update.py
+++ b/saleor/graphql/page/tests/deprecated/test_page_update.py
@@ -92,7 +92,6 @@ def test_page_update_mutation_publication_date_and_published_at_provided(
)
page_id = graphene.Node.to_global_id("Page", page.id)
- # test creating root page
variables = {
"id": page_id,
"input": {
diff --git a/saleor/graphql/page/tests/mutations/test_page_create.py b/saleor/graphql/page/tests/mutations/test_page_create.py
index d0364a1046..9b073038a6 100644
--- a/saleor/graphql/page/tests/mutations/test_page_create.py
+++ b/saleor/graphql/page/tests/mutations/test_page_create.py
@@ -1234,3 +1234,153 @@ def test_create_page_with_variant_reference_attribute(
page_type_variant_reference_attribute.refresh_from_db()
assert page_type_variant_reference_attribute.values.count() == values_count + 1
+
+
+def test_create_page_with_category_reference_attribute(
+ staff_api_client,
+ permission_manage_pages,
+ page_type,
+ page_type_category_reference_attribute,
+ category,
+):
+ # given
+ page_slug = "test-slug"
+ page_content = dummy_editorjs("test content", True)
+ page_title = "test title"
+ page_is_published = True
+ page_type = PageType.objects.create(
+ name="Test page type 2", slug="test-page-type-2"
+ )
+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)
+
+ ref_attribute_id = graphene.Node.to_global_id(
+ "Attribute", page_type_category_reference_attribute.pk
+ )
+ page_type.page_attributes.add(page_type_category_reference_attribute)
+ reference = graphene.Node.to_global_id("Category", category.pk)
+
+ values_count = page_type_category_reference_attribute.values.count()
+
+ variables = {
+ "input": {
+ "title": page_title,
+ "content": page_content,
+ "isPublished": page_is_published,
+ "slug": page_slug,
+ "pageType": page_type_id,
+ "attributes": [{"id": ref_attribute_id, "references": [reference]}],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CREATE_PAGE_MUTATION, variables, permissions=[permission_manage_pages]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageCreate"]
+ errors = data["errors"]
+
+ assert not errors
+ assert data["page"]["title"] == page_title
+ assert data["page"]["content"] == page_content
+ assert data["page"]["slug"] == page_slug
+ assert data["page"]["isPublished"] == page_is_published
+ assert data["page"]["pageType"]["id"] == page_type_id
+ assert len(data["page"]["attributes"]) == 1
+ page_id = data["page"]["id"]
+ _, new_page_pk = graphene.Node.from_global_id(page_id)
+ expected_attr_data = {
+ "attribute": {"slug": page_type_category_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{new_page_pk}_{category.pk}",
+ "file": None,
+ "name": category.name,
+ "reference": reference,
+ "plainText": None,
+ "dateTime": None,
+ "date": None,
+ }
+ ],
+ }
+ assert data["page"]["attributes"][0] == expected_attr_data
+
+ page_type_category_reference_attribute.refresh_from_db()
+ assert page_type_category_reference_attribute.values.count() == values_count + 1
+
+
+def test_create_page_with_collection_reference_attribute(
+ staff_api_client,
+ permission_manage_pages,
+ page_type,
+ page_type_collection_reference_attribute,
+ collection,
+):
+ # given
+ page_slug = "test-slug"
+ page_content = dummy_editorjs("test content", True)
+ page_title = "test title"
+ page_is_published = True
+ page_type = PageType.objects.create(
+ name="Test page type 2", slug="test-page-type-2"
+ )
+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)
+
+ ref_attribute_id = graphene.Node.to_global_id(
+ "Attribute", page_type_collection_reference_attribute.pk
+ )
+ page_type.page_attributes.add(page_type_collection_reference_attribute)
+ reference = graphene.Node.to_global_id("Collection", collection.pk)
+
+ values_count = page_type_collection_reference_attribute.values.count()
+
+ variables = {
+ "input": {
+ "title": page_title,
+ "content": page_content,
+ "isPublished": page_is_published,
+ "slug": page_slug,
+ "pageType": page_type_id,
+ "attributes": [{"id": ref_attribute_id, "references": [reference]}],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CREATE_PAGE_MUTATION, variables, permissions=[permission_manage_pages]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageCreate"]
+ errors = data["errors"]
+
+ assert not errors
+ assert data["page"]["title"] == page_title
+ assert data["page"]["content"] == page_content
+ assert data["page"]["slug"] == page_slug
+ assert data["page"]["isPublished"] == page_is_published
+ assert data["page"]["pageType"]["id"] == page_type_id
+ assert len(data["page"]["attributes"]) == 1
+ page_id = data["page"]["id"]
+ _, new_page_pk = graphene.Node.from_global_id(page_id)
+ expected_attr_data = {
+ "attribute": {"slug": page_type_collection_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{new_page_pk}_{collection.pk}",
+ "file": None,
+ "name": collection.name,
+ "reference": reference,
+ "plainText": None,
+ "dateTime": None,
+ "date": None,
+ }
+ ],
+ }
+ assert data["page"]["attributes"][0] == expected_attr_data
+
+ page_type_collection_reference_attribute.refresh_from_db()
+ assert page_type_collection_reference_attribute.values.count() == values_count + 1
diff --git a/saleor/graphql/page/tests/mutations/test_page_update.py b/saleor/graphql/page/tests/mutations/test_page_update.py
index a26b6c8b62..20f30ab059 100644
--- a/saleor/graphql/page/tests/mutations/test_page_update.py
+++ b/saleor/graphql/page/tests/mutations/test_page_update.py
@@ -929,6 +929,112 @@ def test_update_page_with_variant_reference_attribute_existing_value(
assert page_type_variant_reference_attribute.values.count() == values_count
+def test_update_page_with_category_reference_attribute_new_value(
+ staff_api_client,
+ permission_manage_pages,
+ page,
+ page_type_category_reference_attribute,
+ category,
+):
+ # given
+ query = UPDATE_PAGE_MUTATION
+
+ page_type = page.page_type
+ page_type.page_attributes.add(page_type_category_reference_attribute)
+ values_count = page_type_category_reference_attribute.values.count()
+ ref_attribute_id = graphene.Node.to_global_id(
+ "Attribute", page_type_category_reference_attribute.pk
+ )
+ reference = graphene.Node.to_global_id("Category", category.pk)
+ page_id = graphene.Node.to_global_id("Page", page.id)
+
+ variables = {
+ "id": page_id,
+ "input": {"attributes": [{"id": ref_attribute_id, "references": [reference]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_pages]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageUpdate"]
+
+ assert not data["errors"]
+ assert data["page"]
+ updated_attribute = {
+ "attribute": {"slug": page_type_category_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{page.pk}_{category.pk}",
+ "name": category.name,
+ "file": None,
+ "plainText": None,
+ "reference": reference,
+ }
+ ],
+ }
+ assert updated_attribute in data["page"]["attributes"]
+
+ page_type_category_reference_attribute.refresh_from_db()
+ assert page_type_category_reference_attribute.values.count() == values_count + 1
+
+
+def test_update_page_with_collection_reference_attribute_new_value(
+ staff_api_client,
+ permission_manage_pages,
+ page,
+ page_type_collection_reference_attribute,
+ collection,
+):
+ # given
+ query = UPDATE_PAGE_MUTATION
+
+ page_type = page.page_type
+ page_type.page_attributes.add(page_type_collection_reference_attribute)
+ values_count = page_type_collection_reference_attribute.values.count()
+ ref_attribute_id = graphene.Node.to_global_id(
+ "Attribute", page_type_collection_reference_attribute.pk
+ )
+ reference = graphene.Node.to_global_id("Collection", collection.pk)
+ page_id = graphene.Node.to_global_id("Page", page.id)
+
+ variables = {
+ "id": page_id,
+ "input": {"attributes": [{"id": ref_attribute_id, "references": [reference]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_pages]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageUpdate"]
+
+ assert not data["errors"]
+ assert data["page"]
+ updated_attribute = {
+ "attribute": {"slug": page_type_collection_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{page.pk}_{collection.pk}",
+ "name": collection.name,
+ "file": None,
+ "plainText": None,
+ "reference": reference,
+ }
+ ],
+ }
+ assert updated_attribute in data["page"]["attributes"]
+
+ page_type_collection_reference_attribute.refresh_from_db()
+ assert page_type_collection_reference_attribute.values.count() == values_count + 1
+
+
@freeze_time("2020-03-18 12:00:00")
def test_public_page_sets_publication_date(
staff_api_client, permission_manage_pages, page_type
diff --git a/saleor/graphql/product/tests/mutations/test_category_delete.py b/saleor/graphql/product/tests/mutations/test_category_delete.py
index f443a21bb5..1852c231aa 100644
--- a/saleor/graphql/product/tests/mutations/test_category_delete.py
+++ b/saleor/graphql/product/tests/mutations/test_category_delete.py
@@ -7,6 +7,8 @@ from django.core.files import File
from django.utils.functional import SimpleLazyObject
from freezegun import freeze_time
+from .....attribute.models import AttributeValue
+from .....attribute.utils import associate_attribute_values_to_instance
from .....core.utils.json_serializer import CustomJsonEncoder
from .....discount.utils.promotion import get_active_catalogue_promotion_rules
from .....product.models import Category, ProductChannelListing
@@ -208,3 +210,130 @@ def test_category_delete_mutation_for_children_from_categories_tree(
for product_channel_listing in product_channel_listings:
assert product_channel_listing.is_published is False
assert not product_channel_listing.published_at
+
+
+def test_category_delete_removes_reference_to_product(
+ staff_api_client,
+ category,
+ product_type_product_reference_attribute,
+ product_type,
+ product,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_CATEGORY_DELETE
+
+ product_type.product_attributes.add(product_type_product_reference_attribute)
+ attr_value = AttributeValue.objects.create(
+ attribute=product_type_product_reference_attribute,
+ name=category.name,
+ slug=f"{product.pk}_{category.pk}",
+ reference_category=category,
+ )
+ associate_attribute_values_to_instance(
+ product, {product_type_product_reference_attribute.pk: [attr_value]}
+ )
+ reference_id = graphene.Node.to_global_id("Category", category.pk)
+
+ variables = {"id": reference_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["categoryDelete"]
+
+ with pytest.raises(attr_value._meta.model.DoesNotExist):
+ attr_value.refresh_from_db()
+ with pytest.raises(category._meta.model.DoesNotExist):
+ category.refresh_from_db()
+
+ assert not data["errors"]
+
+
+def test_category_delete_removes_reference_to_product_variant(
+ staff_api_client,
+ category,
+ product_type_product_reference_attribute,
+ product_type,
+ product_list,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_CATEGORY_DELETE
+
+ variant = product_list[0].variants.first()
+ product_type.variant_attributes.set([product_type_product_reference_attribute])
+ attr_value = AttributeValue.objects.create(
+ attribute=product_type_product_reference_attribute,
+ name=category.name,
+ slug=f"{variant.pk}_{category.pk}",
+ reference_category=category,
+ )
+ associate_attribute_values_to_instance(
+ variant, {product_type_product_reference_attribute.pk: [attr_value]}
+ )
+ reference_id = graphene.Node.to_global_id("Category", category.pk)
+
+ variables = {"id": reference_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["categoryDelete"]
+
+ with pytest.raises(attr_value._meta.model.DoesNotExist):
+ attr_value.refresh_from_db()
+ with pytest.raises(category._meta.model.DoesNotExist):
+ category.refresh_from_db()
+
+ assert not data["errors"]
+
+
+def test_category_delete_removes_reference_to_page(
+ staff_api_client,
+ category,
+ page,
+ page_type_product_reference_attribute,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_CATEGORY_DELETE
+
+ page_type = page.page_type
+ page_type.page_attributes.add(page_type_product_reference_attribute)
+ attr_value = AttributeValue.objects.create(
+ attribute=page_type_product_reference_attribute,
+ name=page.title,
+ slug=f"{page.pk}_{category.pk}",
+ reference_category=category,
+ )
+ associate_attribute_values_to_instance(
+ page, {page_type_product_reference_attribute.pk: [attr_value]}
+ )
+ reference_id = graphene.Node.to_global_id("Category", category.pk)
+
+ variables = {"id": reference_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["categoryDelete"]
+
+ with pytest.raises(attr_value._meta.model.DoesNotExist):
+ attr_value.refresh_from_db()
+ with pytest.raises(category._meta.model.DoesNotExist):
+ category.refresh_from_db()
+
+ assert not data["errors"]
diff --git a/saleor/graphql/product/tests/mutations/test_category_update.py b/saleor/graphql/product/tests/mutations/test_category_update.py
index bf9adb36b7..5ad3ecb687 100644
--- a/saleor/graphql/product/tests/mutations/test_category_update.py
+++ b/saleor/graphql/product/tests/mutations/test_category_update.py
@@ -8,7 +8,6 @@ from django.utils import timezone
from django.utils.functional import SimpleLazyObject
from django.utils.text import slugify
from freezegun import freeze_time
-from graphql_relay import to_global_id
from .....core.utils.json_serializer import CustomJsonEncoder
from .....product.error_codes import ProductErrorCode
@@ -343,7 +342,7 @@ def test_category_update_mutation_invalid_background_image_content_type(
variables = {
"name": "new-name",
"slug": "new-slug",
- "id": to_global_id("Category", category.id),
+ "id": graphene.Node.to_global_id("Category", category.id),
"backgroundImage": image_name,
"backgroundImageAlt": image_alt,
"isPublished": True,
@@ -397,7 +396,7 @@ def test_category_update_mutation_invalid_background_image(
variables = {
"name": "new-name",
"slug": "new-slug",
- "id": to_global_id("Category", category.id),
+ "id": graphene.Node.to_global_id("Category", category.id),
"backgroundImage": image_name,
"backgroundImageAlt": image_alt,
"isPublished": True,
@@ -635,7 +634,7 @@ def test_update_category_mutation_remove_background_image(
"""
assert category_with_image.background_image
variables = {
- "id": to_global_id("Category", category_with_image.id),
+ "id": graphene.Node.to_global_id("Category", category_with_image.id),
"backgroundImage": None,
}
response = staff_api_client.post_graphql(
diff --git a/saleor/graphql/product/tests/mutations/test_collection_add_products.py b/saleor/graphql/product/tests/mutations/test_collection_add_products.py
index 529e7edcef..2f2bcd4f52 100644
--- a/saleor/graphql/product/tests/mutations/test_collection_add_products.py
+++ b/saleor/graphql/product/tests/mutations/test_collection_add_products.py
@@ -1,6 +1,6 @@
from unittest.mock import patch
-from graphql_relay import to_global_id
+import graphene
from .....discount.utils.promotion import get_active_catalogue_promotion_rules
from .....product.error_codes import CollectionErrorCode
@@ -36,8 +36,10 @@ def test_add_products_to_collection(
# given
query = COLLECTION_ADD_PRODUCTS_MUTATION
- collection_id = to_global_id("Collection", collection.id)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
products_before = collection.products.count()
variables = {"id": collection_id, "products": product_ids}
@@ -63,8 +65,10 @@ def test_add_products_to_collection_trigger_product_updated_webhook(
permission_manage_products,
):
query = COLLECTION_ADD_PRODUCTS_MUTATION
- collection_id = to_global_id("Collection", collection.id)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
products_before = collection.products.count()
variables = {"id": collection_id, "products": product_ids}
response = staff_api_client.post_graphql(
@@ -80,8 +84,10 @@ def test_add_products_to_collection_on_sale_trigger_discounted_price_recalculati
staff_api_client, collection, product_list, permission_manage_products
):
query = COLLECTION_ADD_PRODUCTS_MUTATION
- collection_id = to_global_id("Collection", collection.id)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
products_before = collection.products.count()
variables = {"id": collection_id, "products": product_ids}
response = staff_api_client.post_graphql(
@@ -97,8 +103,10 @@ def test_add_products_to_collection_with_product_without_variants(
):
query = COLLECTION_ADD_PRODUCTS_MUTATION
product_list[0].variants.all().delete()
- collection_id = to_global_id("Collection", collection.id)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
variables = {"id": collection_id, "products": product_ids}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
diff --git a/saleor/graphql/product/tests/mutations/test_collection_create.py b/saleor/graphql/product/tests/mutations/test_collection_create.py
index 93dcedfafb..5a2933f076 100644
--- a/saleor/graphql/product/tests/mutations/test_collection_create.py
+++ b/saleor/graphql/product/tests/mutations/test_collection_create.py
@@ -1,8 +1,8 @@
import os
from unittest.mock import patch
+import graphene
import pytest
-from graphql_relay import to_global_id
from .....product.models import Collection
from .....product.tests.utils import create_image
@@ -72,7 +72,9 @@ def test_create_collection(
# given
staff_api_client.user.user_permissions.add(permission_manage_products)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
image_file, image_name = create_image()
image_alt = "Alt text for an image."
name = "test-name"
@@ -130,7 +132,9 @@ def test_create_collection_trigger_product_update_webhook(
):
query = CREATE_COLLECTION_MUTATION
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
name = "test-name"
slug = "test-slug"
description = dummy_editorjs("description", True)
diff --git a/saleor/graphql/product/tests/mutations/test_collection_delete.py b/saleor/graphql/product/tests/mutations/test_collection_delete.py
index a4a0d1fcfc..9b774aed20 100644
--- a/saleor/graphql/product/tests/mutations/test_collection_delete.py
+++ b/saleor/graphql/product/tests/mutations/test_collection_delete.py
@@ -1,9 +1,11 @@
from unittest.mock import MagicMock, patch
+import graphene
import pytest
from django.core.files import File
-from graphql_relay import to_global_id
+from .....attribute.models import AttributeValue
+from .....attribute.utils import associate_attribute_values_to_instance
from .....discount.utils.promotion import get_active_catalogue_promotion_rules
from .....thumbnail.models import Thumbnail
from ....tests.utils import (
@@ -16,6 +18,11 @@ DELETE_COLLECTION_MUTATION = """
collection {
name
}
+ errors {
+ field
+ message
+ code
+ }
}
}
"""
@@ -32,7 +39,7 @@ def test_delete_collection(
# given
query = DELETE_COLLECTION_MUTATION
collection.products.set(product_list)
- collection_id = to_global_id("Collection", collection.id)
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
variables = {"id": collection_id}
# when
@@ -69,7 +76,7 @@ def test_delete_collection_with_background_image(
Thumbnail.objects.create(collection=collection, size=200, image=thumbnail_mock)
collection_id = collection.id
- variables = {"id": to_global_id("Collection", collection.id)}
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.id)}
# when
response = staff_api_client.post_graphql(
@@ -97,7 +104,7 @@ def test_delete_collection_trigger_product_updated_webhook(
):
query = DELETE_COLLECTION_MUTATION
collection.products.add(*product_list)
- collection_id = to_global_id("Collection", collection.id)
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
variables = {"id": collection_id}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
@@ -108,3 +115,130 @@ def test_delete_collection_trigger_product_updated_webhook(
with pytest.raises(collection._meta.model.DoesNotExist):
collection.refresh_from_db()
assert len(product_list) == product_updated_mock.call_count
+
+
+def test_collection_delete_removes_reference_to_product(
+ staff_api_client,
+ collection,
+ product_type_product_reference_attribute,
+ product_type,
+ product,
+ permission_manage_products,
+):
+ # given
+ query = DELETE_COLLECTION_MUTATION
+
+ product_type.product_attributes.add(product_type_product_reference_attribute)
+ attr_value = AttributeValue.objects.create(
+ attribute=product_type_product_reference_attribute,
+ name=collection.name,
+ slug=f"{product.pk}_{collection.pk}",
+ reference_collection=collection,
+ )
+ associate_attribute_values_to_instance(
+ product, {product_type_product_reference_attribute.pk: [attr_value]}
+ )
+ reference_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ variables = {"id": reference_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["collectionDelete"]
+
+ with pytest.raises(attr_value._meta.model.DoesNotExist):
+ attr_value.refresh_from_db()
+ with pytest.raises(collection._meta.model.DoesNotExist):
+ collection.refresh_from_db()
+
+ assert not data["errors"]
+
+
+def test_collection_delete_removes_reference_to_product_variant(
+ staff_api_client,
+ collection,
+ product_type_product_reference_attribute,
+ product_type,
+ product_list,
+ permission_manage_products,
+):
+ # given
+ query = DELETE_COLLECTION_MUTATION
+
+ variant = product_list[0].variants.first()
+ product_type.variant_attributes.set([product_type_product_reference_attribute])
+ attr_value = AttributeValue.objects.create(
+ attribute=product_type_product_reference_attribute,
+ name=collection.name,
+ slug=f"{variant.pk}_{collection.pk}",
+ reference_collection=collection,
+ )
+ associate_attribute_values_to_instance(
+ variant, {product_type_product_reference_attribute.pk: [attr_value]}
+ )
+ reference_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ variables = {"id": reference_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["collectionDelete"]
+
+ with pytest.raises(attr_value._meta.model.DoesNotExist):
+ attr_value.refresh_from_db()
+ with pytest.raises(collection._meta.model.DoesNotExist):
+ collection.refresh_from_db()
+
+ assert not data["errors"]
+
+
+def test_collection_delete_removes_reference_to_page(
+ staff_api_client,
+ collection,
+ page,
+ page_type_product_reference_attribute,
+ permission_manage_products,
+):
+ # given
+ query = DELETE_COLLECTION_MUTATION
+
+ page_type = page.page_type
+ page_type.page_attributes.add(page_type_product_reference_attribute)
+ attr_value = AttributeValue.objects.create(
+ attribute=page_type_product_reference_attribute,
+ name=page.title,
+ slug=f"{page.pk}_{collection.pk}",
+ reference_collection=collection,
+ )
+ associate_attribute_values_to_instance(
+ page, {page_type_product_reference_attribute.pk: [attr_value]}
+ )
+ reference_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ variables = {"id": reference_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["collectionDelete"]
+
+ with pytest.raises(attr_value._meta.model.DoesNotExist):
+ attr_value.refresh_from_db()
+ with pytest.raises(collection._meta.model.DoesNotExist):
+ collection.refresh_from_db()
+
+ assert not data["errors"]
diff --git a/saleor/graphql/product/tests/mutations/test_collection_remove_products.py b/saleor/graphql/product/tests/mutations/test_collection_remove_products.py
index bcfcb2fbc6..4e3ffbce31 100644
--- a/saleor/graphql/product/tests/mutations/test_collection_remove_products.py
+++ b/saleor/graphql/product/tests/mutations/test_collection_remove_products.py
@@ -1,6 +1,6 @@
from unittest.mock import patch
-from graphql_relay import to_global_id
+import graphene
from .....discount.utils.promotion import get_active_catalogue_promotion_rules
from ....tests.utils import (
@@ -30,8 +30,10 @@ def test_remove_products_from_collection(
# given
query = COLLECTION_REMOVE_PRODUCTS_MUTATION
collection.products.add(*product_list)
- collection_id = to_global_id("Collection", collection.id)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
products_before = collection.products.count()
variables = {"id": collection_id, "products": product_ids}
@@ -58,8 +60,10 @@ def test_remove_products_from_collection_trigger_product_updated_webhook(
):
query = COLLECTION_REMOVE_PRODUCTS_MUTATION
collection.products.add(*product_list)
- collection_id = to_global_id("Collection", collection.id)
- product_ids = [to_global_id("Product", product.pk) for product in product_list]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ product_ids = [
+ graphene.Node.to_global_id("Product", product.pk) for product in product_list
+ ]
products_before = collection.products.count()
variables = {"id": collection_id, "products": product_ids}
response = staff_api_client.post_graphql(
diff --git a/saleor/graphql/product/tests/mutations/test_collection_update.py b/saleor/graphql/product/tests/mutations/test_collection_update.py
index c592fb9466..ff19d7bdd7 100644
--- a/saleor/graphql/product/tests/mutations/test_collection_update.py
+++ b/saleor/graphql/product/tests/mutations/test_collection_update.py
@@ -3,7 +3,6 @@ from unittest.mock import MagicMock, Mock, patch
import graphene
import pytest
from django.core.files import File
-from graphql_relay import to_global_id
from .....product.error_codes import ProductErrorCode
from .....product.models import Collection
@@ -70,7 +69,7 @@ def test_update_collection(
"name": name,
"slug": slug,
"description": description,
- "id": to_global_id("Collection", collection.id),
+ "id": graphene.Node.to_global_id("Collection", collection.id),
"metadata": [{"key": metadata_key, "value": metadata_value}],
"privateMetadata": [{"key": metadata_key, "value": metadata_value}],
}
@@ -134,7 +133,7 @@ def test_update_collection_metadata_marks_prices_to_recalculate(
collection.products.set([product])
variables = {
- "id": to_global_id("Collection", collection.id),
+ "id": graphene.Node.to_global_id("Collection", collection.id),
"metadata": [{"key": metadata_key, "value": metadata_value}],
}
@@ -204,7 +203,7 @@ def test_update_collection_with_background_image(
variables = {
"name": "new-name",
"slug": "new-slug",
- "id": to_global_id("Collection", collection.id),
+ "id": graphene.Node.to_global_id("Collection", collection.id),
"backgroundImage": image_name,
"backgroundImageAlt": image_alt,
}
@@ -254,7 +253,7 @@ def test_update_collection_invalid_background_image_content_type(
variables = {
"name": "new-name",
"slug": "new-slug",
- "id": to_global_id("Collection", collection.id),
+ "id": graphene.Node.to_global_id("Collection", collection.id),
"backgroundImage": image_name,
"backgroundImageAlt": image_alt,
}
@@ -307,7 +306,7 @@ def test_update_collection_invalid_background_image(
variables = {
"name": "new-name",
"slug": "new-slug",
- "id": to_global_id("Collection", collection.id),
+ "id": graphene.Node.to_global_id("Collection", collection.id),
"backgroundImage": image_name,
"backgroundImageAlt": image_alt,
}
@@ -377,8 +376,8 @@ def test_update_collection_slug(
assert old_slug != input_slug
- node_id = graphene.Node.to_global_id("Collection", collection.id)
- variables = {"slug": input_slug, "id": node_id}
+ Node_id = graphene.Node.to_global_id("Collection", collection.id)
+ variables = {"slug": input_slug, "id": Node_id}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
@@ -408,8 +407,8 @@ def test_update_collection_slug_exists(
assert input_slug != collection.slug
- node_id = graphene.Node.to_global_id("Collection", collection.id)
- variables = {"slug": input_slug, "id": node_id}
+ Node_id = graphene.Node.to_global_id("Collection", collection.id)
+ variables = {"slug": input_slug, "id": Node_id}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
@@ -470,8 +469,8 @@ def test_update_collection_slug_and_name(
assert input_slug != old_slug
assert input_name != old_name
- node_id = graphene.Node.to_global_id("Collection", collection.id)
- variables = {"slug": input_slug, "name": input_name, "id": node_id}
+ Node_id = graphene.Node.to_global_id("Collection", collection.id)
+ variables = {"slug": input_slug, "name": input_name, "id": Node_id}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)
@@ -512,7 +511,7 @@ def test_update_collection_mutation_remove_background_image(
"""
assert collection_with_image.background_image
variables = {
- "id": to_global_id("Collection", collection_with_image.id),
+ "id": graphene.Node.to_global_id("Collection", collection_with_image.id),
"backgroundImage": None,
}
response = staff_api_client.post_graphql(
diff --git a/saleor/graphql/product/tests/mutations/test_product_create.py b/saleor/graphql/product/tests/mutations/test_product_create.py
index 5f3f23803d..963135babf 100644
--- a/saleor/graphql/product/tests/mutations/test_product_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_create.py
@@ -1756,6 +1756,157 @@ def test_create_product_with_variant_reference_attribute(
assert product_type_variant_reference_attribute.values.count() == values_count + 1
+def test_create_product_with_category_reference_attribute(
+ staff_api_client,
+ product_type,
+ category,
+ color_attribute,
+ product_type_category_reference_attribute,
+ permission_manage_products,
+):
+ # given
+ query = CREATE_PRODUCT_MUTATION
+
+ values_count = product_type_category_reference_attribute.values.count()
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ # Add second attribute
+ product_type.product_attributes.add(product_type_category_reference_attribute)
+ reference_attr_id = graphene.Node.to_global_id(
+ "Attribute", product_type_category_reference_attribute.id
+ )
+ reference = graphene.Node.to_global_id("Category", category.pk)
+
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": reference_attr_id, "references": [reference]}],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["errors"] == []
+ assert data["product"]["name"] == product_name
+ assert data["product"]["slug"] == product_slug
+ assert data["product"]["productType"]["name"] == product_type.name
+ assert data["product"]["category"]["name"] == category.name
+ _, product_id = graphene.Node.from_global_id(data["product"]["id"])
+ expected_attributes_data = [
+ {"attribute": {"slug": color_attribute.slug}, "values": []},
+ {
+ "attribute": {"slug": product_type_category_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{product_id}_{category.id}",
+ "name": category.name,
+ "file": None,
+ "richText": None,
+ "plainText": None,
+ "boolean": None,
+ "date": None,
+ "dateTime": None,
+ "reference": reference,
+ }
+ ],
+ },
+ ]
+ for attr_data in data["product"]["attributes"]:
+ assert attr_data in expected_attributes_data
+
+ product_type_category_reference_attribute.refresh_from_db()
+ assert product_type_category_reference_attribute.values.count() == values_count + 1
+
+
+def test_create_product_with_collection_reference_attribute(
+ staff_api_client,
+ product_type,
+ category,
+ color_attribute,
+ product_type_collection_reference_attribute,
+ permission_manage_products,
+ collection,
+):
+ # given
+ query = CREATE_PRODUCT_MUTATION
+
+ values_count = product_type_collection_reference_attribute.values.count()
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ # Add second attribute
+ product_type.product_attributes.add(product_type_collection_reference_attribute)
+ reference_attr_id = graphene.Node.to_global_id(
+ "Attribute", product_type_collection_reference_attribute.id
+ )
+ reference = graphene.Node.to_global_id("Collection", collection.pk)
+
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": reference_attr_id, "references": [reference]}],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["errors"] == []
+ assert data["product"]["name"] == product_name
+ assert data["product"]["slug"] == product_slug
+ assert data["product"]["productType"]["name"] == product_type.name
+ assert data["product"]["category"]["name"] == category.name
+ _, product_id = graphene.Node.from_global_id(data["product"]["id"])
+ expected_attributes_data = [
+ {"attribute": {"slug": color_attribute.slug}, "values": []},
+ {
+ "attribute": {"slug": product_type_collection_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{product_id}_{collection.id}",
+ "name": collection.name,
+ "file": None,
+ "richText": None,
+ "plainText": None,
+ "boolean": None,
+ "date": None,
+ "dateTime": None,
+ "reference": reference,
+ }
+ ],
+ },
+ ]
+ for attr_data in data["product"]["attributes"]:
+ assert attr_data in expected_attributes_data
+
+ product_type_collection_reference_attribute.refresh_from_db()
+ assert (
+ product_type_collection_reference_attribute.values.count() == values_count + 1
+ )
+
+
def test_create_product_with_product_reference_attribute_values_saved_in_order(
staff_api_client,
product_type,
diff --git a/saleor/graphql/product/tests/mutations/test_product_update.py b/saleor/graphql/product/tests/mutations/test_product_update.py
index 644e75dd5b..0944f723d5 100644
--- a/saleor/graphql/product/tests/mutations/test_product_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_update.py
@@ -1273,7 +1273,6 @@ def test_update_product_with_page_reference_attribute_existing_value(
query = MUTATION_UPDATE_PRODUCT
product_id = graphene.Node.to_global_id("Product", product.pk)
-
attribute_id = graphene.Node.to_global_id(
"Attribute", product_type_page_reference_attribute.pk
)
@@ -1287,9 +1286,7 @@ def test_update_product_with_page_reference_attribute_existing_value(
associate_attribute_values_to_instance(
product, {product_type_page_reference_attribute.pk: [attr_value]}
)
-
values_count = product_type_page_reference_attribute.values.count()
-
reference = graphene.Node.to_global_id("Page", page.pk)
variables = {
@@ -1349,9 +1346,7 @@ def test_update_product_with_page_reference_attribute_value_not_given(
product_type_page_reference_attribute.value_required = True
product_type_page_reference_attribute.save(update_fields=["value_required"])
-
product_id = graphene.Node.to_global_id("Product", product.pk)
-
attribute_id = graphene.Node.to_global_id(
"Attribute", product_type_page_reference_attribute.pk
)
@@ -1395,14 +1390,11 @@ def test_update_product_with_product_reference_attribute_value(
product = product_list[0]
product_id = graphene.Node.to_global_id("Product", product.pk)
product_ref = product_list[1]
-
attribute_id = graphene.Node.to_global_id(
"Attribute", product_type_product_reference_attribute.pk
)
product_type.product_attributes.add(product_type_product_reference_attribute)
-
values_count = product_type_product_reference_attribute.values.count()
-
reference = graphene.Node.to_global_id("Product", product_ref.pk)
variables = {
@@ -1463,14 +1455,11 @@ def test_update_product_with_variant_reference_attribute_value(
product = product_list[0]
product_id = graphene.Node.to_global_id("Product", product.pk)
variant_ref = product_list[1].variants.first()
-
attribute_id = graphene.Node.to_global_id(
"Attribute", product_type_variant_reference_attribute.pk
)
product_type.product_attributes.add(product_type_variant_reference_attribute)
-
values_count = product_type_variant_reference_attribute.values.count()
-
reference = graphene.Node.to_global_id("ProductVariant", variant_ref.pk)
variables = {
@@ -1509,11 +1498,137 @@ def test_update_product_with_variant_reference_attribute_value(
],
}
assert expected_file_att_data in attributes
+ product_type_variant_reference_attribute.refresh_from_db()
+ assert product_type_variant_reference_attribute.values.count() == values_count + 1
updated_webhook_mock.assert_called_once_with(product)
- product_type_variant_reference_attribute.refresh_from_db()
- assert product_type_variant_reference_attribute.values.count() == values_count + 1
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_category_reference_attribute_value(
+ updated_webhook_mock,
+ staff_api_client,
+ product_type_category_reference_attribute,
+ product,
+ product_type,
+ category,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribute_id = graphene.Node.to_global_id(
+ "Attribute", product_type_category_reference_attribute.pk
+ )
+ product_type.product_attributes.add(product_type_category_reference_attribute)
+ values_count = product_type_category_reference_attribute.values.count()
+ reference = graphene.Node.to_global_id("Category", category.pk)
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "references": [reference]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert data["errors"] == []
+
+ attributes = data["product"]["attributes"]
+
+ assert len(attributes) == 2
+ expected_file_att_data = {
+ "attribute": {
+ "id": attribute_id,
+ "name": product_type_category_reference_attribute.name,
+ },
+ "values": [
+ {
+ "id": ANY,
+ "name": category.name,
+ "slug": f"{product.id}_{category.id}",
+ "file": None,
+ "reference": reference,
+ "boolean": None,
+ "plainText": None,
+ }
+ ],
+ }
+ assert expected_file_att_data in attributes
+ product_type_category_reference_attribute.refresh_from_db()
+ assert product_type_category_reference_attribute.values.count() == values_count + 1
+
+ updated_webhook_mock.assert_called_once_with(product)
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_collection_reference_attribute_value(
+ updated_webhook_mock,
+ staff_api_client,
+ product_type_collection_reference_attribute,
+ product,
+ product_type,
+ collection,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ attribute_id = graphene.Node.to_global_id(
+ "Attribute", product_type_collection_reference_attribute.pk
+ )
+ product_type.product_attributes.add(product_type_collection_reference_attribute)
+ values_count = product_type_collection_reference_attribute.values.count()
+ reference = graphene.Node.to_global_id("Collection", collection.pk)
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "references": [reference]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert data["errors"] == []
+
+ attributes = data["product"]["attributes"]
+ assert len(attributes) == 2
+ expected_file_att_data = {
+ "attribute": {
+ "id": attribute_id,
+ "name": product_type_collection_reference_attribute.name,
+ },
+ "values": [
+ {
+ "id": ANY,
+ "name": collection.name,
+ "slug": f"{product.id}_{collection.id}",
+ "file": None,
+ "reference": reference,
+ "boolean": None,
+ "plainText": None,
+ }
+ ],
+ }
+ assert expected_file_att_data in attributes
+ product_type_collection_reference_attribute.refresh_from_db()
+ assert (
+ product_type_collection_reference_attribute.values.count() == values_count + 1
+ )
+
+ updated_webhook_mock.assert_called_once_with(product)
def test_update_product_with_attribute_without_id_or_external_ref(
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_create.py b/saleor/graphql/product/tests/mutations/test_product_variant_create.py
index 0cdb35ada7..7828b7a237 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_create.py
@@ -1119,6 +1119,210 @@ def test_create_variant_with_variant_reference_attribute_no_references_given(
updated_webhook_mock.assert_not_called()
+@patch("saleor.plugins.manager.PluginsManager.product_variant_created")
+def test_create_variant_with_category_reference_attribute(
+ created_webhook_mock,
+ staff_api_client,
+ product,
+ product_type,
+ product_type_category_reference_attribute,
+ category_list,
+ permission_manage_products,
+ warehouse,
+):
+ # given
+ query = CREATE_VARIANT_MUTATION
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ sku = "1"
+
+ product_type_category_reference_attribute.value_required = True
+ product_type_category_reference_attribute.save(update_fields=["value_required"])
+
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(product_type_category_reference_attribute)
+ ref_attr_id = graphene.Node.to_global_id(
+ "Attribute", product_type_category_reference_attribute.id
+ )
+
+ category_ref_1 = graphene.Node.to_global_id("Category", category_list[0].pk)
+ category_ref_2 = graphene.Node.to_global_id("Category", category_list[1].pk)
+
+ values_count = product_type_category_reference_attribute.values.count()
+
+ stocks = [
+ {
+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ "quantity": 20,
+ }
+ ]
+ variables = {
+ "input": {
+ "product": product_id,
+ "sku": sku,
+ "stocks": stocks,
+ "attributes": [
+ {"id": ref_attr_id, "references": [category_ref_1, category_ref_2]}
+ ],
+ "trackInventory": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)["data"]["productVariantCreate"]
+
+ assert not content["errors"]
+ data = content["productVariant"]
+ assert data["sku"] == sku
+ variant_id = data["id"]
+ _, variant_pk = graphene.Node.from_global_id(variant_id)
+ assert (
+ data["attributes"][0]["attribute"]["slug"]
+ == product_type_category_reference_attribute.slug
+ )
+ expected_values = [
+ {
+ "slug": f"{variant_pk}_{category_list[0].pk}",
+ "file": None,
+ "richText": None,
+ "plainText": None,
+ "reference": category_ref_1,
+ "name": category_list[0].name,
+ "boolean": None,
+ "date": None,
+ "dateTime": None,
+ },
+ {
+ "slug": f"{variant_pk}_{category_list[1].pk}",
+ "file": None,
+ "richText": None,
+ "plainText": None,
+ "reference": category_ref_2,
+ "name": category_list[1].name,
+ "boolean": None,
+ "date": None,
+ "dateTime": None,
+ },
+ ]
+ for value in expected_values:
+ assert value in data["attributes"][0]["values"]
+ assert len(data["stocks"]) == 1
+ assert data["stocks"][0]["quantity"] == stocks[0]["quantity"]
+ assert data["stocks"][0]["warehouse"]["slug"] == warehouse.slug
+
+ product_type_category_reference_attribute.refresh_from_db()
+ assert product_type_category_reference_attribute.values.count() == values_count + 2
+
+ created_webhook_mock.assert_called_once_with(product.variants.last())
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_variant_created")
+def test_create_variant_with_collection_reference_attribute(
+ created_webhook_mock,
+ staff_api_client,
+ product,
+ product_type,
+ product_type_collection_reference_attribute,
+ collection_list,
+ permission_manage_products,
+ warehouse,
+):
+ # given
+ query = CREATE_VARIANT_MUTATION
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ sku = "1"
+
+ product_type_collection_reference_attribute.value_required = True
+ product_type_collection_reference_attribute.save(update_fields=["value_required"])
+
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(product_type_collection_reference_attribute)
+ ref_attr_id = graphene.Node.to_global_id(
+ "Attribute", product_type_collection_reference_attribute.id
+ )
+
+ collection_ref_1 = graphene.Node.to_global_id("Collection", collection_list[0].pk)
+ collection_ref_2 = graphene.Node.to_global_id("Collection", collection_list[1].pk)
+
+ values_count = product_type_collection_reference_attribute.values.count()
+
+ stocks = [
+ {
+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ "quantity": 20,
+ }
+ ]
+ variables = {
+ "input": {
+ "product": product_id,
+ "sku": sku,
+ "stocks": stocks,
+ "attributes": [
+ {"id": ref_attr_id, "references": [collection_ref_1, collection_ref_2]}
+ ],
+ "trackInventory": True,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)["data"]["productVariantCreate"]
+
+ assert not content["errors"]
+ data = content["productVariant"]
+ assert data["sku"] == sku
+ variant_id = data["id"]
+ _, variant_pk = graphene.Node.from_global_id(variant_id)
+ assert (
+ data["attributes"][0]["attribute"]["slug"]
+ == product_type_collection_reference_attribute.slug
+ )
+ expected_values = [
+ {
+ "slug": f"{variant_pk}_{collection_list[0].pk}",
+ "file": None,
+ "richText": None,
+ "plainText": None,
+ "reference": collection_ref_1,
+ "name": collection_list[0].name,
+ "boolean": None,
+ "date": None,
+ "dateTime": None,
+ },
+ {
+ "slug": f"{variant_pk}_{collection_list[1].pk}",
+ "file": None,
+ "richText": None,
+ "plainText": None,
+ "reference": collection_ref_2,
+ "name": collection_list[1].name,
+ "boolean": None,
+ "date": None,
+ "dateTime": None,
+ },
+ ]
+ for value in expected_values:
+ assert value in data["attributes"][0]["values"]
+ assert len(data["stocks"]) == 1
+ assert data["stocks"][0]["quantity"] == stocks[0]["quantity"]
+ assert data["stocks"][0]["warehouse"]["slug"] == warehouse.slug
+
+ product_type_collection_reference_attribute.refresh_from_db()
+ assert (
+ product_type_collection_reference_attribute.values.count() == values_count + 2
+ )
+
+ created_webhook_mock.assert_called_once_with(product.variants.last())
+
+
@patch("saleor.plugins.manager.PluginsManager.product_variant_created")
def test_create_variant_with_numeric_attribute(
created_webhook_mock,
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_update.py
index 2a870b6f40..9294ad1474 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_update.py
@@ -1961,6 +1961,61 @@ def test_update_product_variant_with_variant_reference_attribute(
assert variant_data["attributes"][0]["values"][0]["reference"] == reference
+def test_update_product_variant_with_category_reference_attribute(
+ staff_api_client,
+ product,
+ category,
+ product_type_category_reference_attribute,
+ permission_manage_products,
+):
+ # given
+ variant = product.variants.first()
+ sku = str(uuid4())[:12]
+ assert not variant.sku == sku
+
+ product_type = product.product_type
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(product_type_category_reference_attribute)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ ref_attribute_id = graphene.Node.to_global_id(
+ "Attribute", product_type_category_reference_attribute.pk
+ )
+ reference = graphene.Node.to_global_id("Category", category.pk)
+
+ variables = {
+ "id": variant_id,
+ "sku": sku,
+ "attributes": [{"id": ref_attribute_id, "references": [reference]}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_UPDATE_VARIANT_ATTRIBUTES,
+ variables,
+ permissions=[permission_manage_products],
+ )
+
+ # then
+ content = get_graphql_content(response)
+
+ data = content["data"]["productVariantUpdate"]
+ assert not data["errors"]
+ variant_data = data["productVariant"]
+ assert variant_data
+ assert variant_data["sku"] == sku
+ assert len(variant_data["attributes"]) == 1
+ assert (
+ variant_data["attributes"][0]["attribute"]["slug"]
+ == product_type_category_reference_attribute.slug
+ )
+ assert len(variant_data["attributes"][0]["values"]) == 1
+ assert (
+ variant_data["attributes"][0]["values"][0]["slug"]
+ == f"{variant.pk}_{category.pk}"
+ )
+ assert variant_data["attributes"][0]["values"][0]["reference"] == reference
+
+
def test_update_product_variant_change_attribute_values_ordering(
staff_api_client,
variant,
diff --git a/saleor/graphql/product/tests/queries/test_categories_query_with_filter.py b/saleor/graphql/product/tests/queries/test_categories_query_with_filter.py
index e05da8aa24..9628f2b5bb 100644
--- a/saleor/graphql/product/tests/queries/test_categories_query_with_filter.py
+++ b/saleor/graphql/product/tests/queries/test_categories_query_with_filter.py
@@ -1,7 +1,6 @@
import graphene
import pytest
from freezegun import freeze_time
-from graphql_relay import to_global_id
from .....product.models import (
Category,
@@ -855,7 +854,15 @@ def test_categories_where_by_ids_empty_list(api_client, category_list):
({"search": "cat1"}, 3),
({"search": "Description cat1."}, 2),
({"search": "Subcategory_description"}, 1),
- ({"ids": [to_global_id("Category", 2), to_global_id("Category", 3)]}, 2),
+ (
+ {
+ "ids": [
+ graphene.Node.to_global_id("Category", 2),
+ graphene.Node.to_global_id("Category", 3),
+ ]
+ },
+ 2,
+ ),
],
)
def test_categories_query_with_filter(
diff --git a/saleor/graphql/product/tests/queries/test_collections_query_with_filter_and_sort.py b/saleor/graphql/product/tests/queries/test_collections_query_with_filter_and_sort.py
index e33c630856..ed43a164a5 100644
--- a/saleor/graphql/product/tests/queries/test_collections_query_with_filter_and_sort.py
+++ b/saleor/graphql/product/tests/queries/test_collections_query_with_filter_and_sort.py
@@ -2,7 +2,6 @@ import datetime
import graphene
import pytest
-from graphql_relay import to_global_id
from .....product.models import Collection, CollectionChannelListing
from .....tests.utils import dummy_editorjs
@@ -19,7 +18,15 @@ from ....tests.utils import (
({"published": "HIDDEN"}, 1),
({"search": "-published1"}, 1),
({"search": "Collection3"}, 1),
- ({"ids": [to_global_id("Collection", 2), to_global_id("Collection", 3)]}, 2),
+ (
+ {
+ "ids": [
+ graphene.Node.to_global_id("Collection", 2),
+ graphene.Node.to_global_id("Collection", 3),
+ ]
+ },
+ 2,
+ ),
],
)
def test_collections_query_with_filter(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 342139b4af..a7feb9b4dd 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -6160,6 +6160,8 @@ enum AttributeEntityTypeEnum @doc(category: "Attributes") {
PAGE
PRODUCT
PRODUCT_VARIANT
+ CATEGORY
+ COLLECTION
}
enum AttributeTypeEnum @doc(category: "Attributes") {