Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add mutation for bulk publish/unpublish collections
Adam Janikb99ff33f73cc
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.pyindex b29f9d61ac..3e212a9b0d 100644--- a/saleor/graphql/product/bulk_mutations/products.py+++ b/saleor/graphql/product/bulk_mutations/products.py@@ -36,6 +36,38 @@ class CollectionBulkDelete(ModelBulkDeleteMutation):return user.has_perm('product.manage_products')+class CollectionBulkPublish(ModelBulkPublishMutation):+ class Arguments:+ ids = graphene.List(+ graphene.ID,+ required=True,+ description='List of collections IDs to publish.')++ class Meta:+ description = 'Publish collections.'+ model = models.Collection++ @classmethod+ def user_is_allowed(cls, user, input):+ return user.has_perm('product.manage_products')+++class CollectionBulkUnpublish(CollectionBulkPublish):+ class Arguments:+ ids = graphene.List(+ graphene.ID,+ required=True,+ description='List of collections IDs to unpublish.')++ class Meta:+ description = 'Unpublish collections.'+ model = models.Collection++ @classmethod+ def bulk_action(cls, queryset):+ queryset.update(is_published=False)++class ProductBulkDelete(ModelBulkDeleteMutation):class Arguments:ids = graphene.List(diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex 0518bade3e..19d82cde24 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -12,9 +12,9 @@ from ..translations.mutations import (from .bulk_mutations.attributes import (AttributeBulkDelete, AttributeValueBulkDelete)from .bulk_mutations.products import (- CategoryBulkDelete, CollectionBulkDelete, ProductBulkDelete,- ProductBulkPublish, ProductImageBulkDelete, ProductTypeBulkDelete,- ProductVariantBulkDelete)+ CategoryBulkDelete, CollectionBulkDelete, CollectionBulkPublish,+ ProductBulkDelete, ProductBulkPublish, ProductImageBulkDelete,+ ProductTypeBulkDelete, ProductVariantBulkDelete)from .enums import StockAvailabilityfrom .mutations.attributes import (AttributeCreate, AttributeDelete, AttributeUpdate, AttributeValueCreate,@@ -187,6 +187,7 @@ class ProductMutations(graphene.ObjectType):collection_create = CollectionCreate.Field()collection_delete = CollectionDelete.Field()collection_bulk_delete = CollectionBulkDelete.Field()+ collection_bulk_publish = CollectionBulkPublish.Field()collection_remove_products = CollectionRemoveProducts.Field()collection_update = CollectionUpdate.Field()collection_translate = CollectionTranslate.Field()diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 32d819b6d0..36530a89c5 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -498,6 +498,16 @@ type CollectionBulkDelete {count: Int!}+type CollectionBulkPublish {+ errors: [Error!]+ count: Int!+}++type CollectionBulkUnpublish {+ errors: [Error!]+ count: Int!+}+type CollectionCountableConnection {pageInfo: PageInfo!edges: [CollectionCountableEdge!]!@@ -1114,6 +1124,8 @@ type Mutations {collectionCreate(input: CollectionCreateInput!): CollectionCreatecollectionDelete(id: ID!): CollectionDeletecollectionBulkDelete(ids: [ID]!): CollectionBulkDelete+ collectionBulkPublish(ids: [ID]!): CollectionBulkPublish+ collectionBulkUnpublish(ids: [ID]!): CollectionBulkUnpublishcollectionRemoveProducts(collectionId: ID!, products: [ID]!): CollectionRemoveProductscollectionUpdate(id: ID!, input: CollectionInput!): CollectionUpdatecollectionTranslate(id: ID!, input: TranslationInput!, languageCode: LanguageCodeEnum!): CollectionTranslatediff --git a/tests/api/test_bulk_delete.py b/tests/api/test_bulk_delete.pyindex d629cc534e..2d0f09dfff 100644--- a/tests/api/test_bulk_delete.py+++ b/tests/api/test_bulk_delete.py@@ -59,14 +59,6 @@ def category_list():return category_1, category_2, category_3-def collection_list():- collection_1 = Collection.objects.create(name='Collection 1')- collection_2 = Collection.objects.create(name='Collection 2')- collection_3 = Collection.objects.create(name='Collection 3')- return collection_1, collection_2, collection_3--@pytest.fixturedef menu_list():menu_1 = Menu.objects.create(name='test-navbar-1', json_content={})diff --git a/tests/api/test_collection.py b/tests/api/test_collection.pyindex cace77d7c7..42718188cd 100644--- a/tests/api/test_collection.py+++ b/tests/api/test_collection.py@@ -44,7 +44,8 @@ def test_collections_query(assert collection_data['name'] == collection.nameassert collection_data['slug'] == collection.slugassert collection_data['description'] == collection.description- assert collection_data['products']['totalCount'] == collection.products.count()+ assert collection_data['products'][+ 'totalCount'] == collection.products.count()# query all collections only as a staff user with proper permissionsstaff_api_client.user.user_permissions.add(permission_manage_products)@@ -55,7 +56,8 @@ def test_collections_query(def test_create_collection(- monkeypatch, staff_api_client, product_list, permission_manage_products):+ monkeypatch, staff_api_client, product_list,+ permission_manage_products):query = """mutation createCollection($name: String!, $slug: String!, $description: String,@@ -128,7 +130,8 @@ def test_create_collection(def test_create_collection_without_background_image(- monkeypatch, staff_api_client, product_list, permission_manage_products):+ monkeypatch, staff_api_client, product_list,+ permission_manage_products):query = """mutation createCollection($name: String!, $slug: String!, $products: [ID], $isPublished: Boolean!) {@@ -149,7 +152,7 @@ def test_create_collection_without_background_image(mock_create_thumbnails)variables = {- 'name': 'test-name', 'slug': 'test-slug', 'isPublished': True,}+ 'name': 'test-name', 'slug': 'test-slug', 'isPublished': True, }response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products])get_graphql_content(response)@@ -185,7 +188,8 @@ def test_update_collection(publication_date = date.today()variables = {'name': name, 'slug': slug, 'description': description,- 'id': to_global_id('Collection', collection.id), 'isPublished': True, 'publicationDate': publication_date}+ 'id': to_global_id('Collection', collection.id), 'isPublished': True,+ 'publicationDate': publication_date}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products])content = get_graphql_content(response)@@ -346,7 +350,8 @@ def test_add_products_to_collection(content = get_graphql_content(response)data = content['data']['collectionAddProducts']['collection']assert data[- 'products']['totalCount'] == no_products_before + len(product_ids)+ 'products']['totalCount'] == no_products_before + len(+ product_ids)def test_remove_products_from_collection(@@ -375,7 +380,8 @@ def test_remove_products_from_collection(content = get_graphql_content(response)data = content['data']['collectionRemoveProducts']['collection']assert data[- 'products']['totalCount'] == no_products_before - len(product_ids)+ 'products']['totalCount'] == no_products_before - len(+ product_ids)FETCH_COLLECTION_QUERY = """@@ -478,7 +484,7 @@ def test_fetch_unpublished_collection_staff_user(permissions=[permission_manage_products])assert collection_data['name'] == unpublished_collection.nameassert collection_data[- 'isPublished'] == unpublished_collection.is_published+ 'isPublished'] == unpublished_collection.is_publisheddef test_fetch_unpublished_collection_customer(@@ -492,3 +498,56 @@ def test_fetch_unpublished_collection_anonymous_user(api_client, unpublished_collection):collection_data = _fetch_collection(api_client, unpublished_collection)assert collection_data is None+++def test_bulk_publish_collection(+ staff_api_client, collection_list_unpublished,+ permission_manage_products):+ collection_list = collection_list_unpublished+ assert not any(collection.is_published for collection in collection_list)++ query = """+ mutation publishManyCollections($ids: [ID]!) {+ collectionBulkPublish(ids: $ids) {+ count+ }+ }+ """+ variables = {'ids': [+ graphene.Node.to_global_id('Collection', collection.id)+ for collection in collection_list]}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products])+ content = get_graphql_content(response)+ collection_list = Collection.objects.filter(+ id__in=[collection.pk for collection in collection_list])++ assert content['data']['collectionBulkPublish']['count'] == len(+ collection_list)+ assert all(collection.is_published for collection in collection_list)+++def test_bulk_unpublish_collection(+ staff_api_client, collection_list,+ permission_manage_products):+ assert all(collection.is_published for collection in collection_list)++ query = """+ mutation unPublishManyCollections($ids: [ID]!) {+ collectionBulkUnpublish(ids: $ids) {+ count+ }+ }+ """+ variables = {'ids': [+ graphene.Node.to_global_id('Collection', collection.id)+ for collection in collection_list]}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products])+ content = get_graphql_content(response)+ collection_list = Collection.objects.filter(+ id__in=[collection.pk for collection in collection_list])++ assert content['data']['collectionBulkUnpublish']['count'] == len(+ collection_list)+ assert not any(collection.is_published for collection in collection_list)diff --git a/tests/conftest.py b/tests/conftest.pyindex 3666b8c780..0c34683cd9 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -743,6 +743,26 @@ def collection_with_image(db, image):return collection+def collection_list(db):+ collections = Collection.objects.bulk_create(+ [+ Collection(name='Collection 1'),+ Collection(name='Collection 2'),+ Collection(name='Collection 3'),+ ]+ )+ return collections+++def collection_list_unpublished(collection_list):+ collections = Collection.objects.filter(+ pk__in=[collection.pk for collection in collection_list])+ collections.update(is_published=False)+ return collections++@pytest.fixturedef draft_collection(db):collection = Collection.objects.create(