From d64215306ffd7a368b315f507cb1aa399c2cfebf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 16 Apr 2020 15:20:54 +0200
Subject: [PATCH 1/2] toulouse-axel: get possible days for annual values
 (#41405)

---
 passerelle/contrib/toulouse_axel/models.py |  39 +++++
 tests/test_toulouse_axel.py                | 174 ++++++++++++++++++++-
 2 files changed, 207 insertions(+), 6 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 0494f3d7..977f8cf6 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -24,6 +24,7 @@ from django.db import models
 from django.http import HttpResponse
 from django.utils import dateformat
 from django.utils.dates import WEEKDAYS_REV
+from django.utils.dates import WEEKDAYS as WEEKDAYS_LABELS
 from django.utils.translation import ugettext_lazy as _
 
 from passerelle.base.models import BaseResource
@@ -985,6 +986,44 @@ class ToulouseAxel(BaseResource):
 
         return {'data': activity_days}
 
+    @endpoint(
+        description=_("Get annual possible days to book an activity of a child"),
+        perm='can_access',
+        parameters={
+            'NameID': {'description': _('Publik ID')},
+            'idpersonne': {'description': _('Child ID')},
+            'activity_type': {'description': _('Activity type (MAT, MIDI, SOIR, GARD)')},
+        })
+    def clae_booking_activity_annual_possible_days(self, request, NameID, idpersonne, activity_type):
+        link = self.get_link(NameID)
+        if activity_type not in ['MAT', 'MIDI', 'SOIR', 'GARD']:
+            raise APIError('bad activity_type, should be MAT, MIDI, SOIR or GARD', err_code='bad-request', http_status=400)
+
+        today = datetime.date.today()
+        reference_year = utils.get_reference_year_from_date(today)
+        activities = self.get_child_activities(
+            dui=link.dui,
+            reference_year=reference_year,
+            child_id=idpersonne,
+            activity_types=[activity_type]).get('ACTIVITE', [])
+
+        activity_days = []
+        if activities:
+            activity = activities[0]
+            for i, day in enumerate(['monday', 'tuesday', 'wednesday', 'thursday', 'friday']):
+                disabled = False
+                if activity_type == 'GARD' and day != 'wednesday':
+                    disabled = True
+                elif activity_type == 'SOIR' and day == 'wednesday':
+                    disabled = True
+                activity_days.append({
+                    'id': '{}:{}:{}:{}'.format(idpersonne, activity_type, activity['IDACTIVITE'], day),
+                    'text': WEEKDAYS_LABELS[i],
+                    'disabled': disabled,
+                })
+
+        return {'data': activity_days}
+
     @endpoint(
         description=_("Get booked days for an activity of a child, for a period"),
         perm='can_access',
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 2656cc32..6a00468e 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -2615,12 +2615,6 @@ def test_clae_booking_activity_possible_days_endpoint(app, resource, child_activ
     assert resp.json['data'][3]['prefill'] is False
     assert resp.json['data'][4]['prefill'] is True
 
-    with mock_getdata(content, 'ReservationPeriode'):
-        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-            resp = app.get(
-                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
-                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
-
     # again - data are in cache
     resp = app.get(
         '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
@@ -2674,6 +2668,174 @@ def test_clae_booking_activity_possible_days_period(app, resource, today, start_
         assert resp.json['data'][-1]['id'] == '3535:MAT:TOTO:%s' % last_date
 
 
+@freezegun.freeze_time('2019-09-01')
+def test_clae_booking_activity_annual_possible_days_endpoint_axel_error(app, resource):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    with mock.patch('passerelle.contrib.toulouse_axel.schemas.enfants_activites') as operation:
+        operation.side_effect = AxelError('FooBar')
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+    assert resp.json['err_desc'] == "Axel error: FooBar"
+    assert resp.json['err'] == 'error'
+
+
+@freezegun.freeze_time('2019-09-01')
+def test_clae_booking_activity_annual_possible_days_endpoint_activity_type(app, resource):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=FOO', status=400)
+    assert resp.json['err_desc'] == "bad activity_type, should be MAT, MIDI, SOIR or GARD"
+    assert resp.json['err'] == 'bad-request'
+
+
+@freezegun.freeze_time('2019-09-01')
+def test_clae_booking_activity_annual_possible_days_endpoint_no_result(app, resource):
+    resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+    assert resp.json['err_desc'] == "Person not found"
+    assert resp.json['err'] == 'not-found'
+
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
+    with open(filepath) as xml:
+        content = xml.read()
+    with mock_getdata(content, 'EnfantsActivites'):
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=4242&activity_type=MAT')
+    assert resp.json['err_desc'] == "Child not found"
+    assert resp.json['err'] == 'not-found'
+
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value={'ACTIVITE': []}):
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == []
+
+
+@freezegun.freeze_time('2019-09-01')
+def test_clae_booking_activity_annual_possible_days_endpoint(app, resource, child_activities_data):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    activities = {'ACTIVITE': [child_activities_data['ENFANT'][0]['ACTIVITE'][0]]}
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == [
+        {
+            'id': '3535:MAT:A19P1M1:monday',
+            'text': 'Monday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MAT:A19P1M1:tuesday',
+            'text': 'Tuesday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MAT:A19P1M1:wednesday',
+            'text': 'Wednesday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MAT:A19P1M1:thursday',
+            'text': 'Thursday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MAT:A19P1M1:friday',
+            'text': 'Friday',
+            'disabled': False,
+        },
+    ]
+    activities = {'ACTIVITE': [child_activities_data['ENFANT'][0]['ACTIVITE'][1]]}
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MIDI')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == [
+        {
+            'id': '3535:MIDI:A19P1M2:monday',
+            'text': 'Monday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MIDI:A19P1M2:tuesday',
+            'text': 'Tuesday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MIDI:A19P1M2:wednesday',
+            'text': 'Wednesday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MIDI:A19P1M2:thursday',
+            'text': 'Thursday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:MIDI:A19P1M2:friday',
+            'text': 'Friday',
+            'disabled': False,
+        },
+    ]
+    activities = {'ACTIVITE': [child_activities_data['ENFANT'][0]['ACTIVITE'][2]]}
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=SOIR')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == [
+        {
+            'id': '3535:SOIR:A19P1M3:monday',
+            'text': 'Monday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:SOIR:A19P1M3:tuesday',
+            'text': 'Tuesday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:SOIR:A19P1M3:wednesday',
+            'text': 'Wednesday',
+            'disabled': True,
+        },
+        {
+            'id': '3535:SOIR:A19P1M3:thursday',
+            'text': 'Thursday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:SOIR:A19P1M3:friday',
+            'text': 'Friday',
+            'disabled': False,
+        },
+    ]
+    activities = {'ACTIVITE': [child_activities_data['ENFANT'][0]['ACTIVITE'][3]]}
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=GARD')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == [
+        {
+            'id': '3535:GARD:A19P1M4:monday',
+            'text': 'Monday',
+            'disabled': True,
+        },
+        {
+            'id': '3535:GARD:A19P1M4:tuesday',
+            'text': 'Tuesday',
+            'disabled': True,
+        },
+        {
+            'id': '3535:GARD:A19P1M4:wednesday',
+            'text': 'Wednesday',
+            'disabled': False,
+        },
+        {
+            'id': '3535:GARD:A19P1M4:thursday',
+            'text': 'Thursday',
+            'disabled': True,
+        },
+        {
+            'id': '3535:GARD:A19P1M4:friday',
+            'text': 'Friday',
+            'disabled': True,
+        },
+    ]
+
+
 @freezegun.freeze_time('2019-09-01')
 def test_clae_booking_activity_prefill_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
-- 
2.20.1

