Skip to content

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

add collections handling for product bulk create (#15081)

Jakubf3c7b7c80b53
diff --git a/saleor/graphql/product/bulk_mutations/product_bulk_create.py b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
index 0f49b69059..f9b772e57f 100644
--- a/saleor/graphql/product/bulk_mutations/product_bulk_create.py
+++ b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
@@ -18,6 +18,7 @@ from ....core.utils.validators import get_oembed_data
from ....permission.enums import ProductPermissions
from ....product import ProductMediaTypes, models
from ....product.error_codes import ProductBulkCreateErrorCode
+from ....product.models import CollectionProduct
from ....product.tasks import update_products_discounted_prices_for_promotion_task
from ....thumbnail.utils import get_filename_from_url
from ....warehouse.models import Warehouse
@@ -763,6 +764,23 @@ class ProductBulkCreate(BaseMutation):
return variants, updated_channels
+ @classmethod
+ def _save_m2m(cls, _info, instances_data):
+ product_collections = []
+ for instance_data in instances_data:
+ product = instance_data["instance"]
+ if not product:
+ continue
+
+ cleaned_input = instance_data["cleaned_input"]
+ if collections := cleaned_input.get("collections"):
+ for collection in collections:
+ product_collections.append(
+ CollectionProduct(product=product, collection=collection)
+ )
+
+ CollectionProduct.objects.bulk_create(product_collections)
+
@classmethod
def prepare_products_channel_listings(
cls, product, listings_input, listings_to_create, updated_channels
@@ -880,6 +898,9 @@ class ProductBulkCreate(BaseMutation):
# save all objects
variants, updated_channels = cls.save(info, instances_data_with_errors_list)
+ # save m2m fields
+ cls._save_m2m(info, instances_data_with_errors_list)
+
# prepare and return data
results = get_results(instances_data_with_errors_list)
products = [result.product for result in results if result.product]
diff --git a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
index 9080e21aef..c8151b0277 100644
--- a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
@@ -15,6 +15,7 @@ from .....attribute.tests.model_helpers import (
from .....product.error_codes import ProductBulkCreateErrorCode
from .....product.models import Product
from .....product.tests.utils import create_image
+from ....core.enums import ErrorPolicyEnum
from ....tests.utils import (
get_graphql_content,
get_multipart_request_body_with_multiple_files,
@@ -23,8 +24,9 @@ from ....tests.utils import (
PRODUCT_BULK_CREATE_MUTATION = """
mutation ProductBulkCreate(
$products: [ProductBulkCreateInput!]!
+ $errorPolicy: ErrorPolicyEnum
) {
- productBulkCreate(products: $products) {
+ productBulkCreate(products: $products, errorPolicy: $errorPolicy) {
results {
errors {
path
@@ -46,6 +48,9 @@ PRODUCT_BULK_CREATE_MUTATION = """
category{
name
}
+ collections{
+ id
+ }
description
attributes{
attribute{
@@ -85,6 +90,7 @@ def test_product_bulk_create_with_base_data(
update_products_discounted_price_task_mock,
staff_api_client,
product_type,
+ collection,
category,
description_json,
permission_manage_products,
@@ -93,7 +99,7 @@ def test_product_bulk_create_with_base_data(
description_json_string = json.dumps(description_json)
product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
category_id = graphene.Node.to_global_id("Category", category.pk)
-
+ collection_id = graphene.Node.to_global_id("Collection", collection.pk)
product_name_1 = "test name 1"
product_name_2 = "test name 2"
base_product_slug = "product-test-slug"
@@ -108,6 +114,7 @@ def test_product_bulk_create_with_base_data(
"description": description_json_string,
"chargeTaxes": product_charge_taxes,
"taxCode": product_tax_rate,
+ "collections": [collection_id],
"weight": 2,
},
{
@@ -139,6 +146,7 @@ def test_product_bulk_create_with_base_data(
assert data["results"][0]["product"]["slug"] == "test-name-1"
assert data["results"][0]["product"]["description"] == description_json_string
assert data["results"][0]["product"]["category"]["name"] == category.name
+ assert data["results"][0]["product"]["collections"][0]["id"] == collection_id
assert data["results"][1]["product"]["name"] == product_name_2
assert data["results"][1]["product"]["description"] == description_json_string
assert data["results"][1]["product"]["category"]["name"] == category.name
@@ -154,6 +162,72 @@ def test_product_bulk_create_with_base_data(
assert args == {product.id for product in products}
+def test_product_bulk_create_with_base_data_and_collections(
+ staff_api_client,
+ product_type,
+ collection_list,
+ category,
+ description_json,
+ permission_manage_products,
+):
+ # given
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ collection_ids = [
+ graphene.Node.to_global_id("Collection", collection.pk)
+ for collection in collection_list
+ ]
+ product_name_1 = "test name 1"
+ product_name_2 = "test name 2"
+ base_product_slug = "product-test-slug"
+ product_charge_taxes = True
+ product_tax_rate = "STANDARD"
+
+ products = [
+ {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name_1,
+ "chargeTaxes": product_charge_taxes,
+ "taxCode": product_tax_rate,
+ "collections": collection_ids,
+ "weight": 2,
+ },
+ {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name_2,
+ "collections": [collection_ids[0]],
+ "slug": f"{base_product_slug}-2",
+ "chargeTaxes": product_charge_taxes,
+ "taxCode": product_tax_rate,
+ },
+ ]
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(
+ PRODUCT_BULK_CREATE_MUTATION,
+ {"products": products},
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productBulkCreate"]
+
+ # then
+ products = Product.objects.all()
+ assert not data["results"][0]["errors"]
+ assert not data["results"][1]["errors"]
+ assert data["count"] == 2
+ assert data["results"][0]["product"]["name"] == product_name_1
+ assert len(data["results"][0]["product"]["collections"]) == len(collection_ids)
+ for collection in data["results"][0]["product"]["collections"]:
+ assert collection["id"] in collection_ids
+ assert data["results"][1]["product"]["name"] == product_name_2
+ assert len(data["results"][1]["product"]["collections"]) == 1
+ assert data["results"][1]["product"]["collections"][0]["id"] == collection_ids[0]
+ assert len(products) == 2
+
+
def test_product_bulk_create_with_no_slug_and_name_with_unslugify_characters(
staff_api_client,
product_type,
@@ -1856,3 +1930,50 @@ def test_product_bulk_create_with_variants_and_channel_listings_with_wrong_price
assert errors[1]["path"] == "variants.0.channelListings.0.costPrice"
assert errors[1]["code"] == ProductBulkCreateErrorCode.INVALID_PRICE.name
assert errors[1]["channels"] == [channel_id]
+
+
+def test_product_bulk_create_with_collections_and_invalid_product_data(
+ staff_api_client,
+ product_type,
+ collection,
+ category,
+ description_json,
+ permission_manage_products,
+):
+ # given
+ description_json_string = json.dumps(description_json)
+ invalid_product_type_id = graphene.Node.to_global_id("ProductType", -999)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ collection_id = graphene.Node.to_global_id("Collection", collection.pk)
+ product_name_1 = "test name 1"
+ product_charge_taxes = True
+ product_tax_rate = "STANDARD"
+
+ products = [
+ {
+ "productType": invalid_product_type_id,
+ "category": category_id,
+ "name": product_name_1,
+ "description": description_json_string,
+ "chargeTaxes": product_charge_taxes,
+ "taxCode": product_tax_rate,
+ "collections": [collection_id],
+ "weight": 2,
+ }
+ ]
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(
+ PRODUCT_BULK_CREATE_MUTATION,
+ {"products": products, "errorPolicy": ErrorPolicyEnum.REJECT_FAILED_ROWS.name},
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productBulkCreate"]
+
+ # then
+ assert data["count"] == 0
+ assert data["results"][0]["errors"]
+ error = data["results"][0]["errors"][0]
+ assert error["code"] == ProductBulkCreateErrorCode.NOT_FOUND.name
+ assert error["path"] == "productType"