Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Shrink to one mutation with parameter
Adam Janikc2a8f3163217
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 55ec955072..72b353b82e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,7 +12,7 @@ All notable, unreleased changes to this project will be documented in this file.- Move dialog windows to querystring rather than router paths - #3953 by @dominik-zeglen- Add mutation for bulk cancel orders - #3967 by @akjanik- Cleanup and maintenance of the GraphQL API code - #3942 by @NyanKiyoshi-- Add mutations for bulk publish products - #3969 by akjanik+- Add mutations for bulk publishing and unpublishing products - #3969 by akjanik## 2.5.0diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.pyindex 32b4cba19e..37dd75653d 100644--- a/saleor/graphql/product/bulk_mutations/products.py+++ b/saleor/graphql/product/bulk_mutations/products.py@@ -106,27 +106,18 @@ class ProductBulkPublish(ModelBulkPublishMutation):graphene.ID,required=True,description='List of products IDs to publish.')+ is_published = graphene.Boolean(+ required=True,+ description='Determine if product will be published or not.')class Meta:description = 'Publish products.'model = models.Product@classmethod- def user_is_allowed(cls, user, input):+ def user_is_allowed(cls, user, _ids):return user.has_perm('product.manage_products')--class ProductBulkUnpublish(ProductBulkPublish):- class Arguments:- ids = graphene.List(- graphene.ID,- required=True,- description='List of products IDs to unpublish.')-- class Meta:- description = 'Unpublish products.'- model = models.Product-@classmethod- def bulk_action(cls, queryset):- queryset.update(is_published=False)+ def bulk_action(cls, queryset, is_published):+ queryset.update(is_published=is_published)diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex 35aaf9d5b1..0518bade3e 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -13,8 +13,8 @@ from .bulk_mutations.attributes import (AttributeBulkDelete, AttributeValueBulkDelete)from .bulk_mutations.products import (CategoryBulkDelete, CollectionBulkDelete, ProductBulkDelete,- ProductBulkPublish, ProductBulkUnpublish, ProductImageBulkDelete,- ProductTypeBulkDelete, ProductVariantBulkDelete)+ ProductBulkPublish, ProductImageBulkDelete, ProductTypeBulkDelete,+ ProductVariantBulkDelete)from .enums import StockAvailabilityfrom .mutations.attributes import (AttributeCreate, AttributeDelete, AttributeUpdate, AttributeValueCreate,@@ -195,7 +195,6 @@ class ProductMutations(graphene.ObjectType):product_delete = ProductDelete.Field()product_bulk_delete = ProductBulkDelete.Field()product_bulk_publish = ProductBulkPublish.Field()- product_bulk_unpublish = ProductBulkUnpublish.Field()product_update = ProductUpdate.Field()product_translate = ProductTranslate.Field()diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 8180efe626..860ddb1a87 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1121,8 +1121,7 @@ type Mutations {productCreate(input: ProductCreateInput!): ProductCreateproductDelete(id: ID!): ProductDeleteproductBulkDelete(ids: [ID]!): ProductBulkDelete- productBulkPublish(ids: [ID]!): ProductBulkPublish- productBulkUnpublish(ids: [ID]!): ProductBulkUnpublish+ productBulkPublish(ids: [ID]!, isPublished: Boolean!): ProductBulkPublishproductUpdate(id: ID!, input: ProductInput!): ProductUpdateproductTranslate(id: ID!, input: TranslationInput!, languageCode: LanguageCodeEnum!): ProductTranslateproductImageCreate(input: ProductImageCreateInput!): ProductImageCreate@@ -1707,11 +1706,6 @@ type ProductBulkPublish {count: Int!}-type ProductBulkUnpublish {- errors: [Error!]- count: Int!-}-type ProductCountableConnection {pageInfo: PageInfo!edges: [ProductCountableEdge!]!diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 8083873a29..2aadcef244 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -1798,24 +1798,27 @@ def test_variant_digital_content(assert 'id' in content['data']['productVariant']['digitalContent']-def test_bulk_publish(+MUTATION_BULK_PUBLISH_PRODUCTS = """+ mutation publishManyProducts($ids: [ID]!, $is_published: Boolean!) {+ productBulkPublish(ids: $ids, isPublished: $is_published) {+ count+ }+ }+ """+++def test_bulk_publish_products(staff_api_client, product_list_unpublished,permission_manage_products):product_list = product_list_unpublishedassert not any(product.is_published for product in product_list)- query = """- mutation publishManyProducts($ids: [ID]!) {- productBulkPublish(ids: $ids) {- count- }- }- """variables = {'ids': [graphene.Node.to_global_id('Product', product.id)- for product in product_list]}+ for product in product_list], 'is_published': True}response = staff_api_client.post_graphql(- query, variables, permissions=[permission_manage_products])+ MUTATION_BULK_PUBLISH_PRODUCTS, variables,+ permissions=[permission_manage_products])content = get_graphql_content(response)product_list = Product.objects.filter(id__in=[product.pk for product in product_list])@@ -1824,28 +1827,22 @@ def test_bulk_publish(assert all(product.is_published for product in product_list)-def test_bulk_unpublish(+def test_bulk_unpublish_products(staff_api_client, product_list_published,permission_manage_products):product_list = product_list_publishedassert all(product.is_published for product in product_list)- query = """- mutation unPublishManyProducts($ids: [ID]!) {- productBulkUnpublish(ids: $ids) {- count- }- }- """variables = {'ids': [graphene.Node.to_global_id('Product', product.id)- for product in product_list]}+ for product in product_list], 'is_published': False}response = staff_api_client.post_graphql(- query, variables, permissions=[permission_manage_products])+ MUTATION_BULK_PUBLISH_PRODUCTS, variables,+ permissions=[permission_manage_products])content = get_graphql_content(response)product_list = Product.objects.filter(id__in=[product.pk for product in product_list])- assert content['data']['productBulkUnpublish']['count'] == len(+ assert content['data']['productBulkPublish']['count'] == len(product_list)assert not any(product.is_published for product in product_list)