Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add sorting by LAST_MODIFIED_AT field to GraphQL schema (#9245)
Rafał Pitoń3f773c3890ae
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0e591152e4..1c43efa578 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,16 @@ All notable, unreleased changes to this project will be documented in this file.### Other changes+- Add `CREATED_AT` and `LAST_MODIFIED_AT` sorting to some GraphQL fields - #9245 by @rafalp+ - Added `LAST_MODIFIED_AT` sort option to `ExportFileSortingInput`+ - Added `CREATED_AT` and `LAST_MODIFIED_AT` sort options to `OrderSortingInput` type+ - Added `LAST_MODIFIED_AT` and `PUBLISHED_AT` sort options to `ProductOrder` type+ - Added `CREATED_AT` and `LAST_MODIFIED_AT` sort options to `SaleSortingInput` type+ - Added `CREATED_AT` and `LAST_MODIFIED_AT` sort options to `UserSortingInput` type+ - Added `ProductVariantSortingInput` type with `LAST_MODIFIED_AT` sort option+ - Deprecated `UPDATED_AT` sort option on `ExportFileSortingInput`+ - Deprecated `LAST_MODIFIED` and `PUBLICATION_DATE` sort options on `ProductOrder` type+ - Deprecated `CREATION_DATE` sort option on `OrderSortingInput` type- Drop wishlist models - #9313 by @maarcingebala# 3.1.2diff --git a/saleor/graphql/account/sorters.py b/saleor/graphql/account/sorters.pyindex 17c640cc14..d195a2a775 100644--- a/saleor/graphql/account/sorters.py+++ b/saleor/graphql/account/sorters.py@@ -9,6 +9,8 @@ class UserSortField(graphene.Enum):LAST_NAME = ["last_name", "first_name", "pk"]EMAIL = ["email"]ORDER_COUNT = ["order_count", "email"]+ CREATED_AT = ["date_joined", "pk"]+ LAST_MODIFIED_AT = ["updated_at", "pk"]@propertydef description(self):diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.pyindex bd1cfd45b2..58c11078db 100644--- a/saleor/graphql/account/tests/test_account.py+++ b/saleor/graphql/account/tests/test_account.py@@ -4884,12 +4884,16 @@ QUERY_CUSTOMERS_WITH_SORT = """({"field": "EMAIL", "direction": "DESC"}, ["Joe", "Leslie", "John"]),({"field": "ORDER_COUNT", "direction": "ASC"}, ["John", "Leslie", "Joe"]),({"field": "ORDER_COUNT", "direction": "DESC"}, ["Joe", "Leslie", "John"]),+ ({"field": "CREATED_AT", "direction": "ASC"}, ["John", "Joe", "Leslie"]),+ ({"field": "CREATED_AT", "direction": "DESC"}, ["Leslie", "Joe", "John"]),+ ({"field": "LAST_MODIFIED_AT", "direction": "ASC"}, ["Leslie", "John", "Joe"]),+ ({"field": "LAST_MODIFIED_AT", "direction": "DESC"}, ["Joe", "John", "Leslie"]),],)def test_query_customers_with_sort(customer_sort, result_order, staff_api_client, permission_manage_users, channel_USD):- User.objects.bulk_create(+ users = User.objects.bulk_create([User(first_name="John",@@ -4914,9 +4918,15 @@ def test_query_customers_with_sort(),])++ users[2].save()+ users[0].save()+ users[1].save()+Order.objects.create(user=User.objects.get(email="[email protected]"), channel=channel_USD)+variables = {"sort_by": customer_sort}staff_api_client.user.user_permissions.add(permission_manage_users)response = staff_api_client.post_graphql(QUERY_CUSTOMERS_WITH_SORT, variables)diff --git a/saleor/graphql/csv/sorters.py b/saleor/graphql/csv/sorters.pyindex c4640b3941..bf5e72af0b 100644--- a/saleor/graphql/csv/sorters.py+++ b/saleor/graphql/csv/sorters.py@@ -1,5 +1,6 @@import graphene+from ..core.descriptions import DEPRECATED_IN_3X_INPUTfrom ..core.types import SortInputObjectType@@ -7,12 +8,22 @@ class ExportFileSortField(graphene.Enum):STATUS = ["status"]CREATED_AT = ["created_at"]UPDATED_AT = ["updated_at"]+ LAST_MODIFIED_AT = ["updated_at"]@propertydef description(self):- if self.name in ExportFileSortField.__enum__._member_names_:- sort_name = self.name.lower().replace("_", " ")- return f"Sort export file by {sort_name}."+ # pylint: disable=no-member+ descriptions = {+ ExportFileSortField.STATUS.name: "status.",+ ExportFileSortField.CREATED_AT.name: "creation date.",+ ExportFileSortField.UPDATED_AT.name: (+ f"update date. {DEPRECATED_IN_3X_INPUT}"+ ),+ ExportFileSortField.LAST_MODIFIED_AT.name: "update date.",+ }++ for self.name in ExportFileSortField.__enum__._member_names_:+ return f"Sort export file by {descriptions[self.name]}"raise ValueError("Unsupported enum value: %s" % self.value)diff --git a/saleor/graphql/csv/tests/queries/test_export_files.py b/saleor/graphql/csv/tests/queries/test_export_files.pyindex 0d7ed73ec4..0794a25c9a 100644--- a/saleor/graphql/csv/tests/queries/test_export_files.py+++ b/saleor/graphql/csv/tests/queries/test_export_files.py@@ -245,7 +245,9 @@ def test_sort_export_files_query_by_created_at_date()[email protected]("sort_by", ["UPDATED_AT", "LAST_MODIFIED_AT"])def test_sort_export_files_query_by_updated_at_date(+ sort_by,staff_api_client,user_export_file,permission_manage_products,@@ -264,7 +266,7 @@ def test_sort_export_files_query_by_updated_at_date(second_export_file.save()query = SORT_EXPORT_FILES_QUERY- variables = {"sortBy": {"field": "UPDATED_AT", "direction": "ASC"}}+ variables = {"sortBy": {"field": sort_by, "direction": "ASC"}}response = staff_api_client.post_graphql(query,diff --git a/saleor/graphql/discount/sorters.py b/saleor/graphql/discount/sorters.pyindex 6fbd850b47..90a272bf89 100644--- a/saleor/graphql/discount/sorters.py+++ b/saleor/graphql/discount/sorters.py@@ -10,6 +10,8 @@ class SaleSortField(graphene.Enum):END_DATE = ["end_date", "name", "pk"]VALUE = ["value", "name", "pk"]TYPE = ["type", "name", "pk"]+ CREATED_AT = ["created", "name", "pk"]+ LAST_MODIFIED_AT = ["updated_at", "name", "pk"]@propertydef description(self):diff --git a/saleor/graphql/discount/tests/test_sale_sorting_with_channels.py b/saleor/graphql/discount/tests/test_sale_sorting_with_channels.pyindex a93d3cc06a..16151d0f77 100644--- a/saleor/graphql/discount/tests/test_sale_sorting_with_channels.py+++ b/saleor/graphql/discount/tests/test_sale_sorting_with_channels.py@@ -75,6 +75,13 @@ def sales_for_sorting_with_channels(db, channel_USD, channel_PLN):),])++ sales[4].save()+ sales[2].save()+ sales[0].save()+ sales[1].save()+ sales[3].save()+return sales@@ -125,6 +132,22 @@ def test_sales_with_sorting_and_without_channel({"field": "VALUE", "direction": "DESC"},["Sale2", "Sale3", "Sale15", "Sale1"],),+ (+ {"field": "CREATED_AT", "direction": "ASC"},+ ["Sale1", "Sale2", "Sale3", "Sale15"],+ ),+ (+ {"field": "CREATED_AT", "direction": "DESC"},+ ["Sale15", "Sale3", "Sale2", "Sale1"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "ASC"},+ ["Sale15", "Sale3", "Sale1", "Sale2"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "DESC"},+ ["Sale2", "Sale1", "Sale3", "Sale15"],+ ),],)def test_sales_with_sorting_and_channel_USD(@@ -164,6 +187,22 @@ def test_sales_with_sorting_and_channel_USD({"field": "VALUE", "direction": "DESC"},["Sale1", "Sale15", "Sale4", "Sale2"],),+ (+ {"field": "CREATED_AT", "direction": "ASC"},+ ["Sale1", "Sale2", "Sale4", "Sale15"],+ ),+ (+ {"field": "CREATED_AT", "direction": "DESC"},+ ["Sale15", "Sale4", "Sale2", "Sale1"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "ASC"},+ ["Sale15", "Sale1", "Sale2", "Sale4"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "DESC"},+ ["Sale4", "Sale2", "Sale1", "Sale15"],+ ),],)def test_sales_with_sorting_and_channel_PLN(diff --git a/saleor/graphql/order/sorters.py b/saleor/graphql/order/sorters.pyindex 4a561d64a1..543558cfbf 100644--- a/saleor/graphql/order/sorters.py+++ b/saleor/graphql/order/sorters.py@@ -2,21 +2,35 @@ import graphenefrom django.db.models import CharField, ExpressionWrapper, OuterRef, QuerySet, Subqueryfrom ...payment.models import Payment+from ..core.descriptions import DEPRECATED_IN_3X_INPUTfrom ..core.types import SortInputObjectTypeclass OrderSortField(graphene.Enum):NUMBER = ["pk"]CREATION_DATE = ["created", "status", "pk"]+ CREATED_AT = ["created", "status", "pk"]+ LAST_MODIFIED_AT = ["updated_at", "status", "pk"]CUSTOMER = ["billing_address__last_name", "billing_address__first_name", "pk"]PAYMENT = ["last_charge_status", "status", "pk"]FULFILLMENT_STATUS = ["status", "user_email", "pk"]@propertydef description(self):+ # pylint: disable=no-member+ descriptions = {+ OrderSortField.CREATION_DATE.name: (+ f"creation date. {DEPRECATED_IN_3X_INPUT}"+ ),+ }+if self.name in OrderSortField.__enum__._member_names_:+ if self.name in descriptions:+ return f"Sort orders by {descriptions[self.name]}"+sort_name = self.name.lower().replace("_", " ")return f"Sort orders by {sort_name}."+raise ValueError("Unsupported enum value: %s" % self.value)@staticmethoddiff --git a/saleor/graphql/order/tests/test_pagination.py b/saleor/graphql/order/tests/test_pagination.pyindex 447c40fa2d..b6c03e4e6a 100644--- a/saleor/graphql/order/tests/test_pagination.py+++ b/saleor/graphql/order/tests/test_pagination.py@@ -608,6 +608,10 @@ def test_draft_orders_query_pagination_with_filter_search_by_id(({"field": "CUSTOMER", "direction": "DESC"}, [1, 0]),({"field": "FULFILLMENT_STATUS", "direction": "ASC"}, [2, 1]),({"field": "FULFILLMENT_STATUS", "direction": "DESC"}, [0, 1]),+ ({"field": "CREATED_AT", "direction": "ASC"}, [1, 0]),+ ({"field": "CREATED_AT", "direction": "DESC"}, [2, 0]),+ ({"field": "LAST_MODIFIED_AT", "direction": "ASC"}, [2, 0]),+ ({"field": "LAST_MODIFIED_AT", "direction": "DESC"}, [1, 0]),],)def test_query_orders_pagination_with_sort(@@ -654,6 +658,11 @@ def test_query_orders_pagination_with_sort(channel=channel_USD,))++ created_orders[2].save()+ created_orders[0].save()+ created_orders[1].save()+page_size = 2variables = {"first": page_size, "after": None, "sortBy": order_sort}staff_api_client.user.user_permissions.add(permission_manage_orders)diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex 247b20d669..aa72a33a2c 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -110,6 +110,7 @@ from .sorters import (CollectionSortingInput,ProductOrder,ProductTypeSortingInput,+ ProductVariantSortingInput,)from .types import (Category,@@ -236,6 +237,7 @@ class ProductQueries(graphene.ObjectType):filter=ProductVariantFilterInput(description="Filtering options for product variant."),+ sort_by=ProductVariantSortingInput(description="Sort products variants."),description="List of product variants.",)report_product_sales = ConnectionField(diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.pyindex d52c728d93..e55c25118e 100644--- a/saleor/graphql/product/sorters.py+++ b/saleor/graphql/product/sorters.py@@ -131,6 +131,8 @@ class ProductOrderField(graphene.Enum):TYPE = ["product_type__name", "name", "slug"]PUBLISHED = ["is_published", "name", "slug"]PUBLICATION_DATE = ["publication_date", "name", "slug"]+ PUBLISHED_AT = ["publication_date", "name", "slug"]+ LAST_MODIFIED_AT = ["updated_at", "name", "slug"]COLLECTION = ["sort_order"]RATING = ["rating", "name", "slug"]@@ -149,9 +151,16 @@ class ProductOrderField(graphene.Enum):ProductOrderField.MINIMAL_PRICE.name: ("a minimal price of a product's variant."),- ProductOrderField.DATE.name: "update date.",+ ProductOrderField.DATE.name: f"update date. {DEPRECATED_IN_3X_INPUT}",ProductOrderField.PUBLISHED.name: "publication status.",- ProductOrderField.PUBLICATION_DATE.name: "publication date.",+ ProductOrderField.PUBLICATION_DATE.name: (+ f"publication date. {DEPRECATED_IN_3X_INPUT}"+ ),+ ProductOrderField.LAST_MODIFIED.name: (+ f"update date. {DEPRECATED_IN_3X_INPUT}"+ ),+ ProductOrderField.PUBLISHED_AT.name: "publication date.",+ ProductOrderField.LAST_MODIFIED_AT.name: "update date.",ProductOrderField.RATING.name: "rating.",}if self.name in descriptions:@@ -190,6 +199,10 @@ class ProductOrderField(graphene.Enum):@staticmethoddef qs_with_publication_date(queryset: QuerySet, channel_slug: str) -> QuerySet:+ return ProductOrderField.qs_with_published_at(queryset, channel_slug)++ @staticmethod+ def qs_with_published_at(queryset: QuerySet, channel_slug: str) -> QuerySet:subquery = Subquery(ProductChannelListing.objects.filter(product_id=OuterRef("pk"), channel__slug=str(channel_slug)@@ -228,6 +241,25 @@ class ProductOrder(ChannelSortInputObjectType):sort_enum = ProductOrderField+class ProductVariantSortField(graphene.Enum):+ LAST_MODIFIED_AT = ["updated_at", "name", "pk"]++ @property+ def description(self):+ # pylint: disable=no-member+ if self.name in ProductVariantSortField.__enum__._member_names_:+ sort_name = self.name.lower().replace("_", " ")+ return f"Sort products variants by {sort_name}."++ raise ValueError("Unsupported enum value: %s" % self.value)+++class ProductVariantSortingInput(SortInputObjectType):+ class Meta:+ sort_enum = ProductVariantSortField+ type_name = "productVariants"++class ProductTypeSortField(graphene.Enum):NAME = ["name", "slug"]DIGITAL = ["is_digital", "name", "slug"]diff --git a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.pyindex 634f28bdc1..57abe86688 100644--- a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py+++ b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py@@ -124,26 +124,31 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):product=products[0],sku=str(uuid.uuid4()).replace("-", ""),track_inventory=True,+ name="XS",),ProductVariant(product=products[1],sku=str(uuid.uuid4()).replace("-", ""),track_inventory=True,+ name="S",),ProductVariant(product=products[2],sku=str(uuid.uuid4()).replace("-", ""),track_inventory=True,+ name="M",),ProductVariant(product=products[3],sku=str(uuid.uuid4()).replace("-", ""),track_inventory=True,+ name="L",),ProductVariant(product=products[4],sku=str(uuid.uuid4()).replace("-", ""),track_inventory=True,+ name="XL",),])@@ -200,6 +205,19 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):),])++ products[3].save()+ products[4].save()+ products[0].save()+ products[2].save()+ products[1].save()++ variants[2].save()+ variants[0].save()+ variants[4].save()+ variants[1].save()+ variants[3].save()+return products@@ -283,6 +301,22 @@ def test_products_with_sorting_and_without_channel({"field": "PUBLICATION_DATE", "direction": "DESC"},["Product1", "Product2", "ProductProduct1", "ProductProduct2"],),+ (+ {"field": "PUBLISHED_AT", "direction": "ASC"},+ ["ProductProduct2", "ProductProduct1", "Product2", "Product1"],+ ),+ (+ {"field": "PUBLISHED_AT", "direction": "DESC"},+ ["Product1", "Product2", "ProductProduct1", "ProductProduct2"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "ASC"},+ ["Product2", "Product1", "ProductProduct2", "ProductProduct1"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "DESC"},+ ["ProductProduct1", "ProductProduct2", "Product1", "Product2"],+ ),],)def test_products_with_sorting_and_channel_USD(@@ -338,6 +372,30 @@ def test_products_with_sorting_and_channel_USD({"field": "MINIMAL_PRICE", "direction": "DESC"},["Product1", "Product3", "ProductProduct2", "ProductProduct1"],),+ (+ {"field": "PUBLICATION_DATE", "direction": "ASC"},+ ["Product3", "ProductProduct1", "ProductProduct2", "Product1"],+ ),+ (+ {"field": "PUBLICATION_DATE", "direction": "DESC"},+ ["Product1", "ProductProduct2", "ProductProduct1", "Product3"],+ ),+ (+ {"field": "PUBLISHED_AT", "direction": "ASC"},+ ["Product3", "ProductProduct1", "ProductProduct2", "Product1"],+ ),+ (+ {"field": "PUBLISHED_AT", "direction": "DESC"},+ ["Product1", "ProductProduct2", "ProductProduct1", "Product3"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "ASC"},+ ["Product3", "Product1", "ProductProduct2", "ProductProduct1"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "DESC"},+ ["ProductProduct1", "ProductProduct2", "Product1", "Product3"],+ ),],)def test_products_with_sorting_and_channel_PLN(@@ -635,3 +693,94 @@ def test_product_query_with_filter_updated_at(variants = content["data"]["products"]["edges"]assert len(variants) == count+++GET_SORTED_VARIANTS_QUERY = """+query Variants($sortBy: ProductVariantSortingInput, $channel: String) {+ productVariants(first: 10, sortBy: $sortBy, channel: $channel) {+ edges {+ node {+ name+ }+ }+ }+}+"""+++ "sort_by, variants_order",+ [+ (+ {"field": "LAST_MODIFIED_AT", "direction": "ASC"},+ ["M", "XS", "S", "L"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "DESC"},+ ["L", "S", "XS", "M"],+ ),+ ],+)+def test_products_variants_with_sorting_and_channel_USD(+ sort_by,+ variants_order,+ staff_api_client,+ permission_manage_products,+ products_for_sorting_with_channels,+ channel_USD,+):+ # given+ variables = {"sortBy": sort_by, "channel": channel_USD.slug}++ # when+ response = staff_api_client.post_graphql(+ GET_SORTED_VARIANTS_QUERY,+ variables,+ permissions=[permission_manage_products],+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ products_nodes = content["data"]["productVariants"]["edges"]+ for index, product_name in enumerate(variants_order):+ assert product_name == products_nodes[index]["node"]["name"]+++ "sort_by, variants_order",+ [+ (+ {"field": "LAST_MODIFIED_AT", "direction": "ASC"},+ ["M", "XS", "XL", "S"],+ ),+ (+ {"field": "LAST_MODIFIED_AT", "direction": "DESC"},+ ["S", "XL", "XS", "M"],+ ),+ ],+)+def test_products_variants_with_sorting_and_channel_PLN(+ sort_by,+ variants_order,+ staff_api_client,+ permission_manage_products,+ products_for_sorting_with_channels,+ channel_PLN,+):+ # given+ variables = {"sortBy": sort_by, "channel": channel_PLN.slug}++ # when+ response = staff_api_client.post_graphql(+ GET_SORTED_VARIANTS_QUERY,+ variables,+ permissions=[permission_manage_products],+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ products_nodes = content["data"]["productVariants"]["edges"]+ for index, product_name in enumerate(variants_order):+ assert product_name == products_nodes[index]["node"]["name"]diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 47c5efe0b3..baf81c55b0 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2189,6 +2189,7 @@ enum ExportFileSortField {STATUSCREATED_ATUPDATED_AT+ LAST_MODIFIED_AT}input ExportFileSortingInput {@@ -4706,6 +4707,8 @@ input OrderSettingsUpdateInput {enum OrderSortField {NUMBERCREATION_DATE+ CREATED_AT+ LAST_MODIFIED_ATCUSTOMERPAYMENTFULFILLMENT_STATUS@@ -5693,6 +5696,8 @@ enum ProductOrderField {TYPEPUBLISHEDPUBLICATION_DATE+ PUBLISHED_AT+ LAST_MODIFIED_ATCOLLECTIONRATING}@@ -6007,6 +6012,15 @@ type ProductVariantSetDefault {errors: [ProductError!]!}+enum ProductVariantSortField {+ LAST_MODIFIED_AT+}++input ProductVariantSortingInput {+ direction: OrderDirection!+ field: ProductVariantSortField!+}+type ProductVariantStocksCreate {productVariant: ProductVariantbulkStockErrors: [BulkStockError!]! @deprecated(reason: "This field will be removed in Saleor 4.0. Use `errors` field instead.")@@ -6083,7 +6097,7 @@ type Query {productType(id: ID!): ProductTypeproductTypes(filter: ProductTypeFilterInput, sortBy: ProductTypeSortingInput, before: String, after: String, first: Int, last: Int): ProductTypeCountableConnectionproductVariant(id: ID, sku: String, channel: String): ProductVariant- productVariants(ids: [ID], channel: String, filter: ProductVariantFilterInput, before: String, after: String, first: Int, last: Int): ProductVariantCountableConnection+ productVariants(ids: [ID], channel: String, filter: ProductVariantFilterInput, sortBy: ProductVariantSortingInput, before: String, after: String, first: Int, last: Int): ProductVariantCountableConnectionreportProductSales(period: ReportingPeriod!, channel: String!, before: String, after: String, first: Int, last: Int): ProductVariantCountableConnectionpayment(id: ID!): Paymentpayments(filter: PaymentFilterInput, before: String, after: String, first: Int, last: Int): PaymentCountableConnection@@ -6282,6 +6296,8 @@ enum SaleSortField {END_DATEVALUETYPE+ CREATED_AT+ LAST_MODIFIED_AT}input SaleSortingInput {@@ -7088,6 +7104,8 @@ enum UserSortField {LAST_NAMEORDER_COUNT+ CREATED_AT+ LAST_MODIFIED_AT}input UserSortingInput {