Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor sort in attributes query
fowczarek05339ecfaf96
diff --git a/saleor/graphql/product/enums.py b/saleor/graphql/product/enums.pyindex dd1a68e98d..706657f41b 100644--- a/saleor/graphql/product/enums.py+++ b/saleor/graphql/product/enums.py@@ -36,47 +36,3 @@ class ProductTypeConfigurable(graphene.Enum):class ProductTypeEnum(graphene.Enum):DIGITAL = "digital"SHIPPABLE = "shippable"---class AttributeSortField(graphene.Enum):- NAME = "name"- SLUG = "slug"- VALUE_REQUIRED = "value_required"- IS_VARIANT_ONLY = "is_variant_only"- VISIBLE_IN_STOREFRONT = "visible_in_storefront"- FILTERABLE_IN_STOREFRONT = "filterable_in_storefront"- FILTERABLE_IN_DASHBOARD = "filterable_in_dashboard"-- 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):- if self == AttributeSortField.NAME:- return "Sort attributes by name."- if self == AttributeSortField.SLUG:- return "Sort attributes by slug."- if self == AttributeSortField.VALUE_REQUIRED:- return "Sort attributes by the value required flag."- if self == AttributeSortField.IS_VARIANT_ONLY:- return "Sort attributes by the variant only flag."- if self == AttributeSortField.VISIBLE_IN_STOREFRONT:- return "Sort attributes by visibility in the storefront."- if self == AttributeSortField.FILTERABLE_IN_STOREFRONT:- return "Sort attributes by the filterable in storefront flag."- if self == AttributeSortField.FILTERABLE_IN_DASHBOARD:- return "Sort attributes by the filterable in dashboard flag."- if self == AttributeSortField.DASHBOARD_VARIANT_POSITION:- return "Sort variant attributes by their position in dashboard."- if self == AttributeSortField.DASHBOARD_PRODUCT_POSITION:- 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.pyindex 2ea8b83322..c8c69a2b73 100644--- a/saleor/graphql/product/filters.py+++ b/saleor/graphql/product/filters.py@@ -92,12 +92,6 @@ def filter_products_by_collections(qs, collections):return qs.filter(collections__in=collections)-def sort_qs(qs, sort_by):- if sort_by:- qs = qs.order_by(sort_by["direction"] + sort_by["field"])- return qs--def filter_products_by_stock_availability(qs, stock_availability):qs = qs.annotate(total_available=Sum("variants__quantity") - Sum("variants__quantity_allocated")diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.pyindex 0d1ee6c063..0bb1c2fbfb 100644--- a/saleor/graphql/product/resolvers.py+++ b/saleor/graphql/product/resolvers.py@@ -16,7 +16,6 @@ from ..utils import (get_nodes,sort_queryset,)-from .enums import AttributeSortFieldfrom .filters import (filter_attributes_by_product_types,filter_products_by_attributes,@@ -25,9 +24,13 @@ from .filters import (filter_products_by_minimal_price,filter_products_by_price,filter_products_by_stock_availability,- sort_qs,)-from .sorters import CategoryOrderField, CollectionOrderField, ProductOrderField+from .sorters import (+ AttributeSortField,+ CategoryOrderField,+ CollectionOrderField,+ ProductOrderField,+)if TYPE_CHECKING:from ..product.types import ProductOrder # noqa@@ -58,13 +61,7 @@ def resolve_attributes(qs = filter_attributes_by_product_types(qs, "in_collection", in_collection)if sort_by:- is_asc = sort_by["direction"] == OrderDirection.ASC.value- if sort_by["field"] == AttributeSortField.DASHBOARD_VARIANT_POSITION.value:- qs = qs.variant_attributes_sorted(is_asc)- elif sort_by["field"] == AttributeSortField.DASHBOARD_PRODUCT_POSITION.value:- qs = qs.product_attributes_sorted(is_asc)- else:- qs = sort_qs(qs, sort_by)+ qs = sort_queryset(qs, sort_by, AttributeSortField)else:qs = qs.order_by("name")diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex 6b6eb30f9e..b80bc1dfc7 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -111,7 +111,7 @@ from .resolvers import (resolve_report_product_sales,)from .scalars import AttributeScalar-from .sorters import CategoryOrder, CollectionOrder, ProductOrder+from .sorters import AttributeSortingInput, CategoryOrder, CollectionOrder, ProductOrderfrom .types import (Attribute,Category,@@ -121,7 +121,6 @@ from .types import (ProductType,ProductVariant,)-from .types.attributes import AttributeSortingInputclass ProductQueries(graphene.ObjectType):@@ -156,9 +155,7 @@ class ProductQueries(graphene.ObjectType):),),filter=AttributeFilterInput(description="Filtering options for attributes."),- sort_by=graphene.Argument(- AttributeSortingInput, description="Sorting options for attributes."- ),+ sort_by=AttributeSortingInput(description="Sorting options for attributes."),)attribute = graphene.Field(Attribute,diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.pyindex 4af9b06979..af56a166df 100644--- a/saleor/graphql/product/sorters.py+++ b/saleor/graphql/product/sorters.py@@ -2,9 +2,77 @@ import graphenefrom django.db.models import Count, QuerySetfrom ...product.models import Category, Product+from ..core.enums import OrderDirectionfrom ..core.types import SortInputObjectType+class AttributeSortField(graphene.Enum):+ NAME = "name"+ SLUG = "slug"+ VALUE_REQUIRED = "value_required"+ IS_VARIANT_ONLY = "is_variant_only"+ VISIBLE_IN_STOREFRONT = "visible_in_storefront"+ FILTERABLE_IN_STOREFRONT = "filterable_in_storefront"+ FILTERABLE_IN_DASHBOARD = "filterable_in_dashboard"++ 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):+ # pylint: disable=no-member+ descrtiptions = {+ AttributeSortField.NAME.name: "name",+ AttributeSortField.SLUG.name: "slug",+ AttributeSortField.VALUE_REQUIRED.name: "the value required flag",+ AttributeSortField.IS_VARIANT_ONLY.name: "the variant only flag",+ AttributeSortField.VISIBLE_IN_STOREFRONT.name: (+ "visibility in the storefront"+ ),+ AttributeSortField.FILTERABLE_IN_STOREFRONT.name: (+ "the filterable in storefront flag"+ ),+ AttributeSortField.FILTERABLE_IN_DASHBOARD.name: (+ "the filterable in dashboard flag"+ ),+ AttributeSortField.STOREFRONT_SEARCH_POSITION.name: (+ "their position in storefront"+ ),+ }+ if self.name in descrtiptions:+ return f"Sort attributes by {descrtiptions[self.name]}."+ if self == AttributeSortField.DASHBOARD_VARIANT_POSITION:+ return "Sort variant attributes by their position in dashboard."+ if self == AttributeSortField.DASHBOARD_PRODUCT_POSITION:+ return "Sort product attributes by their position in dashboard."+ 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)++ @staticmethod+ def sort_by_dashboard_variant_position(queryset: QuerySet, sort_by: dict):+ # pylint: disable=no-member+ is_asc = sort_by["direction"] == OrderDirection.ASC.value+ return queryset.variant_attributes_sorted(is_asc)++ @staticmethod+ def sort_by_dashboard_product_position(queryset: QuerySet, sort_by: dict):+ # pylint: disable=no-member+ is_asc = sort_by["direction"] == OrderDirection.ASC.value+ return queryset.product_attributes_sorted(is_asc)+++class AttributeSortingInput(SortInputObjectType):+ class Meta:+ sort_enum = AttributeSortField+ type_name = "attributes"++class CategoryOrderField(graphene.Enum):NAME = "name"PRODUCT_COUNT = "product_count"@@ -38,7 +106,7 @@ class CategoryOrderField(graphene.Enum):class CategoryOrder(SortInputObjectType):class Meta:sort_enum = CategoryOrderField- type_name = "category"+ type_name = "categories"class CollectionOrderField(graphene.Enum):@@ -68,7 +136,7 @@ class CollectionOrderField(graphene.Enum):class CollectionOrder(SortInputObjectType):class Meta:sort_enum = CollectionOrderField- type_name = "collection"+ type_name = "collections"class ProductOrderField(graphene.Enum):@@ -108,4 +176,4 @@ class ProductOrder(SortInputObjectType):class Meta:sort_enum = ProductOrderField- type_name = "product"+ type_name = "products"diff --git a/saleor/graphql/product/types/attributes.py b/saleor/graphql/product/types/attributes.pyindex 500c3fdd3f..83c2c427d4 100644--- a/saleor/graphql/product/types/attributes.py+++ b/saleor/graphql/product/types/attributes.py@@ -7,14 +7,13 @@ from graphene import relayfrom ....product import modelsfrom ....product.utils.attributes import AttributeAssignmentTypefrom ...core.connection import CountableDjangoObjectType-from ...core.enums import OrderDirectionfrom ...core.resolvers import resolve_meta, resolve_private_metafrom ...core.types import MetadataObjectTypefrom ...decorators import permission_requiredfrom ...translations.fields import TranslationFieldfrom ...translations.types import AttributeTranslation, AttributeValueTranslationfrom ..descriptions import AttributeDescriptions, AttributeValueDescriptions-from ..enums import AttributeInputTypeEnum, AttributeSortField, AttributeValueType+from ..enums import AttributeInputTypeEnum, AttributeValueTypeCOLOR_PATTERN = r"^(#[0-9a-fA-F]{3}|#(?:[0-9a-fA-F]{2}){2,4}|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d\.]+%?\))$" # noqacolor_pattern = re.compile(COLOR_PATTERN)@@ -30,19 +29,6 @@ def resolve_attribute_value_type(attribute_value):return AttributeValueType.STRING-class AttributeSortingInput(graphene.InputObjectType):- field = graphene.Argument(- AttributeSortField,- required=True,- description="Sort attributes by the selected field.",- )- direction = graphene.Argument(- OrderDirection,- required=True,- description="Specifies the direction in which to sort the attributes.",- )--class AttributeValue(CountableDjangoObjectType):name = graphene.String(description=AttributeValueDescriptions.NAME)slug = graphene.String(description=AttributeValueDescriptions.SLUG)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex a6d2a7b34c..ecfa79b081 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -318,8 +318,8 @@ enum AttributeSortField {}input AttributeSortingInput {- field: AttributeSortField!direction: OrderDirection!+ field: AttributeSortField}type AttributeTranslatableContent implements Node {