Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Exclude not visible in listings products from category.products query
IKarbowiak8d720c4636ba
diff --git a/saleor/graphql/product/tests/test_category.py b/saleor/graphql/product/tests/test_category.pyindex 853cb2b255..7f26209b0b 100644--- a/saleor/graphql/product/tests/test_category.py+++ b/saleor/graphql/product/tests/test_category.py@@ -7,7 +7,7 @@ from django.utils.text import slugifyfrom graphql_relay import to_global_idfrom ....product.error_codes import ProductErrorCode-from ....product.models import Category+from ....product.models import Category, Productfrom ....product.tests.utils import create_image, create_pdf_file_with_image_extfrom ...tests.utils import get_graphql_content, get_multipart_request_body@@ -33,6 +33,13 @@ QUERY_CATEGORY = """}}}+ products(first: 10) {+ edges {+ node {+ id+ }+ }+ }}}"""@@ -95,6 +102,77 @@ def test_category_query_error_when_no_param(assert len(content["errors"]) == 1+def test_query_category_product_only_visible_in_listings_as_customer(+ user_api_client, product_list+):+ # given+ category = Category.objects.first()++ product_list[0].visible_in_listings = False+ product_list[0].save(update_fields=["visible_in_listings"])++ product_count = Product.objects.count()++ variables = {+ "id": graphene.Node.to_global_id("Category", category.pk),+ }++ # when+ response = user_api_client.post_graphql(QUERY_CATEGORY, variables=variables)++ # then+ content = get_graphql_content(response, ignore_errors=True)+ assert len(content["data"]["category"]["products"]["edges"]) == product_count - 1+++def test_query_category_product_only_visible_in_listings_as_staff_without_perm(+ staff_api_client, product_list+):+ # given+ category = Category.objects.first()++ product_list[0].visible_in_listings = False+ product_list[0].save(update_fields=["visible_in_listings"])++ product_count = Product.objects.count()++ variables = {+ "id": graphene.Node.to_global_id("Category", category.pk),+ }++ # when+ response = staff_api_client.post_graphql(QUERY_CATEGORY, variables=variables)++ # then+ content = get_graphql_content(response, ignore_errors=True)+ assert len(content["data"]["category"]["products"]["edges"]) == product_count - 1+++def test_query_category_product_only_visible_in_listings_as_staff_with_perm(+ staff_api_client, product_list, permission_manage_products+):+ # given+ staff_api_client.user.user_permissions.add(permission_manage_products)++ category = Category.objects.first()++ product_list[0].visible_in_listings = False+ product_list[0].save(update_fields=["visible_in_listings"])++ product_count = Product.objects.count()++ variables = {+ "id": graphene.Node.to_global_id("Category", category.pk),+ }++ # when+ response = staff_api_client.post_graphql(QUERY_CATEGORY, variables=variables)++ # then+ content = get_graphql_content(response, ignore_errors=True)+ assert len(content["data"]["category"]["products"]["edges"]) == product_count++CATEGORY_CREATE_MUTATION = """mutation($name: String, $slug: String, $description: String,diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex 3bf54f6068..b3374c06f8 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -41,7 +41,7 @@ from ...translations.types import (ProductTranslation,ProductVariantTranslation,)-from ...utils import get_database_id+from ...utils import get_database_id, get_user_or_app_from_contextfrom ...utils.filters import reporting_period_to_datefrom ...warehouse.dataloaders import (AvailableQuantityByProductVariantIdAndCountryCodeLoader,@@ -853,8 +853,11 @@ class Category(CountableDjangoObjectType):@staticmethoddef resolve_products(root: models.Category, info, **_kwargs):+ requestor = get_user_or_app_from_context(info.context)tree = root.get_descendants(include_self=True)qs = models.Product.objects.published()+ if not qs.user_has_access_to_all(requestor):+ qs = qs.exclude(visible_in_listings=False)return qs.filter(category__in=tree)@staticmethod