From d176d9fc4b2c59b74b2ce25d957933f1a017651b Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Wed, 10 May 2023 18:53:20 +0200 Subject: [PATCH] toulouse-maelis: add service criteria to catalog (#77084) --- passerelle/contrib/toulouse_maelis/models.py | 15 +- .../toulouse_maelis/R_read_activity_list.xml | 242 ++++++++++++++++++ tests/test_toulouse_maelis.py | 91 ++++++- 3 files changed, 337 insertions(+), 11 deletions(-) diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py index 3bc209f5..69f51821 100644 --- a/passerelle/contrib/toulouse_maelis/models.py +++ b/passerelle/contrib/toulouse_maelis/models.py @@ -352,12 +352,12 @@ class ToulouseMaelis(BaseResource, HTTPResource): return [x.item_data for x in queryset] - def get_referential_value(self, referential_name, key): + def get_referential_value(self, referential_name, key, default='key'): try: return self.referential.get(referential_name=referential_name, item_id=key).item_text except Referential.DoesNotExist: self.logger.warning("No '%s' key into Maelis '%s' referential", key, referential_name) - return key + return key if default == 'key' else None def get_link(self, NameID): try: @@ -2734,6 +2734,7 @@ class ToulouseMaelis(BaseResource, HTTPResource): def read_activity_list(self, request, ref_date=None): reference_year = utils.get_reference_year_from_date(ref_date or now()) labels = { + 'service': 'Service', 'nature': "Nature de l'activité", 'type': "Type de l'activité", 'public': 'Public', @@ -2763,6 +2764,8 @@ class ToulouseMaelis(BaseResource, HTTPResource): activities = catalogs[0]['data'] if catalogs else [] def add_criteria(label_key, criteria_key, criteria_value): + if not criteria_value: + return criterias[label_key]['data'][criteria_key] = criteria_value if criteria_key not in all_criterias[label_key]['data']: all_criterias[label_key]['data'][criteria_key] = criteria_value @@ -2789,19 +2792,23 @@ class ToulouseMaelis(BaseResource, HTTPResource): activity['text'] = ( activity['activityPortail']['libelle2'] or activity['activityPortail']['libelle'] ) + service_id = activity['activityPortail']['idService'] + service_text = self.get_referential_value('Service', service_id, default=None) + activity['activityPortail']['idService_text'] = service_text for label_key in criterias: criterias[label_key]['data'] = {} add_criteria('nature', activity_nature['code'], activity_nature['libelle']) type_value = activity_type['libelle'].split('-')[0].strip() add_criteria('type', slugify(type_value), type_value) + add_criteria('service', service_id, service_text) if activity['activityPortail']['weeklyCalendarActivityList']: for day in activity['activityPortail']['weeklyCalendarActivityList'][0]['dayWeekInfoList']: if day['isOpen']: add_criteria('day', str(day['dayNum']), day_names[day['dayNum'] - 1]) - update_criterias_order_field(criterias, ['nature', 'type', 'day']) + update_criterias_order_field(criterias, ['service', 'nature', 'type', 'day']) for unit in activity.pop('unitPortailList'): unit['id'] = unit['idUnit'] @@ -2839,7 +2846,7 @@ class ToulouseMaelis(BaseResource, HTTPResource): 'meta': { 'reference_year': reference_year, 'all_criterias': all_criterias, - 'all_criterias_order': ['nature', 'type', 'public', 'day', 'place'], + 'all_criterias_order': ['service', 'nature', 'type', 'public', 'day', 'place'], }, } diff --git a/tests/data/toulouse_maelis/R_read_activity_list.xml b/tests/data/toulouse_maelis/R_read_activity_list.xml index ac9c37fb..ed2eb769 100644 --- a/tests/data/toulouse_maelis/R_read_activity_list.xml +++ b/tests/data/toulouse_maelis/R_read_activity_list.xml @@ -6,6 +6,7 @@ A10051141965 Vitrail Fusing 1/2 Je Adultes 2022/2023 - Mardi 14h-17h + A10049329051 2022-09-01T00:00:00+02:00 2023-06-30T00:00:00+02:00 N @@ -624,6 +625,247 @@ + + + A10056514645 + TEST ECOLE DES SPORTS 22/23 SEMESTRE 2 - MULTIACTIVITES + + Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis + 1 + + 2023-02-01T00:00:00+01:00 + 2023-06-30T00:00:00+02:00 + B + 2022 + + FORBIDDEN + I + + C + + 1-AAQ + Activités Aquatiques Activité Réguliére + + 1 + Activités Régulières + + + + 2023 + + 1 + false + + + 2 + false + + + 3 + true + + + 4 + false + + + 5 + false + + + 6 + false + + + 7 + false + + + + 2023-02-01T00:00:00+01:00 + 2023-02-08T00:00:00+01:00 + 2023-02-15T00:00:00+01:00 + 2023-02-22T00:00:00+01:00 + 2023-03-01T00:00:00+01:00 + 2023-03-08T00:00:00+01:00 + 2023-03-15T00:00:00+01:00 + 2023-03-22T00:00:00+01:00 + 2023-03-29T00:00:00+02:00 + 2023-04-05T00:00:00+02:00 + 2023-04-12T00:00:00+02:00 + 2023-04-19T00:00:00+02:00 + 2023-04-26T00:00:00+02:00 + 2023-05-03T00:00:00+02:00 + 2023-05-10T00:00:00+02:00 + 2023-05-17T00:00:00+02:00 + 2023-05-24T00:00:00+02:00 + 2023-05-31T00:00:00+02:00 + 2023-06-07T00:00:00+02:00 + 2023-06-14T00:00:00+02:00 + 2023-06-21T00:00:00+02:00 + 2023-06-28T00:00:00+02:00 + + A10056514650 + MERCREDI - 13h45/17h - 8/15Ans + 2023-02-01T00:00:00+01:00 + 2023-06-30T00:00:00+02:00 + 2008-01-01T00:00:00+01:00 + 2015-12-31T00:00:00+01:00 + B + E + 0 + N + O + + A10053179757 + ARGOULETS + + 0 + + 13:45 + 17:00 + + true + + + + + A10056514648 + MERCREDI - 14h/16h30 - 10/15Ans + 2023-02-01T00:00:00+01:00 + 2023-06-30T00:00:00+02:00 + 2008-01-01T00:00:00+01:00 + 2013-12-31T00:00:00+01:00 + C + E + 0 + N + O + + A10053179876 + LA RAMEE + + 0 + + 14:00 + 16:30 + + true + + + + + A10056514649 + MERCREDI - 15h30/17h - 8/15Ans + 2023-02-01T00:00:00+01:00 + 2023-06-30T00:00:00+02:00 + 2008-01-01T00:00:00+01:00 + 2015-12-31T00:00:00+01:00 + F + E + 0 + N + O + + A10053179757 + ARGOULETS + + 0 + + 15:30 + 17:00 + + true + + + + + + + A10056517594 + TEST promenade forêt enchantée + Promenade forêt enchantée + + Activité senior du 15 au 16 juin 2023 + 1 + + plop + 2023-06-15T00:00:00+02:00 + 2023-06-16T00:00:00+02:00 + B + 2022 + + FORBIDDEN + I + + N + + 1-APE + Activité Pédestre Activité régulière + + 1 + Activités Régulières + + + + 2023 + + 1 + true + + + 2 + true + + + 3 + true + + + 4 + true + + + 5 + true + + + 6 + false + + + 7 + false + + + + 2023-06-15T00:00:00+02:00 + 2023-06-16T00:00:00+02:00 + + A10056517595 + TEST promenade forêt enchantée + A + 2023-06-15T00:00:00+02:00 + 2023-06-16T00:00:00+02:00 + 1900-01-01T00:00:00+01:00 + 1963-12-31T00:00:00+01:00 + X + E + 0 + N + O + + A10056517597 + TERRITOIRE OUEST + + 0 + + + true + + + + diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py index 2f52c328..5d14cc6a 100644 --- a/tests/test_toulouse_maelis.py +++ b/tests/test_toulouse_maelis.py @@ -5972,12 +5972,29 @@ def test_read_activity_list(activity_service, con, app): activity_service.add_soap_response('readActivityList', get_xml_file('R_read_activity_list.xml')) url = get_endpoint('read-activity-list') - con.loisir_nature_codes = '4,L,, S ' + con.loisir_nature_codes = '1,4,L,, S ' con.save() params = {'ref_date': '2023-01-01'} resp = app.get(url, params=params) assert resp.json['err'] == 0 - assert len(resp.json['data']) == 4 + assert len(resp.json['data']) == 8 + assert [ + ( + x['id'], + x['activity']['activityPortail']['idService'], + x['activity']['activityPortail']['idService_text'], + ) + for x in resp.json['data'] + ] == [ + ('A10051141965-A10051141966-A10053179226', 'A10049329051', 'Sorties'), + ('A10051141965-A10051141968-A10053179226', 'A10049329051', 'Sorties'), + ('A10051141965-A10051141970-A10053179226', 'A10049329051', 'Sorties'), + ('A10051141965-A10051141990-A10053179227', 'A10049329051', 'Sorties'), + ('A10056514645-A10056514650-A10053179757', None, None), + ('A10056514645-A10056514648-A10053179876', None, None), + ('A10056514645-A10056514649-A10053179757', None, None), + ('A10056517594-A10056517595-A10056517597', 'plop', None), + ] item = resp.json['data'][0] item['activity'] = 'N/A' item['unit'] = 'N/A' @@ -5991,6 +6008,11 @@ def test_read_activity_list(activity_service, con, app): 'unit': 'N/A', 'place': 'N/A', 'criterias': { + 'service': { + 'text': 'Service', + 'data': {'A10049329051': 'Sorties'}, + 'order': ['A10049329051'], + }, 'nature': {'text': "Nature de l'activité", 'data': {'4': 'ART PLASTIQUE'}, 'order': ['4']}, 'type': { 'text': "Type de l'activité", @@ -6017,6 +6039,59 @@ def test_read_activity_list(activity_service, con, app): 'day': {'text': 'Jours', 'data': {'2': 'Mardi'}, 'order': ['2']}, }, } + assert resp.json['meta'] == { + 'reference_year': 2022, + 'all_criterias': { + 'service': {'text': 'Service', 'data': {'A10049329051': 'Sorties'}, 'order': ['A10049329051']}, + 'nature': { + 'text': "Nature de l'activité", + 'data': {'4': 'ART PLASTIQUE', '1': 'Activités Régulières'}, + 'order': ['1', '4'], + }, + 'type': { + 'text': "Type de l'activité", + 'data': { + 'activite-reguliere': 'ACTIVITE REGULIERE', + 'activites-aquatiques-activite-reguliere': 'Activités Aquatiques Activité Réguliére', + 'activite-pedestre-activite-reguliere': 'Activité Pédestre Activité régulière', + }, + 'order': [ + 'activite-reguliere', + 'activite-pedestre-activite-reguliere', + 'activites-aquatiques-activite-reguliere', + ], + }, + 'public': { + 'text': 'Public', + 'data': { + '0': 'Petit enfant (- de 3 ans)', + '1': 'Enfant (3-11 ans)', + '2': 'Ado (12-17 ans)', + '3': 'Jeune (18-25 ans)', + '4': 'Adulte (26-59 ans)', + '5': 'Sénior (60 ans et plus)', + }, + 'order': ['0', '1', '2', '3', '4', '5'], + }, + 'day': { + 'text': 'Jours', + 'data': {'2': 'Mardi', '3': 'Mercredi', '1': 'Lundi', '4': 'Jeudi', '5': 'Vendredi'}, + 'order': ['1', '2', '3', '4', '5'], + }, + 'place': { + 'text': 'Lieu', + 'data': { + 'A10053179226': 'Centre Culturel ALBAN MINVILLE', + 'A10053179227': 'Un autre centre culturel', + 'A10053179757': 'ARGOULETS', + 'A10053179876': 'LA RAMEE', + 'A10056517597': 'TERRITOIRE OUEST', + }, + 'order': ['A10053179757', 'A10053179226', 'A10053179876', 'A10056517597', 'A10053179227'], + }, + }, + 'all_criterias_order': ['service', 'nature', 'type', 'public', 'day', 'place'], + } # make sure activities have a single place defined for item in resp.json['data']: @@ -6032,13 +6107,14 @@ def test_read_activity_list(activity_service, con, app): 'meta': { 'reference_year': 2022, 'all_criterias': { + 'service': {'text': 'Service', 'data': {}, 'order': []}, 'nature': {'text': "Nature de l'activité", 'data': {}, 'order': []}, 'type': {'text': "Type de l'activité", 'data': {}, 'order': []}, 'public': {'text': 'Public', 'data': {}, 'order': []}, 'day': {'text': 'Jours', 'data': {}, 'order': []}, 'place': {'text': 'Lieu', 'data': {}, 'order': []}, }, - 'all_criterias_order': ['nature', 'type', 'public', 'day', 'place'], + 'all_criterias_order': ['service', 'nature', 'type', 'public', 'day', 'place'], }, 'err': 0, } @@ -6072,7 +6148,7 @@ def test_read_activity_list_cache(mocked_get, con, app): ) resp = app.get(url, params=params) assert resp.json['err'] == 0 - assert len(resp.json['data']) == 4 + assert len(resp.json['data']) == 8 assert [x.item_id for x in Referential.objects.filter(referential_name='ActivityCatalog')] == [ '1969', @@ -6084,7 +6160,7 @@ def test_read_activity_list_cache(mocked_get, con, app): mocked_post.side_effect = ReadTimeout('timeout') resp = app.get(url, params=params) assert resp.json['err'] == 0 - assert len(resp.json['data']) == 4 + assert len(resp.json['data']) == 8 @mock.patch('passerelle.utils.Request.get') @@ -6099,8 +6175,9 @@ def test_read_activity_list_no_nature(mocked_post, mocked_get, con, app): def mocked_reply(referential_name, id=None): queryset = con.referential.filter(referential_name=referential_name, item_id=id) data = [x.item_data for x in queryset] - activity = data[0]['data'][0] - activity['activityPortail']['activityType'] = None + for item in data[0]['data']: + activity = item + activity['activityPortail']['activityType'] = None return data with mock.patch('passerelle.contrib.toulouse_maelis.models.ToulouseMaelis.get_referential') as mocked_ref: