Skip to content

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

Hide attributes that belong to hidden products (not visible in listings).

IKarbowiak9e8f9f6e0448
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 1ddf5e7ded..338599cfe0 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -22,7 +22,11 @@ from ..core.filters import EnumFilter, ListObjectTypeFilter, ObjectTypeFilter
from ..core.types import FilterInputObjectType
from ..core.types.common import IntRangeInput, PriceRangeInput
from ..core.utils import from_global_id_strict_type
-from ..utils import get_nodes, resolve_global_ids_to_primary_keys
+from ..utils import (
+ get_nodes,
+ get_user_or_app_from_context,
+ resolve_global_ids_to_primary_keys,
+)
from ..utils.filters import filter_by_query_param, filter_range_field
from ..warehouse import types as warehouse_types
from .enums import (
@@ -206,7 +210,7 @@ def filter_product_type(qs, _, value):
return qs
-def filter_attributes_by_product_types(qs, field, value):
+def filter_attributes_by_product_types(qs, field, value, requestor):
if not value:
return qs
@@ -222,6 +226,9 @@ def filter_attributes_by_product_types(qs, field, value):
tree = category.get_descendants(include_self=True)
product_qs = Product.objects.filter(category__in=tree)
+ if not product_qs.user_has_access_to_all(requestor):
+ product_qs = product_qs.exclude(visible_in_listings=False)
+
elif field == "in_collection":
collection_id = from_global_id_strict_type(
value, only_type="Collection", field=field
@@ -378,8 +385,8 @@ class AttributeFilter(django_filters.FilterSet):
)
ids = GlobalIDMultipleChoiceFilter(field_name="id")
- in_collection = GlobalIDFilter(method=filter_attributes_by_product_types)
- in_category = GlobalIDFilter(method=filter_attributes_by_product_types)
+ in_collection = GlobalIDFilter(method="filter_in_collection")
+ in_category = GlobalIDFilter(method="filter_in_category")
class Meta:
model = Attribute
@@ -392,6 +399,14 @@ class AttributeFilter(django_filters.FilterSet):
"available_in_grid",
]
+ def filter_in_collection(self, queryset, name, value):
+ requestor = get_user_or_app_from_context(self.request)
+ return filter_attributes_by_product_types(queryset, name, value, requestor)
+
+ def filter_in_category(self, queryset, name, value):
+ requestor = get_user_or_app_from_context(self.request)
+ return filter_attributes_by_product_types(queryset, name, value, requestor)
+
class ProductFilterInput(FilterInputObjectType):
class Meta:
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index 4f3fec1a95..3b380959e2 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -4,21 +4,12 @@ from ...order import OrderStatus
from ...product import models
from ..utils import get_database_id, get_user_or_app_from_context
from ..utils.filters import filter_by_period
-from .filters import (
- filter_attributes_by_product_types,
- filter_products_by_stock_availability,
-)
+from .filters import filter_products_by_stock_availability
-def resolve_attributes(info, qs=None, in_category=None, in_collection=None, **_kwargs):
- qs = qs or models.Attribute.objects.get_visible_to_user(info.context.user)
-
- if in_category:
- qs = filter_attributes_by_product_types(qs, "in_category", in_category)
-
- if in_collection:
- qs = filter_attributes_by_product_types(qs, "in_collection", in_collection)
-
+def resolve_attributes(info, qs=None, **_kwargs):
+ requestor = get_user_or_app_from_context(info.context)
+ qs = qs or models.Attribute.objects.get_visible_to_user(requestor)
return qs.distinct()
diff --git a/saleor/graphql/product/tests/test_attribute_pagination.py b/saleor/graphql/product/tests/test_attribute_pagination.py
index e47abc4a18..78416f9ba9 100644
--- a/saleor/graphql/product/tests/test_attribute_pagination.py
+++ b/saleor/graphql/product/tests/test_attribute_pagination.py
@@ -50,7 +50,10 @@ def attributes_for_pagination(collection, category):
product_type = ProductType.objects.create(name="My Product Type")
product = Product.objects.create(
- name="Test product", product_type=product_type, category=category,
+ name="Test product",
+ product_type=product_type,
+ category=category,
+ visible_in_listings=True,
)
collection.products.add(product)
AttributeVariant.objects.bulk_create(
diff --git a/saleor/graphql/product/tests/test_attributes.py b/saleor/graphql/product/tests/test_attributes.py
index 3851c35872..c266658281 100644
--- a/saleor/graphql/product/tests/test_attributes.py
+++ b/saleor/graphql/product/tests/test_attributes.py
@@ -337,11 +337,11 @@ def test_attributes_filter_by_product_type_with_empty_value():
qs = Attribute.objects.all()
- assert filter_attributes_by_product_types(qs, "...", "") is qs
- assert filter_attributes_by_product_types(qs, "...", None) is qs
+ assert filter_attributes_by_product_types(qs, "...", "", None) is qs
+ assert filter_attributes_by_product_types(qs, "...", None, None) is qs
-def test_attributes_filter_by_product_type_with_unsupported_field():
+def test_attributes_filter_by_product_type_with_unsupported_field(customer_user):
"""Ensure using an unknown field to filter attributes by raises a NotImplemented
exception.
"""
@@ -349,17 +349,19 @@ def test_attributes_filter_by_product_type_with_unsupported_field():
qs = Attribute.objects.all()
with pytest.raises(NotImplementedError) as exc:
- filter_attributes_by_product_types(qs, "in_space", "a-value")
+ filter_attributes_by_product_types(qs, "in_space", "a-value", customer_user)
assert exc.value.args == ("Filtering by in_space is unsupported",)
-def test_attributes_filter_by_non_existing_category_id():
+def test_attributes_filter_by_non_existing_category_id(customer_user):
"""Ensure using a non-existing category ID returns an empty query set."""
category_id = graphene.Node.to_global_id("Category", -1)
mocked_qs = mock.MagicMock()
- qs = filter_attributes_by_product_types(mocked_qs, "in_category", category_id)
+ qs = filter_attributes_by_product_types(
+ mocked_qs, "in_category", category_id, customer_user
+ )
assert qs == mocked_qs.none.return_value
@@ -1982,6 +1984,166 @@ def test_filter_attributes_by_global_id_list(api_client, attribute_list):
assert received_slugs == expected_slugs
+def test_filter_attributes_in_category_by_customer(
+ user_api_client, product_list, weight_attribute
+):
+ # given
+ product_type = ProductType.objects.create(
+ name="Default Type 2",
+ slug="default-type-2",
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.product_attributes.add(weight_attribute)
+
+ last_product = product_list[-1]
+ last_product.product_type = product_type
+ last_product.visible_in_listings = False
+ last_product.save(update_fields=["visible_in_listings", "product_type"])
+
+ associate_attribute_values_to_instance(
+ product_list[-1], weight_attribute, weight_attribute.values.first()
+ )
+
+ attribute_count = Attribute.objects.count()
+
+ category = last_product.category
+ variables = {
+ "filters": {"inCategory": graphene.Node.to_global_id("Category", category.pk)}
+ }
+
+ # when
+ attributes = get_graphql_content(
+ user_api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ # then
+ assert len(attributes) == attribute_count - 1
+ assert weight_attribute.slug not in {
+ attribute["node"]["slug"] for attribute in attributes
+ }
+
+
+def test_filter_attributes_in_category_by_staff_with_perm(
+ staff_api_client, product_list, weight_attribute, permission_manage_products
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ product_type = ProductType.objects.create(
+ name="Default Type 2",
+ slug="default-type-2",
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.product_attributes.add(weight_attribute)
+
+ last_product = product_list[-1]
+ last_product.product_type = product_type
+ last_product.visible_in_listings = False
+ last_product.save(update_fields=["visible_in_listings", "product_type"])
+
+ associate_attribute_values_to_instance(
+ product_list[-1], weight_attribute, weight_attribute.values.first()
+ )
+
+ attribute_count = Attribute.objects.count()
+
+ category = last_product.category
+ variables = {
+ "filters": {"inCategory": graphene.Node.to_global_id("Category", category.pk)}
+ }
+
+ # when
+ attributes = get_graphql_content(
+ staff_api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ # then
+ assert len(attributes) == attribute_count
+
+
+def test_filter_attributes_in_category_by_staff_without_perm(
+ staff_api_client, product_list, weight_attribute, permission_manage_products
+):
+ # given
+ product_type = ProductType.objects.create(
+ name="Default Type 2",
+ slug="default-type-2",
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.product_attributes.add(weight_attribute)
+
+ last_product = product_list[-1]
+ last_product.product_type = product_type
+ last_product.visible_in_listings = False
+ last_product.save(update_fields=["visible_in_listings", "product_type"])
+
+ associate_attribute_values_to_instance(
+ product_list[-1], weight_attribute, weight_attribute.values.first()
+ )
+
+ attribute_count = Attribute.objects.count()
+
+ category = last_product.category
+ variables = {
+ "filters": {"inCategory": graphene.Node.to_global_id("Category", category.pk)}
+ }
+
+ # when
+ attributes = get_graphql_content(
+ staff_api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ # then
+ assert len(attributes) == attribute_count - 1
+ assert weight_attribute.slug not in {
+ attribute["node"]["slug"] for attribute in attributes
+ }
+
+
+def test_filter_attributes_in_collection_by_customer(
+ user_api_client, product_list, weight_attribute, collection
+):
+ # given
+ product_type = ProductType.objects.create(
+ name="Default Type 2",
+ slug="default-type-2",
+ has_variants=True,
+ is_shipping_required=True,
+ )
+ product_type.product_attributes.add(weight_attribute)
+
+ last_product = product_list[-1]
+ last_product.product_type = product_type
+ last_product.visible_in_listings = False
+ last_product.save(update_fields=["visible_in_listings", "product_type"])
+
+ for product in product_list:
+ collection.products.add(product)
+
+ associate_attribute_values_to_instance(
+ product_list[-1], weight_attribute, weight_attribute.values.first()
+ )
+
+ attribute_count = Attribute.objects.count()
+
+ variables = {
+ "filters": {
+ "inCollection": graphene.Node.to_global_id("Collection", collection.pk)
+ }
+ }
+
+ # when
+ attributes = get_graphql_content(
+ user_api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ # then
+ assert len(attributes) == attribute_count
+
+
ATTRIBUTES_SORT_QUERY = """
query($sortBy: AttributeSortingInput) {
attributes(first: 10, sortBy: $sortBy) {
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 875e6711bf..9735bc9021 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -594,6 +594,16 @@ def size_attribute(db): # pylint: disable=W0613
return attribute
+def weight_attribute(db):
+ attribute = Attribute.objects.create(slug="material", name="Material")
+ AttributeValue.objects.create(attribute=attribute, name="Cotton", slug="cotton")
+ AttributeValue.objects.create(
+ attribute=attribute, name="Poliester", slug="poliester"
+ )
+ return attribute
+
+
@pytest.fixture
def attribute_list() -> List[Attribute]:
return list(