manager: can not change check_type_group if bookings are using it (#63847)

This commit is contained in:
Lauréline Guérin 2022-04-15 14:48:36 +02:00
parent e2cd0d6ad6
commit 4b842886b0
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 16 additions and 1 deletions

View File

@ -1317,6 +1317,9 @@ class AgendaBookingCheckSettingsForm(forms.ModelForm):
super().__init__(*args, **kwargs)
if not CheckTypeGroup.objects.exists():
del self.fields['check_type_group']
elif Booking.objects.filter(event__agenda=self.instance, user_check_type__isnull=False).exists():
# not possible to update check_type_group if bookings with non null check_type exist
del self.fields['check_type_group']
class AgendaNotificationsForm(forms.ModelForm):

View File

@ -1,6 +1,7 @@
import pytest
from django.utils.timezone import now
from chrono.agendas.models import Agenda, CheckType, CheckTypeGroup, Desk
from chrono.agendas.models import Agenda, Booking, CheckType, CheckTypeGroup, Desk, Event
from tests.utils import login
pytestmark = pytest.mark.django_db
@ -265,6 +266,8 @@ def test_meetings_agenda_group(app, admin_user):
def test_agenda_group(app, admin_user):
agenda = Agenda.objects.create(label='Foo bar', kind='events')
Desk.objects.create(agenda=agenda, slug='_exceptions_holder')
event = Event.objects.create(agenda=agenda, start_datetime=now(), places=10)
booking = Booking.objects.create(event=event)
app = login(app)
resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
@ -276,6 +279,7 @@ def test_agenda_group(app, admin_user):
assert 'check_type_group' not in resp.context['form'].fields
group = CheckTypeGroup.objects.create(label='Foo bar')
check_type = CheckType.objects.create(label='Foo reason', group=group)
resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
assert 'has_check_types' in resp.context
assert resp.context['has_check_types'] is True
@ -283,4 +287,12 @@ def test_agenda_group(app, admin_user):
resp = resp.click(href='/manage/agendas/%s/check-options' % agenda.pk)
resp.form['check_type_group'] = group.pk
resp = resp.form.submit().follow()
agenda.refresh_from_db()
assert agenda.check_type_group == group
assert 'Check type group: Foo bar' in resp
# cannot change check_type group booking with non null check_type exists
booking.user_check_type = check_type
booking.save()
resp = app.get('/manage/agendas/%s/check-options' % agenda.pk)
assert 'check_type_group' not in resp.context['form'].fields