manager: show holiday name on shared custody month view (#63543)

This commit is contained in:
Valentin Deniaud 2022-04-06 11:10:27 +02:00
parent a36ab59498
commit f497445833
2 changed files with 57 additions and 5 deletions

View File

@ -3153,9 +3153,13 @@ class Person(models.Model):
class SharedCustodySlot:
guardian: Person = dataclasses.field(compare=False)
date: datetime.date
label: str = dataclasses.field(compare=False, default='')
def __str__(self):
return self.guardian.name
if self.label:
return '%s (%s)' % (self.guardian.name, self.label)
else:
return self.guardian.name
class SharedCustodyAgenda(models.Model):
@ -3183,16 +3187,19 @@ class SharedCustodyAgenda(models.Model):
def get_custody_slots(self, min_date, max_date):
slots = set()
periods = self.periods.filter(date_start__lt=max_date, date_end__gt=min_date).order_by(
'-holiday_rule'
periods = (
self.periods.filter(date_start__lt=max_date, date_end__gt=min_date)
.order_by('-holiday_rule')
.select_related('holiday_rule__holiday', 'guardian')
)
for period in periods:
date = max(period.date_start, min_date)
label = period.holiday_rule.holiday.label if period.holiday_rule else ''
while date < period.date_end and date < max_date:
slots.add(SharedCustodySlot(guardian=period.guardian, date=date))
slots.add(SharedCustodySlot(guardian=period.guardian, date=date, label=label))
date += datetime.timedelta(days=1)
for rule in self.rules.all():
for rule in self.rules.all().select_related('guardian'):
slots.update(rule.get_slots(min_date, max_date))
slots = sorted(slots, key=lambda x: x.date)

View File

@ -2,6 +2,8 @@ import datetime
import pytest
from django.core.files.base import ContentFile
from django.db import connection
from django.test.utils import CaptureQueriesContext
from chrono.agendas.models import (
Person,
@ -168,6 +170,42 @@ def test_shared_custody_agenda_month_view(app, admin_user):
app.get('/manage/shared-custody/%s/42/42/' % agenda.pk, status=404)
def test_shared_custody_agenda_month_view_queries(app, admin_user):
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)
SharedCustodyRule.objects.create(agenda=agenda, guardian=father, days=[0, 1, 2], weeks='even')
SharedCustodyRule.objects.create(agenda=agenda, guardian=father, days=[3, 4, 5, 6], weeks='odd')
SharedCustodyRule.objects.create(agenda=agenda, guardian=mother, days=[0, 1, 2], weeks='odd')
SharedCustodyRule.objects.create(agenda=agenda, guardian=mother, days=[3, 4, 5, 6], weeks='even')
for i in range(1, 10):
SharedCustodyPeriod.objects.create(
agenda=agenda,
guardian=mother,
date_start=datetime.date(year=2022, month=12, day=i),
date_end=datetime.date(year=2022, month=12, day=i + 1),
)
# configure holidays
unavailability_calendar = UnavailabilityCalendar.objects.create(label='Calendar', slug='chrono-holidays')
source = unavailability_calendar.timeperiodexceptionsource_set.create(
ics_filename='holidays.ics', ics_file=ContentFile(ICS_HOLIDAYS, name='holidays.ics')
)
source.refresh_timeperiod_exceptions_from_ics()
app = login(app)
resp = app.get('/manage/shared-custody/%s/settings/' % agenda.pk)
resp = resp.click('Add custody rule during holidays')
resp.form['guardian'] = father.pk
resp.form['holiday'].select(text='Vacances de Noël')
resp = resp.form.submit().follow()
with CaptureQueriesContext(connection) as ctx:
app.get('/manage/shared-custody/%s/2022/12/' % agenda.pk)
assert len(ctx.captured_queries) == 9
def test_shared_custody_agenda_holiday_rules(app, admin_user):
father = Person.objects.create(user_external_id='father_id', name='John Doe')
mother = Person.objects.create(user_external_id='mother_id', name='Jane Doe')
@ -241,3 +279,10 @@ def test_shared_custody_agenda_holiday_rules(app, admin_user):
resp = resp.click('Add custody rule during holidays')
assert [x[2] for x in resp.form['holiday'].options] == ['---------', 'Vacances de Noël']
# holiday name is shown on month view
SharedCustodyRule.objects.create(agenda=agenda, guardian=father, days=list(range(7)), weeks='even')
SharedCustodyRule.objects.create(agenda=agenda, guardian=mother, days=list(range(7)), weeks='odd')
resp = app.get('/manage/shared-custody/%s/2022/12/' % agenda.pk)
assert 'John Doe (Vacances de Noël)' in resp.text