Skip to content

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

Added flags to attributes: whether they are filterable or not

NyanKiyoshid7f878fc8e11
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 60d7a07964..ddd6814647 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Added `attributeAssign(...)` and `attributeUnassign(...)` mutations in the GraphQL API. - #4266 by @NyanKiyoshi
- Implemented a `input_type` field into the attribute models and refactored the product attributes to be able to store more than one value per attribute per product. - #4255 by @NyanKiyoshi.
- Staff users can now flag attributes as to be hidden in the storefront. - #4338 by @NyanKiyoshi
+- Added flags to attributes: whether they are filterable or not in dashboard and/or storefront. - #4341 by @NyanKiyoshi.
## [Unreleased]
diff --git a/saleor/graphql/product/descriptions.py b/saleor/graphql/product/descriptions.py
index dc21a3da6d..79e8ad3305 100644
--- a/saleor/graphql/product/descriptions.py
+++ b/saleor/graphql/product/descriptions.py
@@ -7,6 +7,8 @@ class AttributeDescriptions:
VISIBLE_IN_STOREFRONT = (
"Whether the attribute should be visible or not in storefront"
)
+ FILTERABLE_IN_STOREFRONT = "Whether the attribute can be filtered in storefront"
+ FILTERABLE_IN_DASHBOARD = "Whether the attribute can be filtered in dashboard"
class AttributeValueDescriptions:
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index d5b6fbcfd4..8afeecbec0 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -38,6 +38,12 @@ class AttributeCreateInput(graphene.InputObjectType):
visible_in_storefront = graphene.Boolean(
description=AttributeDescriptions.VISIBLE_IN_STOREFRONT
)
+ filterable_in_storefront = graphene.Boolean(
+ description=AttributeDescriptions.FILTERABLE_IN_STOREFRONT
+ )
+ filterable_in_dashboard = graphene.Boolean(
+ description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD
+ )
class AttributeUpdateInput(graphene.InputObjectType):
@@ -59,6 +65,12 @@ class AttributeUpdateInput(graphene.InputObjectType):
visible_in_storefront = graphene.Boolean(
description=AttributeDescriptions.VISIBLE_IN_STOREFRONT
)
+ filterable_in_storefront = graphene.Boolean(
+ description=AttributeDescriptions.FILTERABLE_IN_STOREFRONT
+ )
+ filterable_in_dashboard = graphene.Boolean(
+ description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD
+ )
class AttributeAssignInput(graphene.InputObjectType):
diff --git a/saleor/graphql/product/types/attributes.py b/saleor/graphql/product/types/attributes.py
index f839a460a0..7e6f4191bf 100644
--- a/saleor/graphql/product/types/attributes.py
+++ b/saleor/graphql/product/types/attributes.py
@@ -79,6 +79,20 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
model_field="values",
)
+ visible_in_storefront = gql_optimizer.field(
+ graphene.Boolean(description=AttributeDescriptions.VISIBLE_IN_STOREFRONT),
+ model_field="visible_in_storefront",
+ )
+
+ filterable_in_storefront = gql_optimizer.field(
+ graphene.Boolean(description=AttributeDescriptions.FILTERABLE_IN_STOREFRONT),
+ model_field="filterable_in_storefront",
+ )
+ filterable_in_dashboard = gql_optimizer.field(
+ graphene.Boolean(description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD),
+ model_field="filterable_in_dashboard",
+ )
+
translation = graphene.Field(
AttributeTranslation,
language_code=graphene.Argument(
@@ -96,12 +110,7 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
description = """
Custom attribute of a product. Attributes can be
assigned to products and variants at the product type level."""
- only_fields = [
- "id",
- "product_types",
- "product_variant_types",
- "visible_in_storefront",
- ]
+ only_fields = ["id", "product_types", "product_variant_types"]
interfaces = [relay.Node]
model = models.Attribute
@@ -118,6 +127,21 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
def resolve_meta(root, _info):
return resolve_meta(root, _info)
+ @staticmethod
+ @permission_required("product.manage_products")
+ def resolve_visible_in_storefront(root: models.Attribute, *_args):
+ return root.visible_in_storefront
+
+ @staticmethod
+ @permission_required("product.manage_products")
+ def resolve_filterable_in_storefront(root: models.Attribute, *_args):
+ return root.filterable_in_storefront
+
+ @staticmethod
+ @permission_required("product.manage_products")
+ def resolve_filterable_in_dashboard(root: models.Attribute, *_args):
+ return root.filterable_in_dashboard
+
class SelectedAttribute(graphene.ObjectType):
attribute = graphene.Field(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 5344eae5b1..a7f3a60c92 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -145,13 +145,15 @@ type Attribute implements Node {
id: ID!
productTypes(before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection
productVariantTypes(before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection
- visibleInStorefront: Boolean!
privateMeta: [MetaStore]!
meta: [MetaStore]!
inputType: AttributeInputTypeEnum
name: String
slug: String
values: [AttributeValue]
+ visibleInStorefront: Boolean
+ filterableInStorefront: Boolean
+ filterableInDashboard: Boolean
translation(languageCode: LanguageCodeEnum!): AttributeTranslation
}
@@ -203,6 +205,8 @@ input AttributeCreateInput {
values: [AttributeValueCreateInput]
isVariantOnly: Boolean
visibleInStorefront: Boolean
+ filterableInStorefront: Boolean
+ filterableInDashboard: Boolean
}
type AttributeDelete {
@@ -255,6 +259,8 @@ input AttributeUpdateInput {
addValues: [AttributeValueCreateInput]
isVariantOnly: Boolean
visibleInStorefront: Boolean
+ filterableInStorefront: Boolean
+ filterableInDashboard: Boolean
}
type AttributeUpdateMeta {
diff --git a/saleor/product/migrations/0096_migrate_attrs_to_m2m.py b/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
index 8d42b6451e..c79081ff26 100644
--- a/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
+++ b/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
@@ -156,4 +156,14 @@ class Migration(migrations.Migration):
name="visible_in_storefront",
field=models.BooleanField(default=True),
),
+ migrations.AddField(
+ model_name="attribute",
+ name="filterable_in_dashboard",
+ field=models.BooleanField(default=True),
+ ),
+ migrations.AddField(
+ model_name="attribute",
+ name="filterable_in_storefront",
+ field=models.BooleanField(default=True),
+ ),
]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 01ab022a71..f5c430c2d5 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -525,6 +525,9 @@ class Attribute(ModelWithMetadata):
visible_in_storefront = models.BooleanField(default=True)
is_variant_only = models.BooleanField(default=False)
+ filterable_in_storefront = models.BooleanField(default=True)
+ filterable_in_dashboard = models.BooleanField(default=True)
+
objects = AttributeQuerySet.as_manager()
translated = TranslationProxy()
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 065324cd25..b2ffd5ed2c 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -2,6 +2,7 @@ import graphene
import pytest
from django.db.models import Q
from django.template.defaultfilters import slugify
+from graphene.utils.str_converters import to_camel_case
from saleor.graphql.product.enums import AttributeTypeEnum, AttributeValueType
from saleor.graphql.product.types.attributes import resolve_attribute_value_type
@@ -1161,3 +1162,42 @@ def test_retrieve_product_attributes_input_type(
for gql_attr in found_products[0]["node"]["attributes"]:
assert gql_attr["value"]["type"] == "STRING"
assert gql_attr["value"]["inputType"] == "DROPDOWN"
+
+
+ "attribute, expected_value",
+ (
+ ("filterable_in_storefront", True),
+ ("filterable_in_dashboard", True),
+ ("visible_in_storefront", True),
+ ),
+)
+def test_retrieving_the_restricted_attributes_restricted(
+ staff_api_client,
+ color_attribute,
+ permission_manage_products,
+ attribute,
+ expected_value,
+):
+ attribute = to_camel_case(attribute)
+ query = (
+ """
+ {
+ attributes(first: 10) {
+ edges {
+ node {
+ %s
+ }
+ }
+ }
+ }
+ """
+ % attribute
+ )
+
+ found_attributes = get_graphql_content(
+ staff_api_client.post_graphql(query, permissions=[permission_manage_products])
+ )["data"]["attributes"]["edges"]
+
+ assert len(found_attributes) == 1
+ assert found_attributes[0]["node"][attribute] == expected_value