Skip to content

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

Added flag for filtering attributes on product grid

NyanKiyoshi2dcfb19330a8
diff --git a/saleor/graphql/product/descriptions.py b/saleor/graphql/product/descriptions.py
index 109ecceb3b..25661f1905 100644
--- a/saleor/graphql/product/descriptions.py
+++ b/saleor/graphql/product/descriptions.py
@@ -13,6 +13,9 @@ class AttributeDescriptions:
STOREFRONT_SEARCH_POSITION = (
"The position of the attribute in the storefront navigation (0 by default)"
)
+ AVAILABLE_IN_GRID = (
+ "Whether the attribute can be displayed in the admin product list"
+ )
class AttributeValueDescriptions:
diff --git a/saleor/graphql/product/enums.py b/saleor/graphql/product/enums.py
index ea890a6d64..47039a4166 100644
--- a/saleor/graphql/product/enums.py
+++ b/saleor/graphql/product/enums.py
@@ -79,6 +79,7 @@ class AttributeSortField(graphene.Enum):
DASHBOARD_VARIANT_POSITION = "dashboard_variant_position"
DASHBOARD_PRODUCT_POSITION = "dashboard_product_position"
STOREFRONT_SEARCH_POSITION = "storefront_search_position"
+ AVAILABLE_IN_GRID = "available_in_grid"
@property
def description(self):
@@ -102,4 +103,9 @@ class AttributeSortField(graphene.Enum):
return "Sort product attributes by their position in dashboard."
if self == AttributeSortField.STOREFRONT_SEARCH_POSITION:
return "Sort attributes by their position in storefront."
+ if self == AttributeSortField.AVAILABLE_IN_GRID:
+ return (
+ "Sort attributes based on whether they can be displayed "
+ "or not in a product grid."
+ )
raise ValueError("Unsupported enum value: %s" % self.value)
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index b956fc03b0..df34fef889 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -231,4 +231,5 @@ class AttributeFilter(django_filters.FilterSet):
"visible_in_storefront",
"filterable_in_storefront",
"filterable_in_dashboard",
+ "available_in_grid",
]
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 341154e24b..e64c11c87b 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -50,6 +50,9 @@ class AttributeCreateInput(graphene.InputObjectType):
storefront_search_position = graphene.Int(
required=False, description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION
)
+ available_in_grid = graphene.Boolean(
+ required=False, description=AttributeDescriptions.AVAILABLE_IN_GRID
+ )
class AttributeUpdateInput(graphene.InputObjectType):
@@ -81,6 +84,9 @@ class AttributeUpdateInput(graphene.InputObjectType):
storefront_search_position = graphene.Int(
required=False, description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION
)
+ available_in_grid = graphene.Boolean(
+ required=False, description=AttributeDescriptions.AVAILABLE_IN_GRID
+ )
class AttributeAssignInput(graphene.InputObjectType):
diff --git a/saleor/graphql/product/types/attributes.py b/saleor/graphql/product/types/attributes.py
index 97136220e1..10cb48aa32 100644
--- a/saleor/graphql/product/types/attributes.py
+++ b/saleor/graphql/product/types/attributes.py
@@ -97,23 +97,18 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
model_field="values",
)
- value_required = gql_optimizer.field(
- graphene.Boolean(description=AttributeDescriptions.VALUE_REQUIRED),
- model_field="value_required",
+ value_required = graphene.Boolean(description=AttributeDescriptions.VALUE_REQUIRED)
+ visible_in_storefront = graphene.Boolean(
+ description=AttributeDescriptions.VISIBLE_IN_STOREFRONT
)
-
- visible_in_storefront = gql_optimizer.field(
- graphene.Boolean(description=AttributeDescriptions.VISIBLE_IN_STOREFRONT),
- model_field="visible_in_storefront",
+ filterable_in_storefront = graphene.Boolean(
+ description=AttributeDescriptions.FILTERABLE_IN_STOREFRONT
)
-
- filterable_in_storefront = gql_optimizer.field(
- graphene.Boolean(description=AttributeDescriptions.FILTERABLE_IN_STOREFRONT),
- model_field="filterable_in_storefront",
+ filterable_in_dashboard = graphene.Boolean(
+ description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD
)
- filterable_in_dashboard = gql_optimizer.field(
- graphene.Boolean(description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD),
- model_field="filterable_in_dashboard",
+ available_in_grid = graphene.Boolean(
+ description=AttributeDescriptions.AVAILABLE_IN_GRID
)
translation = graphene.Field(
@@ -129,9 +124,8 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
resolver=resolve_translation,
)
- storefront_search_position = gql_optimizer.field(
- graphene.Int(description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION),
- model_field="storefront_search_position",
+ storefront_search_position = graphene.Int(
+ description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION
)
class Meta:
@@ -180,6 +174,11 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
def resolve_storefront_search_position(root: models.Attribute, *_args):
return root.storefront_search_position
+ @staticmethod
+ @permission_required("product.manage_products")
+ def resolve_available_in_grid(root: models.Attribute, *_args):
+ return root.available_in_grid
+
class SelectedAttribute(graphene.ObjectType):
attribute = graphene.Field(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 82eaa9835f..1b96039f1b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -155,6 +155,7 @@ type Attribute implements Node {
visibleInStorefront: Boolean
filterableInStorefront: Boolean
filterableInDashboard: Boolean
+ availableInGrid: Boolean
translation(languageCode: LanguageCodeEnum!): AttributeTranslation
storefrontSearchPosition: Int
}
@@ -211,6 +212,7 @@ input AttributeCreateInput {
filterableInStorefront: Boolean
filterableInDashboard: Boolean
storefrontSearchPosition: Int
+ availableInGrid: Boolean
}
type AttributeDelete {
@@ -224,6 +226,7 @@ input AttributeFilterInput {
visibleInStorefront: Boolean
filterableInStorefront: Boolean
filterableInDashboard: Boolean
+ availableInGrid: Boolean
search: String
}
@@ -250,6 +253,7 @@ enum AttributeSortField {
DASHBOARD_VARIANT_POSITION
DASHBOARD_PRODUCT_POSITION
STOREFRONT_SEARCH_POSITION
+ AVAILABLE_IN_GRID
}
input AttributeSortingInput {
@@ -294,6 +298,7 @@ input AttributeUpdateInput {
filterableInStorefront: Boolean
filterableInDashboard: Boolean
storefrontSearchPosition: Int
+ availableInGrid: Boolean
}
type AttributeUpdateMeta {
diff --git a/saleor/product/migrations/0102_attribute_available_in_grid.py b/saleor/product/migrations/0102_attribute_available_in_grid.py
new file mode 100644
index 0000000000..2e59f12705
--- /dev/null
+++ b/saleor/product/migrations/0102_attribute_available_in_grid.py
@@ -0,0 +1,16 @@
+# Generated by Django 2.2.3 on 2019-07-19 13:43
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("product", "0101_remove_null_sort_order")]
+
+ operations = [
+ migrations.AddField(
+ model_name="attribute",
+ name="available_in_grid",
+ field=models.BooleanField(blank=True, default=True),
+ )
+ ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index cbf7f1fc87..0bdf347275 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -544,6 +544,7 @@ class Attribute(ModelWithMetadata):
filterable_in_dashboard = models.BooleanField(default=True, blank=True)
storefront_search_position = models.IntegerField(default=0, blank=True)
+ available_in_grid = models.BooleanField(default=True, blank=True)
objects = AttributeQuerySet.as_manager()
translated = TranslationProxy()
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 53ece72b92..21a5edae32 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -1354,6 +1354,7 @@ def test_retrieve_product_attributes_input_type(
("filterable_in_storefront", True),
("filterable_in_dashboard", True),
("visible_in_storefront", True),
+ ("available_in_grid", True),
("value_required", False),
("storefront_search_position", 0),
),
@@ -1365,6 +1366,9 @@ def test_retrieving_the_restricted_attributes_restricted(
attribute,
expected_value,
):
+ """Checks if the attributes are restricted and if their default value
+ is the expected one."""
+
attribute = to_camel_case(attribute)
query = (
"""
@@ -1536,6 +1540,22 @@ def test_filter_attributes_if_filterable_in_dashboard(
assert attributes[0]["node"]["slug"] == "size"
+def test_filter_attributes_if_available_in_grid(
+ api_client, color_attribute, size_attribute
+):
+ color_attribute.available_in_grid = False
+ color_attribute.save(update_fields=["available_in_grid"])
+
+ variables = {"filters": {"availableInGrid": True}}
+
+ attributes = get_graphql_content(
+ api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ assert len(attributes) == 1
+ assert attributes[0]["node"]["slug"] == "size"
+
+
ATTRIBUTES_SORT_QUERY = """
query($sortBy: AttributeSortingInput) {
attributes(first: 10, sortBy: $sortBy) {