api: count correctly requested places on multiple booking (#38306)

This commit is contained in:
Emmanuel Cazenave 2019-12-16 16:50:38 +01:00
parent 5f40de6f79
commit 7c6528f3b8
2 changed files with 46 additions and 1 deletions

View File

@ -496,7 +496,10 @@ class Fillslots(APIView):
if to_cancel_booking.cancellation_datetime:
cancel_error = gettext_noop('cancel booking: booking already cancelled')
else:
to_cancel_places_count = to_cancel_booking.secondary_booking_set.count() + 1
to_cancel_places_count = (
to_cancel_booking.secondary_booking_set.filter(event=to_cancel_booking.event).count()
+ 1
)
if places_count != to_cancel_places_count:
cancel_error = gettext_noop('cancel booking: count is different')
except Booking.DoesNotExist:

View File

@ -1467,6 +1467,48 @@ def test_multiple_booking_api_fillslots(app, some_data, user):
assert Event.objects.get(id=event.id).waiting_list == 2
def test_multiple_booking_move_booking(app, user):
agenda = Agenda(label=u'Foo bar')
agenda.save()
first_date = localtime(now()).replace(hour=17, minute=0, second=0, microsecond=0)
first_date += datetime.timedelta(days=1)
events = []
for i in range(10):
event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda)
event.save()
events.append(event)
first_two_events = events[:2]
events_ids = [x.id for x in first_two_events]
resp_datetimes = app.get('/api/agenda/%s/datetimes/' % agenda.id)
slots = [x['id'] for x in resp_datetimes.json['data'] if x['id'] in events_ids]
app.authorization = ('Basic', ('john.doe', 'password'))
# get 1 place on 2 slots
resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots})
booking = Booking.objects.get(id=resp.json['booking_id'])
assert Booking.objects.filter(primary_booking=booking).count() == 1
for event in first_two_events:
assert Event.objects.get(id=event.id).booked_places == 1
# change, 1 place on 2 other slots
last_two_events = events[-2:]
events_ids = [x.id for x in last_two_events]
resp_datetimes = app.get('/api/agenda/%s/datetimes/' % agenda.id)
slots = [x['id'] for x in resp_datetimes.json['data'] if x['id'] in events_ids]
resp = app.post(
'/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots, 'cancel_booking_id': booking.pk}
)
booking = Booking.objects.get(id=resp.json['booking_id'])
assert Booking.objects.filter(primary_booking=booking).count() == 1
for event in first_two_events:
assert Event.objects.get(id=event.id).booked_places == 0
for event in last_two_events:
assert Event.objects.get(id=event.id).booked_places == 1
def test_agenda_detail_api(app, some_data):
agenda = Agenda.objects.get(slug='foo-bar')
resp = app.get('/api/agenda/%s/' % agenda.slug)