agendas: add command to update shared custody holiday rules (#62801)

This commit is contained in:
Valentin Deniaud 2022-03-29 16:28:39 +02:00
parent 22d3a7e023
commit 5ca2fa0e91
3 changed files with 73 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
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()

2
debian/uwsgi.ini vendored
View File

@ -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

View File

@ -3315,3 +3315,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)