Skip to content

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

Sorting by `created_at` field for multiple queries (#10911)

Piotr Zabieglik6c5aab7790d4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 71704c4306..ca548782df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,9 @@ All notable, unreleased changes to this project will be documented in this file.
- Allow sorting products by `CREATED_AT` field. #10900 by @zedzior
- Add ability to pass metadata directly in create/update mutations for product app models - #10689 by @SzymJ
- Add ability to use SKU argument in `productVariantUpdate`, `productVariantDelete`, `productVariantBulkDelete`, `productVariantStocksUpdate`, `productVariantStocksDelete`, `productVariantChannelListingUpdate` mutations - #10861 by @SzymJ
+- Add sorting by `CREATED_AT` field. #10911 by @zedzior
+ - Affected types: GiftCard, Page.
+ - Deprecated `CREATION_DATE` sort field on Page type. Use `CREATED_AT` instead.
### Other changes
diff --git a/saleor/giftcard/migrations/0016_alter_giftcard_created_at.py b/saleor/giftcard/migrations/0016_alter_giftcard_created_at.py
new file mode 100644
index 0000000000..9d77952f06
--- /dev/null
+++ b/saleor/giftcard/migrations/0016_alter_giftcard_created_at.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.0.8 on 2022-10-20 07:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("giftcard", "0015_rename_created_giftcard_created_at"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="giftcard",
+ name="created_at",
+ field=models.DateTimeField(auto_now_add=True, db_index=True),
+ ),
+ ]
diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.py
index e147f0ce42..5abde2aefb 100644
--- a/saleor/giftcard/models.py
+++ b/saleor/giftcard/models.py
@@ -67,7 +67,7 @@ class GiftCard(ModelWithMetadata):
expiry_date = models.DateField(null=True, blank=True)
tags = models.ManyToManyField(GiftCardTag, "gift_cards")
- created_at = models.DateTimeField(auto_now_add=True)
+ created_at = models.DateTimeField(auto_now_add=True, db_index=True)
last_used_on = models.DateTimeField(null=True, blank=True)
product = models.ForeignKey(
"product.Product",
diff --git a/saleor/graphql/giftcard/sorters.py b/saleor/graphql/giftcard/sorters.py
index 4f42882f02..bb2e5cb3e0 100644
--- a/saleor/graphql/giftcard/sorters.py
+++ b/saleor/graphql/giftcard/sorters.py
@@ -1,5 +1,6 @@
import graphene
+from ..core.descriptions import ADDED_IN_38
from ..core.types import SortInputObjectType
@@ -7,12 +8,16 @@ class GiftCardSortField(graphene.Enum):
PRODUCT = ["product__name", "product__slug"]
USED_BY = ["used_by__first_name", "used_by__last_name", "created_at"]
CURRENT_BALANCE = ["current_balance_amount", "created_at"]
+ CREATED_AT = ["created_at", "id"]
@property
def description(self):
if self.name in GiftCardSortField.__enum__._member_names_:
sort_name = self.name.lower().replace("_", " ")
- return f"Sort orders by {sort_name}."
+ description = f"Sort gift cards by {sort_name}."
+ if self.name == "CREATED_AT":
+ description += ADDED_IN_38
+ return description
raise ValueError("Unsupported enum value: %s" % self.value)
diff --git a/saleor/graphql/giftcard/tests/queries/test_gift_card_sorting.py b/saleor/graphql/giftcard/tests/queries/test_gift_card_sorting.py
index 382f16c133..68eebd5f01 100644
--- a/saleor/graphql/giftcard/tests/queries/test_gift_card_sorting.py
+++ b/saleor/graphql/giftcard/tests/queries/test_gift_card_sorting.py
@@ -14,6 +14,7 @@ QUERY_GIFT_CARDS = """
node {
id
last4CodeChars
+ created
}
}
totalCount
@@ -177,3 +178,36 @@ def test_sorting_gift_cards_by_used_by(
assert [card["node"]["id"] for card in data] == [
graphene.Node.to_global_id("GiftCard", card.pk) for card in gift_card_list
]
+
+
+ "direction",
+ ("ASC", "DESC"),
+)
+def test_sorting_gift_cards_by_created_at(
+ direction,
+ staff_api_client,
+ gift_card_list,
+ permission_manage_gift_card,
+):
+ # given
+ variables = {
+ "sortBy": {
+ "direction": direction,
+ "field": "CREATED_AT",
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GIFT_CARDS, variables, permissions=[permission_manage_gift_card]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ creation_dates = [
+ gc["node"]["created"] for gc in content["data"]["giftCards"]["edges"]
+ ]
+ if direction == "DESC":
+ creation_dates.reverse()
+ assert creation_dates[0] < creation_dates[1] < creation_dates[2]
diff --git a/saleor/graphql/page/sorters.py b/saleor/graphql/page/sorters.py
index 7c1e757493..2aa812ceca 100644
--- a/saleor/graphql/page/sorters.py
+++ b/saleor/graphql/page/sorters.py
@@ -1,6 +1,6 @@
import graphene
-from ..core.descriptions import DEPRECATED_IN_3X_INPUT
+from ..core.descriptions import ADDED_IN_38, DEPRECATED_IN_3X_INPUT
from ..core.types import SortInputObjectType
@@ -11,6 +11,7 @@ class PageSortField(graphene.Enum):
CREATION_DATE = ["created_at", "title", "slug"]
PUBLICATION_DATE = ["published_at", "title", "slug"]
PUBLISHED_AT = ["published_at", "title", "slug"]
+ CREATED_AT = ["created_at", "title", "slug"]
@property
def description(self):
@@ -19,6 +20,10 @@ class PageSortField(graphene.Enum):
description = f"Sort pages by {sort_name}."
if self.name == "PUBLICATION_DATE":
description += DEPRECATED_IN_3X_INPUT
+ if self.name == "CREATION_DATE":
+ description += DEPRECATED_IN_3X_INPUT
+ if self.name == "CREATED_AT":
+ description += ADDED_IN_38
return description
raise ValueError("Unsupported enum value: %s" % self.value)
diff --git a/saleor/graphql/page/tests/queries/test_pages.py b/saleor/graphql/page/tests/queries/test_pages.py
index 19f595f754..2c4fef03d1 100644
--- a/saleor/graphql/page/tests/queries/test_pages.py
+++ b/saleor/graphql/page/tests/queries/test_pages.py
@@ -140,6 +140,8 @@ QUERY_PAGE_WITH_SORT = """
({"field": "VISIBILITY", "direction": "DESC"}, ["Page1", "About", "Page2"]),
({"field": "CREATION_DATE", "direction": "ASC"}, ["Page1", "About", "Page2"]),
({"field": "CREATION_DATE", "direction": "DESC"}, ["Page2", "About", "Page1"]),
+ ({"field": "CREATED_AT", "direction": "ASC"}, ["Page1", "About", "Page2"]),
+ ({"field": "CREATED_AT", "direction": "DESC"}, ["Page2", "About", "Page1"]),
(
{"field": "PUBLISHED_AT", "direction": "ASC"},
["Page1", "Page2", "About"],
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index f9ddf5f333..611c7ce352 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -9997,7 +9997,11 @@ enum PageSortField {
"""Sort pages by visibility."""
VISIBILITY
- """Sort pages by creation date."""
+ """
+ Sort pages by creation date.
+
+ DEPRECATED: this field will be removed in Saleor 4.0.
+ """
CREATION_DATE
"""
@@ -10013,6 +10017,13 @@ enum PageSortField {
DEPRECATED: this field will be removed in Saleor 4.0.
"""
PUBLISHED_AT
+
+ """
+ Sort pages by creation date.
+
+ DEPRECATED: this field will be removed in Saleor 4.0.
+ """
+ CREATED_AT
}
input PageFilterInput {
@@ -10243,14 +10254,21 @@ input GiftCardSortingInput {
}
enum GiftCardSortField {
- """Sort orders by product."""
+ """Sort gift cards by product."""
PRODUCT
- """Sort orders by used by."""
+ """Sort gift cards by used by."""
USED_BY
- """Sort orders by current balance."""
+ """Sort gift cards by current balance."""
CURRENT_BALANCE
+
+ """
+ Sort gift cards by created at.
+
+ Added in Saleor 3.8.
+ """
+ CREATED_AT
}
input GiftCardFilterInput {
diff --git a/saleor/page/migrations/0027_alter_page_created_at.py b/saleor/page/migrations/0027_alter_page_created_at.py
new file mode 100644
index 0000000000..0664565472
--- /dev/null
+++ b/saleor/page/migrations/0027_alter_page_created_at.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.0.8 on 2022-10-20 07:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("page", "0026_alter_page_publication_date"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="page",
+ name="created_at",
+ field=models.DateTimeField(auto_now_add=True, db_index=True),
+ ),
+ ]
diff --git a/saleor/page/models.py b/saleor/page/models.py
index 491fa8c65a..5e940f3b69 100644
--- a/saleor/page/models.py
+++ b/saleor/page/models.py
@@ -23,7 +23,7 @@ class Page(ModelWithMetadata, SeoModel, PublishableModel):
"PageType", related_name="pages", on_delete=models.CASCADE
)
content = SanitizedJSONField(blank=True, null=True, sanitizer=clean_editor_js)
- created_at = models.DateTimeField(auto_now_add=True)
+ created_at = models.DateTimeField(auto_now_add=True, db_index=True)
translated = TranslationProxy()
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 65464cc3e4..f62ddb4f60 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -3783,6 +3783,32 @@ def gift_card_event(gift_card, order, app, staff_user):
)
+def gift_card_list():
+ gift_cards = list(
+ GiftCard.objects.bulk_create(
+ [
+ GiftCard(
+ code="code-test-1",
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ ),
+ GiftCard(
+ code="code-test-2",
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ ),
+ GiftCard(
+ code="code-test-3",
+ initial_balance=Money(10, "USD"),
+ current_balance=Money(10, "USD"),
+ ),
+ ]
+ )
+ )
+ return gift_cards
+
+
def recalculate_order(order):
lines = OrderLine.objects.filter(order_id=order.pk)
prices = [line.total_price for line in lines]