api: only look in waiting list if there's not enough place in event (#17514)

This commit is contained in:
Frédéric Péters 2017-07-10 18:38:44 +02:00
parent 78cad28c0c
commit 471d8db694
2 changed files with 37 additions and 12 deletions

View File

@ -252,13 +252,14 @@ class Fillslot(GenericAPIView):
new_booking = Booking(event_id=event_pk, extra_data=request.data)
if event.waiting_list_places:
if (event.waiting_list + places_count) > event.waiting_list_places:
return Response({'err': 1, 'reason': 'sold out'})
if (event.booked_places + places_count) > event.places or event.waiting_list:
# if this is full or there are people waiting, put new bookings
# in the waiting list.
new_booking.in_waiting_list = True
if (event.waiting_list + places_count) > event.waiting_list_places:
return Response({'err': 1, 'reason': 'sold out'})
else:
if (event.booked_places + places_count) > event.places:
return Response({'err': 1, 'reason': 'sold out'})

View File

@ -560,13 +560,13 @@ def test_multiple_booking_api(app, some_data, user):
assert resp.json['datetime'] == localtime(event.start_datetime).isoformat()
assert 'accept_url' not in resp.json['api']
assert 'cancel_url' in resp.json['api']
assert event.booked_places == 3
assert Event.objects.get(id=event.id).booked_places == 3
resp2 = app.post('/api/agenda/%s/fillslot/%s/?count=2' % (agenda.slug, event.id))
assert event.booked_places == 5
assert Event.objects.get(id=event.id).booked_places == 5
resp = app.post(resp.json['api']['cancel_url'])
assert event.booked_places == 2
assert Event.objects.get(id=event.id).booked_places == 2
# check available places overflow
event.places = 3
@ -574,20 +574,44 @@ def test_multiple_booking_api(app, some_data, user):
event.save()
resp3 = app.post('/api/agenda/%s/fillslot/%s/?count=5' % (agenda.slug, event.id))
assert event.booked_places == 2
assert event.waiting_list == 5
assert Event.objects.get(id=event.id).booked_places == 2
assert Event.objects.get(id=event.id).waiting_list == 5
# check waiting list overflow
resp = app.post('/api/agenda/%s/fillslot/%s/?count=5' % (agenda.slug, event.id))
assert resp.json['err'] == 1
assert resp.json['reason'] == 'sold out'
assert event.booked_places == 2
assert event.waiting_list == 5
assert Event.objects.get(id=event.id).booked_places == 2
assert Event.objects.get(id=event.id).waiting_list == 5
# accept the waiting list
resp = app.post(resp3.json['api']['accept_url'])
assert event.booked_places == 7
assert event.waiting_list == 0
assert Event.objects.get(id=event.id).booked_places == 7
assert Event.objects.get(id=event.id).waiting_list == 0
# check with a short waiting list
Booking.objects.all().delete()
event.places = 4
event.waiting_list_places = 2
event.save()
resp = app.post('/api/agenda/%s/fillslot/%s/?count=5' % (agenda.slug, event.id))
assert resp.json['err'] == 1
assert resp.json['reason'] == 'sold out'
resp = app.post('/api/agenda/%s/fillslot/%s/?count=3' % (agenda.slug, event.id))
assert resp.json['err'] == 0
assert Event.objects.get(id=event.id).booked_places == 3
assert Event.objects.get(id=event.id).waiting_list == 0
resp = app.post('/api/agenda/%s/fillslot/%s/?count=3' % (agenda.slug, event.id))
assert resp.json['err'] == 1
assert resp.json['reason'] == 'sold out'
resp = app.post('/api/agenda/%s/fillslot/%s/?count=2' % (agenda.slug, event.id))
assert resp.json['err'] == 0
assert Event.objects.get(id=event.id).booked_places == 3
assert Event.objects.get(id=event.id).waiting_list == 2
def test_agenda_detail_api(app, some_data):
agenda = Agenda.objects.get(slug='foo-bar')