manager: prefill presence check form with unexpected presence (#88039)
gitea/chrono/pipeline/head This commit looks good Details

This commit is contained in:
Lauréline Guérin 2024-03-12 16:44:07 +01:00
parent 701733da57
commit e0f1d9541d
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 27 additions and 2 deletions

View File

@ -589,12 +589,17 @@ class BookingCheckPresenceForm(forms.Form):
def __init__(self, *args, **kwargs):
agenda = kwargs.pop('agenda')
subscription = kwargs.pop('subscription', False)
super().__init__(*args, **kwargs)
check_types = get_agenda_check_types(agenda)
self.presence_check_types = [ct for ct in check_types if ct.kind == 'presence']
self.fields['check_type'].choices = [('', '---------')] + [
(ct.slug, ct.label) for ct in self.presence_check_types
]
if not self.initial and subscription:
unexpected_presences = [ct for ct in check_types if ct.unexpected_presence]
if unexpected_presences:
self.initial['check_type'] = unexpected_presences[0].slug
class PartialBookingCheckForm(forms.ModelForm):

View File

@ -1759,6 +1759,7 @@ class EventChecksMixin:
)
subscription.presence_form = BookingCheckPresenceForm(
agenda=self.agenda,
subscription=True,
)
# sort results
if (

View File

@ -62,6 +62,7 @@ class CheckType:
slug: str
label: str
kind: str
unexpected_presence: bool = False
def get_agenda_check_types(agenda):
@ -73,5 +74,12 @@ def get_agenda_check_types(agenda):
check_types = []
for ct in result['data']:
check_types.append(CheckType(slug=ct['id'], label=ct['text'], kind=ct['kind']))
check_types.append(
CheckType(
slug=ct['id'],
label=ct['text'],
kind=ct['kind'],
unexpected_presence=ct.get('unexpected_presence') or False,
)
)
return check_types

View File

@ -2482,11 +2482,12 @@ def test_event_check_booking(check_types, app, admin_user):
check_types.return_value = [
CheckType(slug='foo-reason', label='Foo reason', kind='absence'),
CheckType(slug='bar-reason', label='Bar reason', kind='presence'),
CheckType(slug='bar-reason', label='Bar reason', kind='presence', unexpected_presence=True),
]
resp = app.get('/manage/agendas/%s/events/%s/check' % (agenda.pk, event.pk))
assert len(resp.pyquery.find('td.booking-actions form.absence select')) == 1
assert len(resp.pyquery.find('td.booking-actions form.presence select')) == 1
assert resp.pyquery.find('td.booking-actions form.presence option:selected').text() == '---------'
# reset
_test_reset()
@ -2833,6 +2834,14 @@ def test_event_check_subscription(check_types, app, admin_user):
resp = app.get('/manage/agendas/%s/events/%s/check' % (agenda.pk, event.pk))
assert '/manage/agendas/%s/subscriptions/%s/presence/%s' % (agenda.pk, subscription.pk, event.pk) in resp
assert '/manage/agendas/%s/subscriptions/%s/absence/%s' % (agenda.pk, subscription.pk, event.pk) in resp
assert resp.pyquery.find('td.booking-actions form.presence option:selected').text() == '---------'
check_types.return_value = [
CheckType(slug='foo-reason', label='Foo reason', kind='absence'),
CheckType(slug='bar-reason', label='Bar reason', kind='presence'),
CheckType(slug='baz-reason', label='Baz reason', kind='presence', unexpected_presence=True),
]
resp = app.get('/manage/agendas/%s/events/%s/check' % (agenda.pk, event.pk))
assert resp.pyquery.find('td.booking-actions form.presence option:selected').text() == 'Baz reason'
app.post(
'/manage/agendas/%s/subscriptions/%s/presence/%s' % (agenda.pk, subscription.pk, event.pk),
params={'csrfmiddlewaretoken': token, 'check_type': 'bar-reason'},

View File

@ -36,6 +36,7 @@ def test_get_weekday_index():
CHECK_TYPES_DATA = [
{'id': 'bar-reason', 'kind': 'presence', 'text': 'Bar reason'},
{'id': 'baz-reason', 'kind': 'presence', 'text': 'Baz reason', 'unexpected_presence': True},
{'id': 'foo-reason', 'kind': 'absence', 'text': 'Foo reason'},
]
@ -92,6 +93,7 @@ def test_get_agenda_check_types():
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
assert get_agenda_check_types(agenda) == [
CheckType(slug='bar-reason', label='Bar reason', kind='presence'),
CheckType(slug='baz-reason', label='Baz reason', kind='presence', unexpected_presence=True),
CheckType(slug='foo-reason', label='Foo reason', kind='absence'),
]