manager: fix meetingtype edition & slug unicity (#51093)

This commit is contained in:
Lauréline Guérin 2021-02-12 15:48:06 +01:00
parent f32f64ea54
commit 677a33555a
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 37 additions and 11 deletions

View File

@ -228,6 +228,14 @@ class MeetingTypeForm(forms.ModelForm):
model = MeetingType
exclude = ['agenda', 'deleted']
def clean_slug(self):
slug = self.cleaned_data['slug']
if self.instance.agenda.meetingtype_set.filter(slug=slug).exclude(pk=self.instance.pk).exists():
raise ValidationError(_('Another meeting type exists with the same identifier.'))
return slug
def clean(self):
super().clean()
for virtual_agenda in self.instance.agenda.virtual_agendas.all():

View File

@ -1958,8 +1958,7 @@ def test_add_meetings_agenda(app, admin_user):
def test_meetings_agenda_add_meeting_type(app, admin_user):
agenda = Agenda(label=u'Foo bar', kind='meetings')
agenda.save()
agenda = Agenda.objects.create(label=u'Foo bar', kind='meetings')
app = login(app)
resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
assert "This agenda doesn't have any meeting type yet." in resp.text
@ -1974,20 +1973,39 @@ def test_meetings_agenda_add_meeting_type(app, admin_user):
resp.form['duration'] = '60'
resp = resp.form.submit()
meeeting_type = MeetingType.objects.get(agenda=agenda)
assert meeeting_type.label == 'Blah'
assert meeeting_type.duration == 60
assert meeeting_type.deleted is False
meeting_type = MeetingType.objects.get(agenda=agenda)
assert meeting_type.label == 'Blah'
assert meeting_type.duration == 60
assert meeting_type.deleted is False
resp = resp.follow()
assert 'Blah' in resp.text
# and edit
resp = resp.click('Blah')
resp.form['duration'] = '30'
def test_meetings_agenda_edit_meeting_type(app, admin_user):
agenda = Agenda.objects.create(label=u'Foo bar', kind='meetings')
meeting_type = MeetingType.objects.create(agenda=agenda, label='Blah')
meeting_type2 = MeetingType.objects.create(agenda=agenda, label='Other')
other_agenda = Agenda.objects.create(label=u'Foo bar', kind='meetings')
other_meeting_type = MeetingType.objects.create(agenda=other_agenda, label='Blah')
assert meeting_type.slug == other_meeting_type.slug
app = login(app)
resp = app.get('/manage/meetingtypes/%s/edit' % meeting_type.pk)
resp.form['label'] = 'Foo'
resp.form['duration'] = '120'
assert 'deleted' not in resp.form.fields
resp.form.submit().follow()
meeting_type.refresh_from_db()
assert meeting_type.label == 'Foo'
assert meeting_type.slug == other_meeting_type.slug
assert meeting_type.duration == 120
assert meeting_type.deleted is False
# check slug edition
resp = app.get('/manage/meetingtypes/%s/edit' % meeting_type.pk)
resp.form['slug'] = meeting_type2.slug
resp = resp.form.submit()
meeeting_type = MeetingType.objects.get(agenda=agenda)
assert meeeting_type.duration == 30
assert resp.context['form'].errors['slug'] == ['Another meeting type exists with the same identifier.']
def test_meetings_agenda_delete_meeting_type(app, admin_user):