From afc7ff839f84db14e8462828f305002e0e7d6978 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 14 Jan 2021 16:51:20 +0100
Subject: [PATCH 1/4] maelis: factorise child_activity endpoint (#50081)

---
 passerelle/apps/maelis/models.py | 51 +++++++++++++++++---------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/passerelle/apps/maelis/models.py b/passerelle/apps/maelis/models.py
index 83a44d05..670cbc6a 100644
--- a/passerelle/apps/maelis/models.py
+++ b/passerelle/apps/maelis/models.py
@@ -395,59 +395,62 @@ class Maelis(BaseResource):
             end_datetime = start_datetime + timezone.timedelta(days=62)
         r = self.call('ActivityService?wsdl', 'readActivityList',
                       schoolyear=school_year, numPerson=personID,
                       dateStartCalend=start_datetime,
                       dateEndCalend=end_datetime)
         activities = serialize_object(r)
         return {'data': [utils.normalize_activity(a) for a in activities]}
 
-    @endpoint(
-        display_category=_('Activities'),
-        perm='can_access',
-        display_order=2,
-        description=_('Get child activities'),
-        name='child-activities',
-        parameters={
-            'NameID': {'description': _('Publik ID')},
-            'childID': {'description': _('Child ID')},
-            'subscribingStatus': {
-                'description': _('subscribed, not-subscribed or None')},
-            'queryDate': {'description': _('Optional querying date (YYYY-MM-DD)')}
-    })
-    def child_activities(self, request, NameID, childID,
-                         subscribingStatus=None, queryDate=None):
-        if subscribingStatus:
-            if subscribingStatus != 'subscribed' and subscribingStatus != 'not-subscribed':
-                raise APIError('wrong value for subscribingStatus: %s' % subscribingStatus)
-        if queryDate:
+    def get_activities_dates(self, query_date):
+        if query_date:
             try:
-                start_date = parse_date(queryDate)
+                start_date = parse_date(query_date)
             except ValueError as exc:
                 raise APIError('input is well formatted but not a valid date: %s' % exc)
             if not start_date:
-                raise APIError("input isn't well formatted, YYYY-MM-DD expected: %s" % queryDate)
+                raise APIError("input isn't well formatted, YYYY-MM-DD expected: %s" % query_date)
         else:
             start_date = timezone.now().date()
         if start_date.strftime('%m-%d') >= '05-01':
             # start displaying next year activities on may
             school_year = start_date.year
         else:
             school_year = start_date.year - 1
         end_date = utils.get_datetime('%s-07-31' % (school_year + 1)).date()
+        return school_year, start_date, end_date
 
-        child_info = self.get_child_info(NameID, childID)
+    def get_child_activities(self, childID, school_year, start_date, end_date):
         r = self.call('ActivityService?wsdl', 'readActivityList',
                       schoolyear=school_year,
                       numPerson=childID,
                       dateStartCalend=start_date,
                       dateEndCalend=end_date)
+        return serialize_object(r)
 
-        flatted_activities = utils.flatten_activities(
-            serialize_object(r), start_date, end_date)
+    @endpoint(
+        display_category=_('Activities'),
+        perm='can_access',
+        display_order=2,
+        description=_('Get child activities'),
+        name='child-activities',
+        parameters={
+            'NameID': {'description': _('Publik ID')},
+            'childID': {'description': _('Child ID')},
+            'subscribingStatus': {
+                'description': _('subscribed, not-subscribed or None')},
+            'queryDate': {'description': _('Optional querying date (YYYY-MM-DD)')}
+    })
+    def child_activities(self, request, NameID, childID, subscribingStatus=None, queryDate=None):
+        if subscribingStatus and subscribingStatus not in ('subscribed', 'not-subscribed'):
+            raise APIError('wrong value for subscribingStatus: %s' % subscribingStatus)
+        school_year, start_date, end_date = self.get_activities_dates(queryDate)
+        child_info = self.get_child_info(NameID, childID)
+        activities = self.get_child_activities(childID, school_year, start_date, end_date)
+        flatted_activities = utils.flatten_activities(activities, start_date, end_date)
         utils.mark_subscribed_flatted_activities(flatted_activities, child_info)
         data = utils.flatted_activities_as_list(flatted_activities, subscribingStatus)
         return {'data': data}
 
     @endpoint(
         display_category=_('Activities'),
         perm='can_access',
         display_order=3,
-- 
2.29.2

