From 574973c9690f7901a7a77a9598332b2fa4ff4f99 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 26 May 2020 19:36:42 +0200
Subject: [PATCH 2/2] api: return specified end_datetime on event fillslots
 (#37352)

---
 chrono/api/views.py | 20 ++++++++++++--------
 tests/test_api.py   |  5 +++++
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/chrono/api/views.py b/chrono/api/views.py
index 7d1a185..e46ec70 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -894,26 +894,30 @@ class Fillslots(APIView):
             response['desk'] = {'label': available_desk.label, 'slug': available_desk.slug}
         if to_cancel_booking:
             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 event.stop_datetime:
+                response['end_datetime'] = format_response_datetime(event.stop_datetime)
         if agenda.kind == 'events' and multiple_booking:
-            response['events'] = [
-                {
-                    'slug': x.slug,
-                    'text': str(x),
-                    'datetime': format_response_datetime(x.start_datetime),
-                    'description': x.description,
+            response['events'] = []
+            for event in events:
+                event_response = {
+                    'slug': event.slug,
+                    'text': str(event),
+                    'datetime': format_response_datetime(event.start_datetime),
+                    'description': event.description,
                 }
-                for x in events
-            ]
+                if event.stop_datetime:
+                    event_response['end_datetime'] = format_response_datetime(event.stop_datetime)
+                response['events'].append(event_response)
 
         return Response(response)
 
 
 fillslots = Fillslots.as_view()
 
 
 class Fillslot(Fillslots):
diff --git a/tests/test_api.py b/tests/test_api.py
index 77236d2..04c59a8 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -68,16 +68,18 @@ def mock_now(request, monkeypatch):
 @pytest.fixture
 def some_data(time_zone, mock_now):
     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)
     for i in range(3):
         event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda)
+        if i == 0:
+            event.stop_datetime = event.start_datetime + datetime.timedelta(hours=2)
         event.save()
 
     agenda2 = Agenda(label=u'Foo bar 2')
     agenda2.save()
     first_date = localtime(now()).replace(hour=20, minute=0, second=0, microsecond=0)
     first_date += datetime.timedelta(days=1)
     for i in range(2):
         event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda2)
@@ -444,16 +446,17 @@ def test_booking_api(app, some_data, user):
             agenda.slug,
             event.slug or event.id,
         )
 
     app.authorization = ('Basic', ('john.doe', 'password'))
     resp = app.post('/api/agenda/%s/fillslot/%s/' % (agenda.slug, event.id))
     Booking.objects.get(id=resp.json['booking_id'])
     assert resp.json['datetime'] == localtime(event.start_datetime).strftime('%Y-%m-%d %H:%M:%S')
+    assert resp.json['end_datetime'] == localtime(event.stop_datetime).strftime('%Y-%m-%d %H:%M:%S')
     assert 'accept_url' not in resp.json['api']
     assert 'suspend_url' in resp.json['api']
     assert 'cancel_url' in resp.json['api']
     assert 'ics_url' in resp.json['api']
     assert urlparse.urlparse(resp.json['api']['suspend_url']).netloc
     assert urlparse.urlparse(resp.json['api']['cancel_url']).netloc
     assert urlparse.urlparse(resp.json['api']['ics_url']).netloc
     assert Booking.objects.count() == 1
@@ -758,16 +761,18 @@ def test_booking_api_fillslots_slots_string_param(app, some_data, user):
     assert resp.json['err_class'] == 'invalid payload'
     assert resp.json['err_desc'] == 'invalid payload'
 
     slots_string_param = ','.join([str(e) for e in events_ids])
     resp = app.post_json('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots_string_param})
     primary_booking_id = resp.json['booking_id']
     Booking.objects.get(id=primary_booking_id)
     assert Booking.objects.count() == 3
+    assert resp.json['events'][0]['end_datetime']
+    assert not resp.json['events'][1].get('end_datetime')
 
 
 def test_booking_api_meeting(app, meetings_agenda, user):
     agenda_id = meetings_agenda.slug
     meeting_type = MeetingType.objects.get(agenda=meetings_agenda)
     resp = app.get('/api/agenda/meetings/%s/datetimes/' % meeting_type.id)
     event_id = resp.json['data'][2]['id']
     assert urlparse.urlparse(
-- 
2.26.2

