api: create real Event object after slot has been checked (#22971)

This commit is contained in:
Frédéric Péters 2018-04-04 10:47:55 +02:00
parent 1c73fa736c
commit 385670124f
1 changed files with 12 additions and 9 deletions

View File

@ -329,19 +329,15 @@ class Fillslot(GenericAPIView):
available_desk = None
if agenda.kind == 'meetings':
# event is actually a timeslot, convert to a real event object
# event_pk is actually a timeslot id (meeting_type:start_datetime);
# split it back to get both parts.
meeting_type_id, start_datetime_str = event_pk.split(':')
start_datetime = make_aware(datetime.datetime.strptime(
start_datetime_str, '%Y-%m-%d-%H%M'))
event = Event.objects.create(agenda=agenda,
meeting_type_id=meeting_type_id,
start_datetime=start_datetime,
full=False, places=1)
slots = get_all_slots(agenda, MeetingType.objects.get(id=meeting_type_id))
# sort available matching slots by desk id
slots = [slot for slot in slots if not slot.full and slot.start_datetime == event.start_datetime]
slots = [slot for slot in slots if not slot.full and slot.start_datetime == start_datetime]
slots.sort(key=lambda x: x.desk.id)
if slots:
# book first available desk
@ -350,8 +346,15 @@ class Fillslot(GenericAPIView):
if not available_desk:
return Response({'err': 1, 'reason': 'no more desk available'})
event.desk = available_desk
event.save()
# booking requires a real Event object (not a lazy Timeslot);
# create it now, with data from the timeslot and the desk we
# found.
event = Event.objects.create(agenda=agenda,
meeting_type_id=meeting_type_id,
start_datetime=start_datetime,
full=False, places=1,
desk=available_desk)
event_pk = event.id
event = Event.objects.filter(id=event_pk)[0]