From 5d2152357bbeb40480b4df2a897c1520a43bc591 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 29 Mar 2022 16:28:39 +0200 Subject: [PATCH 4/4] agendas: add command to update shared custody holiday rules (#62801) --- .../update_shared_custody_holiday_rules.py | 27 ++++++++++++ debian/uwsgi.ini | 2 + tests/test_agendas.py | 44 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 chrono/agendas/management/commands/update_shared_custody_holiday_rules.py diff --git a/chrono/agendas/management/commands/update_shared_custody_holiday_rules.py b/chrono/agendas/management/commands/update_shared_custody_holiday_rules.py new file mode 100644 index 00000000..78c84e25 --- /dev/null +++ b/chrono/agendas/management/commands/update_shared_custody_holiday_rules.py @@ -0,0 +1,27 @@ +# chrono - agendas system +# Copyright (C) 2022 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.core.management.base import BaseCommand + +from chrono.agendas.models import SharedCustodyHolidayRule + + +class Command(BaseCommand): + help = 'Update shared custody holiday rules' + + def handle(self, **options): + for rule in SharedCustodyHolidayRule.objects.all(): + rule.update_or_create_periods() diff --git a/debian/uwsgi.ini b/debian/uwsgi.ini index 1b17d8c1..00268dbd 100644 --- a/debian/uwsgi.ini +++ b/debian/uwsgi.ini @@ -27,6 +27,8 @@ cron = 1 -1 -1 -1 -1 /usr/bin/chrono-manage tenant_command send_booking_reminder cron = 1 -1 -1 -1 -1 /usr/bin/chrono-manage tenant_command sync_desks_timeperiod_exceptions --all-tenants # daily cron = 2 4 -1 -1 -1 /usr/bin/chrono-manage tenant_command anonymize_bookings --all-tenant +# monthly +cron = 1 1 1 -1 -1 /usr/bin/chrono-manage tenant_command update_shared_custody_holiday_rules --all-tenant # every 01/01 at 1:1 cron = 1 1 1 1 -1 /usr/bin/chrono-manage tenant_command sync_desks_timeperiod_exceptions_from_settings --all-tenants -v0 diff --git a/tests/test_agendas.py b/tests/test_agendas.py index 3675babc..af400638 100644 --- a/tests/test_agendas.py +++ b/tests/test_agendas.py @@ -3308,3 +3308,47 @@ def test_shared_custody_agenda_holiday_rules_application(): ('28/12', 'Jane Doe'), ('29/12', 'John Doe'), ] + + +def test_shared_custody_agenda_update_holiday_rules_command(): + calendar = UnavailabilityCalendar.objects.create(label='Calendar') + + father = Person.objects.create(user_external_id='father_id', name='John Doe') + mother = Person.objects.create(user_external_id='mother_id', name='Jane Doe') + agenda = SharedCustodyAgenda.objects.create(first_guardian=father, second_guardian=mother) + + christmas_holiday = TimePeriodExceptionGroup.objects.create( + unavailability_calendar=calendar, label='Christmas', slug='christmas' + ) + exception = TimePeriodException.objects.create( + unavailability_calendar=calendar, + start_datetime=make_aware(datetime.datetime(year=2021, month=12, day=18, hour=0, minute=0)), + end_datetime=make_aware(datetime.datetime(year=2022, month=1, day=3, hour=0, minute=0)), + group=christmas_holiday, + ) + + SharedCustodyRule.objects.create(agenda=agenda, days=list(range(7)), weeks='even', guardian=father) + SharedCustodyRule.objects.create(agenda=agenda, days=list(range(7)), weeks='odd', guardian=mother) + + rule = SharedCustodyHolidayRule.objects.create(agenda=agenda, guardian=father, holiday=christmas_holiday) + rule.update_or_create_periods() + + period = SharedCustodyPeriod.objects.get() + assert period.date_start == datetime.date(year=2021, month=12, day=18) + assert period.date_end == datetime.date(year=2022, month=1, day=3) + + exception.start_datetime += datetime.timedelta(days=1) + exception.save() + TimePeriodException.objects.create( + unavailability_calendar=calendar, + start_datetime=make_aware(datetime.datetime(year=2022, month=12, day=18, hour=0, minute=0)), + end_datetime=make_aware(datetime.datetime(year=2023, month=1, day=3, hour=0, minute=0)), + group=christmas_holiday, + ) + + call_command('update_shared_custody_holiday_rules') + period1, period2 = SharedCustodyPeriod.objects.all() + assert period1.date_start == datetime.date(year=2021, month=12, day=19) + assert period1.date_end == datetime.date(year=2022, month=1, day=3) + assert period2.date_start == datetime.date(year=2022, month=12, day=18) + assert period2.date_end == datetime.date(year=2023, month=1, day=3) -- 2.30.2