From d131181c4eefcec1c19e00667d2092b492d8b93d Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 1 Dec 2022 17:34:43 +0100 Subject: [PATCH] agendas: add date end field to shared custody agenda (#71633) --- .../0144_sharedcustodyagenda_date_end.py | 24 ++++++++++++++ chrono/agendas/models.py | 8 +++++ .../datetimes/test_events_multiple_agendas.py | 3 +- tests/test_agendas.py | 32 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 chrono/agendas/migrations/0144_sharedcustodyagenda_date_end.py diff --git a/chrono/agendas/migrations/0144_sharedcustodyagenda_date_end.py b/chrono/agendas/migrations/0144_sharedcustodyagenda_date_end.py new file mode 100644 index 00000000..0c0aaf0e --- /dev/null +++ b/chrono/agendas/migrations/0144_sharedcustodyagenda_date_end.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.26 on 2022-11-30 11:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('agendas', '0143_shared_custody_remove_children_field'), + ] + + operations = [ + migrations.AddField( + model_name='sharedcustodyagenda', + name='date_end', + field=models.DateField(null=True, verbose_name='End'), + ), + migrations.AddConstraint( + model_name='sharedcustodyagenda', + constraint=models.UniqueConstraint( + condition=models.Q(date_end__isnull=True), fields=('child',), name='unique_child_no_date_end' + ), + ), + ] diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index 1fcd8d1f..e460eb5a 100644 --- a/chrono/agendas/models.py +++ b/chrono/agendas/models.py @@ -3224,6 +3224,14 @@ class SharedCustodyAgenda(models.Model): ) child = models.ForeignKey(Person, verbose_name=_('Child'), on_delete=models.CASCADE, related_name='+') date_start = models.DateField(_('Start')) + date_end = models.DateField(_('End'), null=True) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=['child'], condition=Q(date_end__isnull=True), name='unique_child_no_date_end' + ) + ] @property def label(self): diff --git a/tests/api/datetimes/test_events_multiple_agendas.py b/tests/api/datetimes/test_events_multiple_agendas.py index 225768cc..d8802edc 100644 --- a/tests/api/datetimes/test_events_multiple_agendas.py +++ b/tests/api/datetimes/test_events_multiple_agendas.py @@ -423,7 +423,8 @@ def test_datetimes_multiple_agendas_queries(app): first_guardian=father, second_guardian=mother, child=child, - date_start=now() - datetime.timedelta(days=5 + i), + date_start=now() - datetime.timedelta(days=5 + 2 * i), + date_end=now() - datetime.timedelta(days=5 + 2 * i + 1), ) SharedCustodyRule.objects.create(agenda=agenda, guardian=father, days=list(range(7)), weeks='even') diff --git a/tests/test_agendas.py b/tests/test_agendas.py index a54434e8..fee11abd 100644 --- a/tests/test_agendas.py +++ b/tests/test_agendas.py @@ -8,6 +8,7 @@ import requests from django.contrib.auth.models import Group, User from django.core.files.base import ContentFile from django.core.management import call_command +from django.db import IntegrityError, transaction from django.db.models import Q from django.test import override_settings from django.utils.timezone import localtime, make_aware, now @@ -3402,3 +3403,34 @@ def test_shared_custody_agenda_update_holiday_rules_command(): 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) + + +def test_shared_custody_agenda_unique_child_no_date_end(): + father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe') + mother = Person.objects.create(user_external_id='mother_id', first_name='Jane', last_name='Doe') + child = Person.objects.create(user_external_id='child_id', first_name='James', last_name='Doe') + SharedCustodyAgenda.objects.create( + first_guardian=father, + second_guardian=mother, + child=child, + date_start=datetime.date(2020, 1, 1), + date_end=datetime.date(2021, 1, 1), + ) + agenda = SharedCustodyAgenda.objects.create( + first_guardian=father, second_guardian=mother, child=child, date_start=datetime.date(2022, 1, 1) + ) + + with pytest.raises(IntegrityError): + with transaction.atomic(): + SharedCustodyAgenda.objects.create( + first_guardian=father, + second_guardian=mother, + child=child, + date_start=datetime.date(2023, 1, 1), + ) + + agenda.date_end = datetime.date(2022, 6, 1) + agenda.save() + SharedCustodyAgenda.objects.create( + first_guardian=father, second_guardian=mother, child=child, date_start=datetime.date(2023, 1, 1) + )