From aecfa3c5cca97e9f268bf732a2cc9dc86175edd3 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 24 Nov 2022 17:57:38 +0100 Subject: [PATCH] manager: forbid empty days field on shared custody rule creation (#71605) --- chrono/manager/forms.py | 4 ++-- tests/manager/test_shared_custody_agenda.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py index d7173a13..8f932d7d 100644 --- a/chrono/manager/forms.py +++ b/chrono/manager/forms.py @@ -1498,7 +1498,7 @@ class SharedCustodyRuleForm(forms.ModelForm): days = forms.TypedMultipleChoiceField( choices=WEEKDAY_CHOICES, coerce=int, - required=False, + required=True, widget=WeekdaysWidget, label=_('Days'), ) @@ -1518,7 +1518,7 @@ class SharedCustodyRuleForm(forms.ModelForm): cleaned_data = super().clean() if self.instance.agenda.rule_overlaps( - days=cleaned_data['days'], weeks=cleaned_data['weeks'], instance=self.instance + days=cleaned_data.get('days', []), weeks=cleaned_data['weeks'], instance=self.instance ): raise ValidationError(_('Rule overlaps existing rules.')) diff --git a/tests/manager/test_shared_custody_agenda.py b/tests/manager/test_shared_custody_agenda.py index 4f6f7ec2..7e69de47 100644 --- a/tests/manager/test_shared_custody_agenda.py +++ b/tests/manager/test_shared_custody_agenda.py @@ -82,6 +82,27 @@ def test_shared_custody_agenda_settings_rules(app, admin_user): assert SharedCustodyRule.objects.count() == 1 +def test_shared_custody_agenda_settings_rules_require_days(app, admin_user): + 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') + agenda = SharedCustodyAgenda.objects.create( + first_guardian=father, second_guardian=mother, date_start=now() + ) + + app = login(app) + resp = app.get('/manage/shared-custody/%s/settings/' % agenda.pk) + + resp = resp.click('Add custody rule') + resp.form['guardian'] = father.pk + resp.form['weeks'] = 'even' + resp = resp.form.submit() + assert 'This field is required.' in resp.text + + resp.form['days'] = [0] + resp.form.submit().follow() + assert SharedCustodyRule.objects.count() == 1 + + @pytest.mark.freeze_time('2022-02-22 14:00') # Tuesday def test_shared_custody_agenda_settings_periods(app, admin_user): father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe') -- 2.39.2