diff --git a/passerelle/contrib/caluire_axel/models.py b/passerelle/contrib/caluire_axel/models.py index 81127d47..a7c0df6d 100644 --- a/passerelle/contrib/caluire_axel/models.py +++ b/passerelle/contrib/caluire_axel/models.py @@ -839,6 +839,84 @@ class CaluireAxel(BaseResource): return updated + def _set_agenda(self, child_id, start_date, end_date, activities_data, current_agenda, booking_list): + # check exclusive activities (GARDERIES and ETUDES) + # build list of requested booked days + exclusive_activities = defaultdict(set) + for booking in booking_list: + _child_id, activity_id, day = booking.split(':') + if _child_id != child_id: + continue + if activity_id not in ['GARDERIES', 'ETUDES']: + continue + exclusive_activities[day].add(booking) + # build list of existing booked days + legacy_exclusive_activities = defaultdict(set) + for activity_id in ['GARDERIES', 'ETUDES']: + legacy_agenda = current_agenda.get(activity_id) or [] + for booking in legacy_agenda: + if booking['prefill'] is not True: + continue + _child_id, activity_id, day = booking['id'].split(':') + if _child_id != child_id: + continue + if activity_id not in ['GARDERIES', 'ETUDES']: + continue + legacy_exclusive_activities[day].add(booking['id']) + # check booking exclusivity for changes only + for day, bookings in exclusive_activities.items(): + if len(legacy_exclusive_activities.get(day) or []) > 1: + # it was already booked in Axel ... + continue + if len(bookings) > 1: + raise APIError( + 'not possible to book %s the same day' % ' and '.join(sorted(list(bookings))), + err_code='bad-request', + http_status=400, + ) + + updated = [] + for activity in activities_data.get('ACTIVITE', []): + activity_id = activity['IDENTACTIVITE'] + if activity_id.startswith('CJ'): + # mercredi or vacances: not bookable + continue + bookings = self.set_bookings( + child_id, + activity_id, + start_date, + end_date, + booking_list, + legacy_agenda=current_agenda[activity_id], + ) + for booking in bookings: + booking['activity_label'] = activity['LIBELLEACTIVITE'] + booking['activity_type'] = self.get_activity_type(activity_id) + updated += bookings + + # sort changes + activity_types = ['matin', 'midi', 'soir'] + updated = [ + ( + not u['booked'], + activity_types.index(u['activity_type']), + u['day'], + u, + ) + for u in updated + ] + updated = sorted(updated, key=itemgetter(0, 1, 2)) + updated = [u for b, a, d, u in updated] + updated = [ + {'booked': u['booked'], 'activity_label': u['activity_label'], 'day': u['day']} for u in updated + ] + + return { + 'updated': True, + 'count': len(updated), + 'changes': updated, + } + @endpoint( display_category=_('Schooling'), display_order=8, @@ -875,82 +953,9 @@ class CaluireAxel(BaseResource): continue agenda[activity_id] = self.get_bookings(child_id, activity_id, start_date, end_date) - # check exclusive activities (GARDERIES and ETUDES) - # build list of requested booked days - exclusive_activities = defaultdict(set) - for booking in post_data['booking_list']: - _child_id, activity_id, day = booking.split(':') - if _child_id != child_id: - continue - if activity_id not in ['GARDERIES', 'ETUDES']: - continue - exclusive_activities[day].add(booking) - # build list of existing booked days - legacy_exclusive_activities = defaultdict(set) - for activity_id in ['GARDERIES', 'ETUDES']: - legacy_agenda = agenda.get(activity_id) or [] - for booking in legacy_agenda: - if booking['prefill'] is not True: - continue - _child_id, activity_id, day = booking['id'].split(':') - if _child_id != child_id: - continue - if activity_id not in ['GARDERIES', 'ETUDES']: - continue - legacy_exclusive_activities[day].add(booking['id']) - # check booking exclusivity for changes only - for day, bookings in exclusive_activities.items(): - if len(legacy_exclusive_activities.get(day) or []) > 1: - # it was already booked in Axel ... - continue - if len(bookings) > 1: - raise APIError( - 'not possible to book %s the same day' % ' and '.join(sorted(list(bookings))), - err_code='bad-request', - http_status=400, - ) - - updated = [] - for activity in activities_data.get('ACTIVITE', []): - activity_id = activity['IDENTACTIVITE'] - if activity_id.startswith('CJ'): - # mercredi or vacances: not bookable - continue - bookings = self.set_bookings( - child_id, - activity_id, - start_date, - end_date, - post_data['booking_list'], - legacy_agenda=agenda[activity_id], - ) - for booking in bookings: - booking['activity_label'] = activity['LIBELLEACTIVITE'] - booking['activity_type'] = self.get_activity_type(activity_id) - updated += bookings - - # sort changes - activity_types = ['matin', 'midi', 'soir'] - updated = [ - ( - not u['booked'], - activity_types.index(u['activity_type']), - u['day'], - u, - ) - for u in updated - ] - updated = sorted(updated, key=itemgetter(0, 1, 2)) - updated = [u for b, a, d, u in updated] - updated = [ - {'booked': u['booked'], 'activity_label': u['activity_label'], 'day': u['day']} for u in updated - ] - - return { - 'updated': True, - 'count': len(updated), - 'changes': updated, - } + return self._set_agenda( + child_id, start_date, end_date, activities_data, agenda, post_data['booking_list'] + ) @endpoint( display_category=_('Schooling'),