From a215db0faf066e356b68201c07db24979707ae24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 3 Dec 2020 16:14:30 +0100
Subject: [PATCH] api: complete slot_status endpoint result (#48877)

---
 chrono/api/views.py | 73 ++++++++++++++++++++++-----------------------
 tests/test_api.py   | 17 +++++++++++
 2 files changed, 53 insertions(+), 37 deletions(-)

diff --git a/chrono/api/views.py b/chrono/api/views.py
index d134495..e9f516b 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -363,6 +363,40 @@ def get_event_places(event):
     return places
 
 
+def get_event_detail(request, event, agenda=None):
+    agenda = agenda or event.agenda
+    return {
+        'id': event.slug,
+        'slug': event.slug,  # kept for compatibility
+        'text': force_text(event),
+        'datetime': format_response_datetime(event.start_datetime),
+        'description': event.description,
+        'pricing': event.pricing,
+        'url': event.url,
+        'disabled': bool(event.full),
+        'api': {
+            'bookings_url': request.build_absolute_uri(
+                reverse(
+                    'api-event-bookings',
+                    kwargs={'agenda_identifier': agenda.slug, 'event_identifier': event.slug},
+                )
+            ),
+            'fillslot_url': request.build_absolute_uri(
+                reverse(
+                    'api-fillslot', kwargs={'agenda_identifier': agenda.slug, 'event_identifier': event.slug},
+                )
+            ),
+            'status_url': request.build_absolute_uri(
+                reverse(
+                    'api-event-status',
+                    kwargs={'agenda_identifier': agenda.slug, 'event_identifier': event.slug},
+                )
+            ),
+        },
+        'places': get_event_places(event),
+    }
+
+
 def get_resources_from_request(request, agenda):
     if agenda.kind != 'meetings' or 'resources' not in request.GET:
         return []
@@ -463,42 +497,7 @@ class Datetimes(APIView):
                 )
             )
 
-        response = {
-            'data': [
-                {
-                    'id': x.slug,
-                    'slug': x.slug,  # kept for compatibility
-                    'text': force_text(x),
-                    'datetime': format_response_datetime(x.start_datetime),
-                    'description': x.description,
-                    'pricing': x.pricing,
-                    'url': x.url,
-                    'disabled': bool(x.full),
-                    'api': {
-                        'bookings_url': request.build_absolute_uri(
-                            reverse(
-                                'api-event-bookings',
-                                kwargs={'agenda_identifier': agenda.slug, 'event_identifier': x.slug},
-                            )
-                        ),
-                        'fillslot_url': request.build_absolute_uri(
-                            reverse(
-                                'api-fillslot',
-                                kwargs={'agenda_identifier': agenda.slug, 'event_identifier': x.slug},
-                            )
-                        ),
-                        'status_url': request.build_absolute_uri(
-                            reverse(
-                                'api-event-status',
-                                kwargs={'agenda_identifier': agenda.slug, 'event_identifier': x.slug},
-                            )
-                        ),
-                    },
-                    'places': get_event_places(x),
-                }
-                for x in entries
-            ]
-        }
+        response = {'data': [get_event_detail(request, x, agenda=agenda) for x in entries]}
         return Response(response)
 
 
@@ -1438,8 +1437,8 @@ class SlotStatus(APIView):
         event = self.get_object(agenda_identifier, event_identifier)
         response = {
             'err': 0,
-            'places': get_event_places(event),
         }
+        response.update(get_event_detail(request, event))
         return Response(response)
 
 
diff --git a/tests/test_api.py b/tests/test_api.py
index b486cae..9534eb1 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1916,6 +1916,23 @@ def test_status(app, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 9
     assert resp.json['places']['reserved'] == 1
+    assert resp.json == {
+        'err': 0,
+        'id': 'event-slug',
+        'slug': 'event-slug',
+        'text': str(event),
+        'datetime': localtime(event.start_datetime).strftime('%Y-%m-%d %H:%M:%S'),
+        'description': None,
+        'pricing': None,
+        'url': None,
+        'disabled': False,
+        'api': {
+            'bookings_url': 'http://testserver/api/agenda/foo-bar/bookings/event-slug/',
+            'fillslot_url': 'http://testserver/api/agenda/foo-bar/fillslot/event-slug/',
+            'status_url': 'http://testserver/api/agenda/foo-bar/status/event-slug/',
+        },
+        'places': {'available': 9, 'reserved': 1, 'total': 10, 'full': False, 'has_waiting_list': False},
+    }
     assert 'waiting_list_total' not in resp.json['places']
 
     Booking(event=event, in_waiting_list=True).save()
-- 
2.28.0

