diff --git a/backend/geonature/core/gn_synthese/routes.py b/backend/geonature/core/gn_synthese/routes.py index 255f9079f6..ede1054752 100644 --- a/backend/geonature/core/gn_synthese/routes.py +++ b/backend/geonature/core/gn_synthese/routes.py @@ -1037,9 +1037,6 @@ def taxon_observers(scope, cd_ref): sort_order = request.args.get("sort_order", SortOrder.ASC, SortOrder) # Handle sorting - if sort_order not in [SortOrder.ASC, SortOrder.DESC]: - raise BadRequest(f"The sort_order {sort_order} is undefined") - if sort_by not in ["observer", "date_min", "date_max", "observation_count", "media_count"]: raise BadRequest(f"The sort_by column {sort_by} is not defined") diff --git a/backend/geonature/core/gn_synthese/utils/taxon_sheet.py b/backend/geonature/core/gn_synthese/utils/taxon_sheet.py index 8500127cb2..6bfcf7ee1e 100644 --- a/backend/geonature/core/gn_synthese/utils/taxon_sheet.py +++ b/backend/geonature/core/gn_synthese/utils/taxon_sheet.py @@ -23,11 +23,8 @@ class TaxonSheetUtils: def update_query_with_sorting(query: Query, sort_by: str, sort_order: SortOrder) -> Query: if sort_order == SortOrder.ASC: return query.order_by(asc(sort_by)) - elif sort_order == SortOrder.DESC: - return query.order_by(desc(sort_by)) - else: - raise BadRequest("The sorting attribute is undefined") - return + + return query.order_by(desc(sort_by)) @staticmethod def paginate(query: Query, page: int, per_page: int) -> Pagination: diff --git a/backend/geonature/tests/fixtures.py b/backend/geonature/tests/fixtures.py index bda91a4930..a4d1ee00e0 100644 --- a/backend/geonature/tests/fixtures.py +++ b/backend/geonature/tests/fixtures.py @@ -328,12 +328,12 @@ def create_user( db.session.add(organisme) users_to_create = [ - (("noright_user", organisme, 0), {}), - (("stranger_user", None, 2), {}), - (("associate_user", organisme, 2), {}), - (("self_user", organisme, 1), {}), + (("noright_user", organisme, 0), {"nom_role": "User", "prenom_role": "NoRight"}), + (("stranger_user", None, 2), {"nom_role": "User", "prenom_role": "Stranger"}), + (("associate_user", organisme, 2), {"nom_role": "User", "prenom_role": "Associate"}), + (("self_user", organisme, 1), {"nom_role": "User", "prenom_role": "Self"}), (("user", organisme, 2), {"nom_role": "Bob", "prenom_role": "Bobby"}), - (("admin_user", organisme, 3), {}), + (("admin_user", organisme, 3), {"nom_role": "Administrateur", "prenom_role": "Test"}), (("associate_user_2_exclude_sensitive", organisme, 2, True), {}), ( ( @@ -523,6 +523,7 @@ def create_synthese( source, uuid=func.uuid_generate_v4(), cor_observers=[], + observers=[], date_min="", date_max="", altitude_min=800, @@ -551,6 +552,7 @@ def create_synthese( altitude_min=altitude_min, altitude_max=altitude_max, cor_observers=cor_observers, + observers=observers, **kwargs, ) @@ -708,6 +710,7 @@ def synthese_data(app, users, datasets, source, sources_modules): source_m, unique_id_sinp, [users["admin_user"], users["user"]], + ["Administrative Test", "Bobby Bob"], date_min, date_max, altitude_min, diff --git a/backend/geonature/tests/test_synthese.py b/backend/geonature/tests/test_synthese.py index 7bc5d26219..1b2d9aa78b 100644 --- a/backend/geonature/tests/test_synthese.py +++ b/backend/geonature/tests/test_synthese.py @@ -1226,7 +1226,9 @@ def test_get_one_synthese_record(self, app, users, synthese_data): assert response.status_code == Forbidden.code def test_taxon_observer(self, synthese_data, users): - set_logged_user(self.client, users["admin_user"]) + set_logged_user(self.client, users["stranger_user"]) + + ## Test Data SORT_ORDER_UNDEFINED = "sort-order-undefined" SORT_ORDER_ASC = "asc" @@ -1238,46 +1240,48 @@ def test_taxon_observer(self, synthese_data, users): CD_REF_OBSERVERS_ASC = { "items": [ { - "date_max": "Mon, 20 May 2019 00:00:00 GMT", - "date_min": "Mon, 20 May 2019 00:00:00 GMT", + "date_max": "Thu, 03 Oct 2024 08:09:10 GMT", + "date_min": "Wed, 02 Oct 2024 11:22:33 GMT", "media_count": 0, - "observation_count": 1, - "observer": "Albert Christophe", + "observation_count": 3, + "observer": "Administrateur Test", }, { - "date_max": "Fri, 04 Oct 2024 17:04:09 GMT", + "date_max": "Thu, 03 Oct 2024 08:09:10 GMT", "date_min": "Wed, 02 Oct 2024 11:22:33 GMT", "media_count": 0, - "observation_count": 4, + "observation_count": 3, "observer": "Bob Bobby", }, ], - "total": 8, - "per_page": 2, "page": 1, + "per_page": 2, + "total": 2, } CD_REF_OBSERVERS_DESC = { "items": [ { - "date_max": "Sat, 20 Apr 2019 00:00:00 GMT", - "date_min": "Thu, 08 Sep 2016 00:00:00 GMT", + "date_max": "Thu, 03 Oct 2024 08:09:10 GMT", + "date_min": "Wed, 02 Oct 2024 11:22:33 GMT", "media_count": 0, "observation_count": 3, - "observer": "Rippert Laurent", + "observer": "Bob Bobby", }, { - "date_max": "Sat, 20 Apr 2019 00:00:00 GMT", - "date_min": "Sat, 20 Apr 2019 00:00:00 GMT", + "date_max": "Thu, 03 Oct 2024 08:09:10 GMT", + "date_min": "Wed, 02 Oct 2024 11:22:33 GMT", "media_count": 0, - "observation_count": 1, - "observer": "Lecuyer Clément", + "observation_count": 3, + "observer": "Administrateur Test", }, ], "page": 1, "per_page": 2, - "total": 8, + "total": 2, } + ## sort_order + # Unknow sort_order parameters: shoudl fallback in asc response = self.client.get( url_for( @@ -1290,7 +1294,7 @@ def test_taxon_observer(self, synthese_data, users): assert response.status_code == 200 assert response.get_json() == CD_REF_OBSERVERS_ASC - # Sort order ASC + # sort order ASC response = self.client.get( url_for( "gn_synthese.taxon_observers", @@ -1302,7 +1306,7 @@ def test_taxon_observer(self, synthese_data, users): assert response.status_code == 200 assert response.get_json() == CD_REF_OBSERVERS_ASC - # Sort order DESC + # sort order DESC response = self.client.get( url_for( "gn_synthese.taxon_observers", @@ -1314,7 +1318,7 @@ def test_taxon_observer(self, synthese_data, users): assert response.status_code == 200 assert response.get_json() == CD_REF_OBSERVERS_DESC - # Unknow sort_by parameters + ## sort_by response = self.client.get( url_for( "gn_synthese.taxon_observers", @@ -1329,7 +1333,7 @@ def test_taxon_observer(self, synthese_data, users): response.json["description"] == f"The sort_by column {SORT_BY_UNDEFINED} is not defined" ) - # Everything is fine + # Ok response = self.client.get( url_for( "gn_synthese.taxon_observers",