From b817b31c30305fadb4fe72a29ec4adb6e892ce6a Mon Sep 17 00:00:00 2001 From: maudetes Date: Wed, 16 Oct 2024 18:58:12 +0200 Subject: [PATCH 1/3] Add default sort per class --- udata/api_fields.py | 1 + udata/core/reuse/api.py | 5 +---- udata/core/reuse/constants.py | 3 +++ udata/core/reuse/models.py | 3 ++- udata/core/reuse/search.py | 3 ++- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/udata/api_fields.py b/udata/api_fields.py index a17ebf6ce7..1405b58704 100644 --- a/udata/api_fields.py +++ b/udata/api_fields.py @@ -295,6 +295,7 @@ def make_lambda(method): location="args", choices=choices, help="The field (and direction) on which sorting apply", + default=kwargs.get("default_sort"), ) searchable = kwargs.pop("searchable", False) diff --git a/udata/core/reuse/api.py b/udata/core/reuse/api.py index 74f8fb0604..c3a4b7abbc 100644 --- a/udata/core/reuse/api.py +++ b/udata/core/reuse/api.py @@ -13,7 +13,7 @@ from udata.core.badges.fields import badge_fields from udata.core.dataset.api_fields import dataset_ref_fields from udata.core.followers.api import FollowAPI -from udata.core.reuse.constants import REUSE_TOPICS, REUSE_TYPES +from udata.core.reuse.constants import REUSE_TOPICS, REUSE_TYPES, SUGGEST_SORTING from udata.core.storages.api import ( image_parser, parse_uploaded_image, @@ -30,9 +30,6 @@ from .models import Reuse from .permissions import ReuseEditPermission -DEFAULT_SORTING = "-created_at" -SUGGEST_SORTING = "-metrics.followers" - class ReuseApiParser(ModelApiParser): sorts = { diff --git a/udata/core/reuse/constants.py b/udata/core/reuse/constants.py index 0efafe6c7e..c2cb3f08d7 100644 --- a/udata/core/reuse/constants.py +++ b/udata/core/reuse/constants.py @@ -35,3 +35,6 @@ TITLE_SIZE_LIMIT = 350 DESCRIPTION_SIZE_LIMIT = 100000 + +DEFAULT_SORTING = "-created_at" +SUGGEST_SORTING = "-metrics.followers" diff --git a/udata/core/reuse/models.py b/udata/core/reuse/models.py index b5c1f4a46c..1e2bc65ac4 100644 --- a/udata/core/reuse/models.py +++ b/udata/core/reuse/models.py @@ -14,7 +14,7 @@ from udata.uris import endpoint_for from udata.utils import hash_url -from .constants import IMAGE_MAX_SIZE, IMAGE_SIZES, REUSE_TOPICS, REUSE_TYPES +from .constants import DEFAULT_SORTING, IMAGE_MAX_SIZE, IMAGE_SIZES, REUSE_TOPICS, REUSE_TYPES __all__ = ("Reuse",) @@ -35,6 +35,7 @@ def check_url_does_not_exists(url): @generate_fields( searchable=True, + default_sort=DEFAULT_SORTING, additionalSorts=[ {"key": "datasets", "value": "metrics.datasets"}, {"key": "followers", "value": "metrics.followers"}, diff --git a/udata/core/reuse/search.py b/udata/core/reuse/search.py index 3774e11647..42e44a161a 100644 --- a/udata/core/reuse/search.py +++ b/udata/core/reuse/search.py @@ -1,6 +1,7 @@ import datetime -from udata.core.reuse.api import DEFAULT_SORTING, ReuseApiParser +from udata.core.reuse.api import ReuseApiParser +from udata.core.reuse.constants import DEFAULT_SORTING from udata.models import Organization, Reuse, User from udata.search import ( BoolFilter, From 2111584a40235a3bdf4eb97aa3944af217c8ae17 Mon Sep 17 00:00:00 2001 From: maudetes Date: Wed, 16 Oct 2024 18:58:54 +0200 Subject: [PATCH 2/3] Add -created_at as default dataserivce sort --- udata/core/dataservices/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/udata/core/dataservices/models.py b/udata/core/dataservices/models.py index 50ed74c255..0c1b658969 100644 --- a/udata/core/dataservices/models.py +++ b/udata/core/dataservices/models.py @@ -94,7 +94,7 @@ class HarvestMetadata(db.EmbeddedDocument): archived_at = field(db.DateTimeField()) -@generate_fields(searchable=True) +@generate_fields(searchable=True, default_sort="-created_at") class Dataservice(WithMetrics, Owned, db.Document): meta = { "indexes": [ From 2022ae7d15b84ac7ec34587dd2c2d77a020f9cae Mon Sep 17 00:00:00 2001 From: maudetes Date: Wed, 16 Oct 2024 19:00:23 +0200 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52b22c3320..558256175c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Current (in progress) -- Nothing yet +- Add default class sort on reuses and dataservices [#3170](https://github.com/opendatateam/udata/pull/3170) ## 9.2.3 (2024-10-14)