Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change naming convetntion
fowczarek259ff60606e9
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.pyindex 0bb1c2fbfb..abe81dcb38 100644--- a/saleor/graphql/product/resolvers.py+++ b/saleor/graphql/product/resolvers.py@@ -27,8 +27,8 @@ from .filters import ()from .sorters import (AttributeSortField,- CategoryOrderField,- CollectionOrderField,+ CategorySortField,+ CollectionSortField,ProductOrderField,)@@ -74,7 +74,7 @@ def resolve_categories(info, query, level=None, sort_by=None, **_kwargs):if level is not None:qs = qs.filter(level=level)qs = filter_by_query_param(qs, query, CATEGORY_SEARCH_FIELDS)- qs = sort_queryset(qs, sort_by, CategoryOrderField)+ qs = sort_queryset(qs, sort_by, CategorySortField)qs = qs.distinct()return gql_optimizer.query(qs, info)@@ -83,7 +83,7 @@ def resolve_collections(info, query, sort_by=None, **_kwargs):user = info.context.userqs = models.Collection.objects.visible_to_user(user)qs = filter_by_query_param(qs, query, COLLECTION_SEARCH_FIELDS)- qs = sort_queryset(qs, sort_by, CollectionOrderField)+ qs = sort_queryset(qs, sort_by, CollectionSortField)return gql_optimizer.query(qs, info)diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex b80bc1dfc7..49c16ff990 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -111,7 +111,12 @@ from .resolvers import (resolve_report_product_sales,)from .scalars import AttributeScalar-from .sorters import AttributeSortingInput, CategoryOrder, CollectionOrder, ProductOrder+from .sorters import (+ AttributeSortingInput,+ CategorySortingInput,+ CollectionSortingInput,+ ProductOrder,+)from .types import (Attribute,Category,@@ -168,7 +173,7 @@ class ProductQueries(graphene.ObjectType):Category,query=graphene.String(description=DESCRIPTIONS["category"]),filter=CategoryFilterInput(description="Filtering options for categories."),- sort_by=CategoryOrder(description="Sort categories."),+ sort_by=CategorySortingInput(description="Sort categories."),level=graphene.Argument(graphene.Int,description="Filter categories by the nesting level in the category tree.",@@ -192,7 +197,7 @@ class ProductQueries(graphene.ObjectType):collections = FilterInputConnectionField(Collection,filter=CollectionFilterInput(description="Filtering options for collections."),- sort_by=CollectionOrder(description="Sort collections."),+ sort_by=CollectionSortingInput(description="Sort collections."),query=graphene.String(description=DESCRIPTIONS["collection"]),description="List of the shop's collections.",)diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.pyindex af56a166df..022fa0d954 100644--- a/saleor/graphql/product/sorters.py+++ b/saleor/graphql/product/sorters.py@@ -23,7 +23,7 @@ class AttributeSortField(graphene.Enum):@propertydef description(self):# pylint: disable=no-member- descrtiptions = {+ descriptions = {AttributeSortField.NAME.name: "name",AttributeSortField.SLUG.name: "slug",AttributeSortField.VALUE_REQUIRED.name: "the value required flag",@@ -41,8 +41,8 @@ class AttributeSortField(graphene.Enum):"their position in storefront"),}- if self.name in descrtiptions:- return f"Sort attributes by {descrtiptions[self.name]}."+ if self.name in descriptions:+ return f"Sort attributes by {descriptions[self.name]}."if self == AttributeSortField.DASHBOARD_VARIANT_POSITION:return "Sort variant attributes by their position in dashboard."if self == AttributeSortField.DASHBOARD_PRODUCT_POSITION:@@ -55,13 +55,17 @@ class AttributeSortField(graphene.Enum):raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def sort_by_dashboard_variant_position(queryset: QuerySet, sort_by: dict):+ def sort_by_dashboard_variant_position(+ queryset: QuerySet, sort_by: SortInputObjectType+ ) -> QuerySet:# pylint: disable=no-memberis_asc = sort_by["direction"] == OrderDirection.ASC.valuereturn queryset.variant_attributes_sorted(is_asc)@staticmethod- def sort_by_dashboard_product_position(queryset: QuerySet, sort_by: dict):+ def sort_by_dashboard_product_position(+ queryset: QuerySet, sort_by: SortInputObjectType+ ) -> QuerySet:# pylint: disable=no-memberis_asc = sort_by["direction"] == OrderDirection.ASC.valuereturn queryset.product_attributes_sorted(is_asc)@@ -73,7 +77,7 @@ class AttributeSortingInput(SortInputObjectType):type_name = "attributes"-class CategoryOrderField(graphene.Enum):+class CategorySortField(graphene.Enum):NAME = "name"PRODUCT_COUNT = "product_count"SUBCATEGORY_COUNT = "subcategory_count"@@ -82,34 +86,38 @@ class CategoryOrderField(graphene.Enum):def description(self):# pylint: disable=no-memberif self in [- CategoryOrderField.NAME,- CategoryOrderField.PRODUCT_COUNT,- CategoryOrderField.SUBCATEGORY_COUNT,+ CategorySortField.NAME,+ CategorySortField.PRODUCT_COUNT,+ CategorySortField.SUBCATEGORY_COUNT,]:sort_name = self.name.lower().replace("_", " ")return f"Sort categories by {sort_name}."raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def sort_by_product_count(queryset: QuerySet, sort_by: dict):+ def sort_by_product_count(+ queryset: QuerySet, sort_by: SortInputObjectType+ ) -> QuerySet:return Category.tree.add_related_count(queryset, Product, "category", "product_count", cumulative=True).order_by(f"{sort_by.direction}product_count")@staticmethod- def sort_by_subcategory_count(queryset: QuerySet, sort_by: dict):+ def sort_by_subcategory_count(+ queryset: QuerySet, sort_by: SortInputObjectType+ ) -> QuerySet:return queryset.annotate(subcategory_count=Count("children__id")).order_by(f"{sort_by.direction}subcategory_count", "pk")-class CategoryOrder(SortInputObjectType):+class CategorySortingInput(SortInputObjectType):class Meta:- sort_enum = CategoryOrderField+ sort_enum = CategorySortFieldtype_name = "categories"-class CollectionOrderField(graphene.Enum):+class CollectionSortField(graphene.Enum):NAME = "name"AVAILABILITY = "is_published"PRODUCT_COUNT = "product_count"@@ -118,24 +126,26 @@ class CollectionOrderField(graphene.Enum):def description(self):# pylint: disable=no-memberif self in [- CollectionOrderField.NAME,- CollectionOrderField.AVAILABILITY,- CollectionOrderField.PRODUCT_COUNT,+ CollectionSortField.NAME,+ CollectionSortField.AVAILABILITY,+ CollectionSortField.PRODUCT_COUNT,]:sort_name = self.name.lower().replace("_", " ")return f"Sort collections by {sort_name}."raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def sort_by_product_count(queryset: QuerySet, sort_by: dict):+ def sort_by_product_count(+ queryset: QuerySet, sort_by: SortInputObjectType+ ) -> QuerySet:return queryset.annotate(product_count=Count("collectionproduct__id")).order_by(f"{sort_by.direction}product_count", "slug")-class CollectionOrder(SortInputObjectType):+class CollectionSortingInput(SortInputObjectType):class Meta:- sort_enum = CollectionOrderField+ sort_enum = CollectionSortFieldtype_name = "collections"@@ -150,7 +160,7 @@ class ProductOrderField(graphene.Enum):@propertydef description(self):# pylint: disable=no-member- descrtiptions = {+ descriptions = {ProductOrderField.NAME.name: "name",ProductOrderField.PRICE.name: "price",ProductOrderField.TYPE.name: "type",@@ -160,8 +170,8 @@ class ProductOrderField(graphene.Enum):ProductOrderField.DATE.name: "update date",ProductOrderField.PUBLISHED.name: "publication status",}- if self.name in descrtiptions:- return f"Sort products by {descrtiptions[self.name]}."+ if self.name in descriptions:+ return f"Sort products by {descriptions[self.name]}."raise ValueError("Unsupported enum value: %s" % self.value)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex ecfa79b081..2dba873fcc 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -570,17 +570,17 @@ input CategoryInput {backgroundImageAlt: String}-input CategoryOrder {- direction: OrderDirection!- field: CategoryOrderField-}--enum CategoryOrderField {+enum CategorySortField {NAMEPRODUCT_COUNTSUBCATEGORY_COUNT}+input CategorySortingInput {+ direction: OrderDirection!+ field: CategorySortField+}+type CategoryTranslatableContent implements Node {seoTitle: StringseoDescription: String@@ -944,17 +944,6 @@ input CollectionInput {publicationDate: Date}-input CollectionOrder {- direction: OrderDirection!- field: CollectionOrderField-}--enum CollectionOrderField {- NAME- AVAILABILITY- PRODUCT_COUNT-}-enum CollectionPublished {PUBLISHEDHIDDEN@@ -972,6 +961,17 @@ type CollectionReorderProducts {productErrors: [ProductError!]}+enum CollectionSortField {+ NAME+ AVAILABILITY+ PRODUCT_COUNT+}++input CollectionSortingInput {+ direction: OrderDirection!+ field: CollectionSortField+}+type CollectionTranslatableContent implements Node {seoTitle: StringseoDescription: String@@ -3428,10 +3428,10 @@ type Query {digitalContents(before: String, after: String, first: Int, last: Int): DigitalContentCountableConnectionattributes(query: String, inCategory: ID, inCollection: ID, filter: AttributeFilterInput, sortBy: AttributeSortingInput, before: String, after: String, first: Int, last: Int): AttributeCountableConnectionattribute(id: ID!): Attribute- categories(query: String, filter: CategoryFilterInput, sortBy: CategoryOrder, level: Int, before: String, after: String, first: Int, last: Int): CategoryCountableConnection+ categories(query: String, filter: CategoryFilterInput, sortBy: CategorySortingInput, level: Int, before: String, after: String, first: Int, last: Int): CategoryCountableConnectioncategory(id: ID!): Categorycollection(id: ID!): Collection- collections(filter: CollectionFilterInput, sortBy: CollectionOrder, query: String, before: String, after: String, first: Int, last: Int): CollectionCountableConnection+ collections(filter: CollectionFilterInput, sortBy: CollectionSortingInput, query: String, before: String, after: String, first: Int, last: Int): CollectionCountableConnectionproduct(id: ID!): Productproducts(filter: ProductFilterInput, attributes: [AttributeScalar], categories: [ID], collections: [ID], sortBy: ProductOrder, stockAvailability: StockAvailability, query: String, before: String, after: String, first: Int, last: Int): ProductCountableConnectionproductType(id: ID!): ProductTypediff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.pyindex 388a0da1a9..dc1d4dc84b 100644--- a/saleor/graphql/utils.py+++ b/saleor/graphql/utils.py@@ -123,7 +123,9 @@ def filter_by_query_param(queryset, query, search_fields):return queryset-def sort_queryset(queryset: QuerySet, sort_by: dict, sort_enum: SortInputObjectType):+def sort_queryset(+ queryset: QuerySet, sort_by: SortInputObjectType, sort_enum: graphene.Enum+) -> QuerySet:"""Sort queryset according to given parameters.Keyword Arguments:diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex fe843bbd0c..2882dd0534 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -2642,7 +2642,7 @@ def test_collections_query_with_filter(QUERY_COLLECTIONS_WITH_SEARCH = """- query ($sort_by: CollectionOrder!) {+ query ($sort_by: CollectionSortingInput!) {collections(first:5, sortBy: $sort_by) {edges{node{@@ -2725,7 +2725,7 @@ def test_categories_query_with_filter(QUERY_CATEGORIES_WITH_SEARCH = """- query ($sort_by: CategoryOrder!) {+ query ($sort_by: CategorySortingInput!) {categories(first:5, sortBy: $sort_by) {edges{node{