From 81f58cad5990f76283990ea47b33deb04804a8a1 Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Thu, 9 Nov 2023 10:53:16 +0100 Subject: [PATCH] toulouse-maelis: filter subscribable school years (#83262) --- passerelle/contrib/toulouse_maelis/models.py | 24 +++++++++++- passerelle/contrib/toulouse_maelis/utils.py | 11 ++++++ .../R_read_year_school_list.xml | 15 +++++--- tests/test_toulouse_maelis.py | 38 ++++++++++++++----- 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py index 22b1ed5b..3fc6c74b 100644 --- a/passerelle/contrib/toulouse_maelis/models.py +++ b/passerelle/contrib/toulouse_maelis/models.py @@ -2632,9 +2632,29 @@ class ToulouseMaelis(BaseResource, HTTPResource): display_category='Inscriptions', description='Lister les années scolaires', name='read-school-years-list', + parameters={ + 'subscribable': { + 'description': "N'afficher que les années ouvertes aux inscriptions", + 'example_value': '0', + }, + }, ) - def read_school_years_list(self, request): - return {'data': self.get_referential('YearSchool')} + def read_school_years_list(self, request, subscribable='1'): + subscribable = utils.strtobool(subscribable) + referential = self.get_referential('YearSchool') + data = [] + for item in referential: + if subscribable is True: + start_date = item.get('dateStartSubscribeSchool') + end_date = item.get('dateEndSubscribeSchool') + if not (start_date and end_date): + continue + start_date = parse_datetime(start_date) + end_date = parse_datetime(end_date) + if not (start_date <= now() <= end_date): + continue + data.append(item) + return {'data': data} @endpoint( display_category='Inscriptions', diff --git a/passerelle/contrib/toulouse_maelis/utils.py b/passerelle/contrib/toulouse_maelis/utils.py index 2fcda66b..b00105c8 100644 --- a/passerelle/contrib/toulouse_maelis/utils.py +++ b/passerelle/contrib/toulouse_maelis/utils.py @@ -17,6 +17,8 @@ from math import inf from dateutil.relativedelta import relativedelta +from passerelle.utils.jsonresponse import APIError + json_date_format = '%Y-%m-%d' @@ -47,3 +49,12 @@ def get_public_criterias(today, start_dob, end_dob): data.append((str(i), publics_txt[i])) break return data + + +def strtobool(val): + val = val.lower() + if val in ('y', 'yes', 't', 'true', 'on', '1'): + return True + elif val in ('n', 'no', 'f', 'false', 'off', '0'): + return False + raise APIError('invalid truth value %r' % val) diff --git a/tests/data/toulouse_maelis/R_read_year_school_list.xml b/tests/data/toulouse_maelis/R_read_year_school_list.xml index 5a280146..2ae268cc 100644 --- a/tests/data/toulouse_maelis/R_read_year_school_list.xml +++ b/tests/data/toulouse_maelis/R_read_year_school_list.xml @@ -5,15 +5,20 @@ 2022 2022-09-01T00:00:00+02:00 2023-07-07T00:00:00+02:00 - 2022-04-01T00:00:00+02:00 - 2023-07-08T00:00:00+02:00 + 2022-09-01T00:00:00+02:00 + 2023-09-01T00:00:00+02:00 2023 - 2023-09-01T00:00:00+02:00 + 2023-09-04T00:00:00+02:00 2024-07-07T00:00:00+02:00 - 2022-12-01T00:00:00+01:00 - 2023-07-08T00:00:00+02:00 + 2022-09-01T00:00:00+02:00 + 2024-07-01T00:00:00+02:00 + + + 2024 + 2024-09-01T00:00:00+02:00 + 2025-07-07T00:00:00+02:00 diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py index f90ba921..58b372d3 100644 --- a/tests/test_toulouse_maelis.py +++ b/tests/test_toulouse_maelis.py @@ -5069,32 +5069,52 @@ def test_get_rl1_direct_debit_order_soap_error(family_service, invoice_service, ) -def test_read_school_year_list(con, app): +def test_read_school_year_list(con, app, freezer): url = get_endpoint('read-school-years-list') - resp = app.get(url) + freezer.move_to('2023-11-09') + + resp = app.get(url + '?subscribable=0') assert resp.json['err'] == 0 - assert len(resp.json['data']) == 2 + assert len(resp.json['data']) == 3 assert resp.json['data'] == [ { 'id': 2022, 'text': '2022', 'schoolYear': 2022, - 'dateStartYearSchool': '2022-09-01T00:00:00+02:00', 'dateEndYearSchool': '2023-07-07T00:00:00+02:00', - 'dateStartSubscribeSchool': '2022-04-01T00:00:00+02:00', - 'dateEndSubscribeSchool': '2023-07-08T00:00:00+02:00', + 'dateStartYearSchool': '2022-09-01T00:00:00+02:00', + 'dateEndSubscribeSchool': '2023-09-01T00:00:00+02:00', + 'dateStartSubscribeSchool': '2022-09-01T00:00:00+02:00', }, { 'id': 2023, 'text': '2023', 'schoolYear': 2023, - 'dateStartYearSchool': '2023-09-01T00:00:00+02:00', 'dateEndYearSchool': '2024-07-07T00:00:00+02:00', - 'dateStartSubscribeSchool': '2022-12-01T00:00:00+01:00', - 'dateEndSubscribeSchool': '2023-07-08T00:00:00+02:00', + 'dateStartYearSchool': '2023-09-04T00:00:00+02:00', + 'dateEndSubscribeSchool': '2024-07-01T00:00:00+02:00', + 'dateStartSubscribeSchool': '2022-09-01T00:00:00+02:00', + }, + { + 'id': 2024, + 'text': '2024', + 'schoolYear': 2024, + 'dateEndYearSchool': '2025-07-07T00:00:00+02:00', + 'dateStartYearSchool': '2024-09-01T00:00:00+02:00', + 'dateEndSubscribeSchool': None, + 'dateStartSubscribeSchool': None, }, ] + # get only subscribable school years by default + resp = app.get(url) + assert resp.json['err'] == 0 + assert [x['text'] for x in resp.json['data']] == ['2023'] + + resp = app.get(url + '?subscribable=plop') + assert resp.json['err'] == 1 + assert resp.json['err_desc'] == "invalid truth value 'plop'" + def test_read_school_levels_list(con, app): url = get_endpoint('read-school-levels-list')