agendas: add date end field to shared custody agenda (#71633)

This commit is contained in:
Valentin Deniaud 2022-12-01 17:34:43 +01:00 committed by Gitea
parent dca88c5900
commit d131181c4e
4 changed files with 66 additions and 1 deletions

View File

@ -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'
),
),
]

View File

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

View File

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

View File

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