manager: forbid empty days field on shared custody rule creation (#71605)
gitea-wip/chrono/pipeline/pr-main This commit looks good Details
gitea-wip/chrono/pipeline/head There was a failure building this commit Details
gitea/chrono/pipeline/head Build started... Details

This commit is contained in:
Valentin Deniaud 2022-11-24 17:57:38 +01:00
parent ade248abf0
commit aecfa3c5cc
2 changed files with 23 additions and 2 deletions

View File

@ -1498,7 +1498,7 @@ class SharedCustodyRuleForm(forms.ModelForm):
days = forms.TypedMultipleChoiceField( days = forms.TypedMultipleChoiceField(
choices=WEEKDAY_CHOICES, choices=WEEKDAY_CHOICES,
coerce=int, coerce=int,
required=False, required=True,
widget=WeekdaysWidget, widget=WeekdaysWidget,
label=_('Days'), label=_('Days'),
) )
@ -1518,7 +1518,7 @@ class SharedCustodyRuleForm(forms.ModelForm):
cleaned_data = super().clean() cleaned_data = super().clean()
if self.instance.agenda.rule_overlaps( 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.')) raise ValidationError(_('Rule overlaps existing rules.'))

View File

@ -82,6 +82,27 @@ def test_shared_custody_agenda_settings_rules(app, admin_user):
assert SharedCustodyRule.objects.count() == 1 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 @pytest.mark.freeze_time('2022-02-22 14:00') # Tuesday
def test_shared_custody_agenda_settings_periods(app, admin_user): 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') father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe')