api: add more infos on places for an event agenda (#40412)

This commit is contained in:
Emmanuel Cazenave 2020-03-04 17:25:45 +01:00
parent bf693322be
commit 2748439c23
2 changed files with 21 additions and 1 deletions

View File

@ -176,15 +176,22 @@ def get_agenda_detail(request, agenda):
def get_event_places(event):
available = event.places - event.booked_places
places = {
'total': event.places,
'reserved': event.booked_places,
'available': event.places - event.booked_places,
'available': available,
'full': event.full,
'has_waiting_list': False,
}
if event.waiting_list_places:
places['has_waiting_list'] = True
places['waiting_list_total'] = event.waiting_list_places
places['waiting_list_reserved'] = event.waiting_list
places['waiting_list_available'] = event.waiting_list_places - event.waiting_list
places['waiting_list_activated'] = event.waiting_list > 0 or available <= 0
# 'waiting_list_activated' means next booking will go into the waiting list
return places
@ -766,6 +773,8 @@ class Fillslots(APIView):
response['cancelled_booking_id'] = cancelled_booking_id
if agenda.kind == 'events' and not multiple_booking:
event = events[0]
# event.full is not up to date, it might have been changed by previous new_booking.save().
event.refresh_from_db()
response['places'] = get_event_places(event)
if agenda.kind == 'events' and multiple_booking:
response['events'] = [

View File

@ -971,6 +971,7 @@ def test_booking_api_available(app, some_data, meetings_agenda, user):
assert resp.json['places']['total'] == 10
assert resp.json['places']['available'] == 9
assert resp.json['places']['reserved'] == 1
assert resp.json['places']['full'] is False
assert 'waiting_list_total' not in resp.json['places']
Booking.objects.create(event=event, in_waiting_list=True)
@ -982,9 +983,11 @@ def test_booking_api_available(app, some_data, meetings_agenda, user):
assert resp.json['places']['total'] == 10
assert resp.json['places']['available'] == 9
assert resp.json['places']['reserved'] == 1
assert resp.json['places']['full'] is False
assert resp.json['places']['waiting_list_total'] == 5
assert resp.json['places']['waiting_list_available'] == 3
assert resp.json['places']['waiting_list_reserved'] == 2
assert resp.json['places']['waiting_list_activated'] is True
# not for mettings agenda
meeting_type = MeetingType.objects.get(agenda=meetings_agenda)
@ -1029,9 +1032,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
assert resp.json['places']['total'] == 10
assert resp.json['places']['available'] == 9
assert resp.json['places']['reserved'] == 1
assert resp.json['places']['full'] is False
assert resp.json['places']['waiting_list_total'] == 2
assert resp.json['places']['waiting_list_available'] == 2
assert resp.json['places']['waiting_list_reserved'] == 0
assert resp.json['places']['waiting_list_activated'] is False
# add another booking
resp = app.post_json(
@ -1041,9 +1046,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
assert resp.json['places']['total'] == 10
assert resp.json['places']['available'] == 8
assert resp.json['places']['reserved'] == 2
assert resp.json['places']['full'] is False
assert resp.json['places']['waiting_list_total'] == 2
assert resp.json['places']['waiting_list_available'] == 2
assert resp.json['places']['waiting_list_reserved'] == 0
assert resp.json['places']['waiting_list_activated'] is False
# add a booking, but in waiting list
resp = app.post_json(
@ -1053,9 +1060,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
assert resp.json['places']['total'] == 10
assert resp.json['places']['available'] == 8
assert resp.json['places']['reserved'] == 2
assert resp.json['places']['full'] is False
assert resp.json['places']['waiting_list_total'] == 2
assert resp.json['places']['waiting_list_available'] == 1
assert resp.json['places']['waiting_list_reserved'] == 1
assert resp.json['places']['waiting_list_activated'] is True
# add a booking => booked in waiting list
resp = app.post_json('/api/agenda/%s/fillslot/%s/' % (agenda.pk, event.pk))
@ -1063,9 +1072,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
assert resp.json['places']['total'] == 10
assert resp.json['places']['available'] == 8
assert resp.json['places']['reserved'] == 2
assert resp.json['places']['full'] is True
assert resp.json['places']['waiting_list_total'] == 2
assert resp.json['places']['waiting_list_available'] == 0
assert resp.json['places']['waiting_list_reserved'] == 2
assert resp.json['places']['waiting_list_activated'] is True
# waiting list is full
resp = app.post_json(