From b396dd980062ab88c09b6bebca1d51ba8b17da92 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 8 Oct 2021 09:47:52 +0200
Subject: [PATCH 3/5] api: display category on agenda details (#57670)

---
 chrono/api/views.py   |  3 ++-
 tests/api/test_all.py | 13 ++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/chrono/api/views.py b/chrono/api/views.py
index e33a54a..2f58bd2 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -350,16 +350,17 @@ def get_agenda_detail(request, agenda, check_events=False):
         'id': agenda.slug,
         'slug': agenda.slug,  # kept for compatibility
         'text': agenda.label,
         'kind': agenda.kind,
         'minimal_booking_delay': agenda.minimal_booking_delay,
         'maximal_booking_delay': agenda.maximal_booking_delay,
         'edit_role': agenda.edit_role.name if agenda.edit_role else None,
         'view_role': agenda.view_role.name if agenda.view_role else None,
+        'category': agenda.category.slug if agenda.category else None,
     }
 
     if agenda.kind == 'meetings':
         agenda_detail['resources'] = [
             {'id': r.slug, 'text': r.label, 'description': r.description} for r in agenda.resources.all()
         ]
     if agenda.kind == 'events':
         agenda_detail['minimal_booking_delay_in_working_days'] = agenda.minimal_booking_delay_in_working_days
@@ -701,17 +702,17 @@ class Agendas(APIView):
         if self.request.method == 'GET':
             return []
         return [permissions.IsAuthenticated()]
 
     def get(self, request, format=None):
         agendas_queryset = (
             Agenda.objects.all()
             .select_related('absence_reasons_group')
-            .prefetch_related('resources', 'absence_reasons_group__absence_reasons')
+            .prefetch_related('resources', 'absence_reasons_group__absence_reasons', 'category')
             .order_by('label')
         )
 
         if 'q' in request.GET:
             if not request.GET['q']:
                 return Response({'data': []})
             agendas_queryset = agendas_queryset.filter(slug__icontains=request.GET['q'])
 
diff --git a/tests/api/test_all.py b/tests/api/test_all.py
index 11684b0..6cd4430 100644
--- a/tests/api/test_all.py
+++ b/tests/api/test_all.py
@@ -63,16 +63,17 @@ def test_agendas_api(app):
                 'id': 'foo-bar',
                 'slug': 'foo-bar',
                 'kind': 'events',
                 'minimal_booking_delay': 1,
                 'minimal_booking_delay_in_working_days': False,
                 'maximal_booking_delay': 56,
                 'edit_role': 'Edit',
                 'view_role': None,
+                'category': 'category-a',
                 'absence_reasons': [
                     {'id': reason.slug, 'slug': reason.slug, 'text': reason.label, 'label': reason.label},
                     {'id': reason2.slug, 'slug': reason2.slug, 'text': reason2.label, 'label': reason2.label},
                 ],
                 'api': {
                     'datetimes_url': 'http://testserver/api/agenda/foo-bar/datetimes/',
                     'fillslots_url': 'http://testserver/api/agenda/foo-bar/fillslots/',
                 },
@@ -82,31 +83,33 @@ def test_agendas_api(app):
                 'id': 'foo-bar-2',
                 'kind': 'events',
                 'slug': 'foo-bar-2',
                 'minimal_booking_delay': 1,
                 'minimal_booking_delay_in_working_days': False,
                 'maximal_booking_delay': 56,
                 'edit_role': None,
                 'view_role': None,
+                'category': 'category-a',
                 'api': {
                     'datetimes_url': 'http://testserver/api/agenda/foo-bar-2/datetimes/',
                     'fillslots_url': 'http://testserver/api/agenda/foo-bar-2/fillslots/',
                 },
             },
             {
                 'text': 'Foo bar 3',
                 'id': 'foo-bar-3',
                 'kind': 'events',
                 'slug': 'foo-bar-3',
                 'minimal_booking_delay': 1,
                 'minimal_booking_delay_in_working_days': False,
                 'maximal_booking_delay': 56,
                 'edit_role': None,
                 'view_role': None,
+                'category': None,
                 'absence_reasons': [
                     {'id': reason.slug, 'slug': reason.slug, 'text': reason.label, 'label': reason.label},
                     {'id': reason2.slug, 'slug': reason2.slug, 'text': reason2.label, 'label': reason2.label},
                 ],
                 'api': {
                     'datetimes_url': 'http://testserver/api/agenda/foo-bar-3/datetimes/',
                     'fillslots_url': 'http://testserver/api/agenda/foo-bar-3/fillslots/',
                 },
@@ -114,16 +117,17 @@ def test_agendas_api(app):
             {
                 'text': 'Foo bar Meeting',
                 'id': 'foo-bar-meeting',
                 'slug': 'foo-bar-meeting',
                 'minimal_booking_delay': 1,
                 'maximal_booking_delay': 56,
                 'edit_role': None,
                 'view_role': 'View',
+                'category': 'category-b',
                 'kind': 'meetings',
                 'resources': [
                     {'id': 'resource-1', 'text': 'Resource 1', 'description': 'Foo bar Resource 1'},
                     {'id': 'resource-2', 'text': 'Resource 2', 'description': 'Foo bar Resource 2'},
                 ],
                 'api': {
                     'meetings_url': 'http://testserver/api/agenda/foo-bar-meeting/meetings/',
                     'desks_url': 'http://testserver/api/agenda/foo-bar-meeting/desks/',
@@ -134,16 +138,17 @@ def test_agendas_api(app):
             {
                 'text': 'Foo bar Meeting 2',
                 'id': 'foo-bar-meeting-2',
                 'slug': 'foo-bar-meeting-2',
                 'minimal_booking_delay': 1,
                 'maximal_booking_delay': 56,
                 'edit_role': None,
                 'view_role': None,
+                'category': None,
                 'kind': 'meetings',
                 'resources': [],
                 'api': {
                     'meetings_url': 'http://testserver/api/agenda/foo-bar-meeting-2/meetings/',
                     'desks_url': 'http://testserver/api/agenda/foo-bar-meeting-2/desks/',
                     'resources_url': 'http://testserver/api/agenda/foo-bar-meeting-2/resources/',
                     'fillslots_url': 'http://testserver/api/agenda/foo-bar-meeting-2/fillslots/',
                 },
@@ -151,16 +156,17 @@ def test_agendas_api(app):
             {
                 'text': 'Virtual Agenda',
                 'id': 'virtual-agenda',
                 'slug': 'virtual-agenda',
                 'minimal_booking_delay': 1,
                 'maximal_booking_delay': 56,
                 'edit_role': 'Edit',
                 'view_role': 'View',
+                'category': None,
                 'kind': 'virtual',
                 'api': {
                     'meetings_url': 'http://testserver/api/agenda/virtual-agenda/meetings/',
                     'desks_url': 'http://testserver/api/agenda/virtual-agenda/desks/',
                     'fillslots_url': 'http://testserver/api/agenda/virtual-agenda/fillslots/',
                 },
             },
         ],
@@ -170,20 +176,20 @@ def test_agendas_api(app):
     assert len(resp.json['data']) == 5
     resp = app.get('/api/agenda/', params={'q': 'MEET'})
     assert len(resp.json['data']) == 2
     resp = app.get('/api/agenda/', params={'q': ''})
     assert len(resp.json['data']) == 0
 
     with CaptureQueriesContext(connection) as ctx:
         resp = app.get('/api/agenda/')
-        assert len(ctx.captured_queries) == 8
+        assert len(ctx.captured_queries) == 9
     with CaptureQueriesContext(connection) as ctx:
         resp = app.get('/api/agenda/', params={'q': 'MEET'})
-        assert len(ctx.captured_queries) == 3
+        assert len(ctx.captured_queries) == 4
 
     resp = app.get('/api/agenda/', params={'with_open_events': '1'})
     assert len(resp.json['data']) == 0
 
     resp = app.get('/api/agenda/', params={'category': ''})
     assert len(resp.json['data']) == 6
     resp = app.get('/api/agenda/', params={'category': '__none__'})
     assert len(resp.json['data']) == 3
@@ -280,17 +286,17 @@ def test_agendas_api(app):
         TimePeriodException.objects.create(
             desk=event_agenda.desk_set.get(),
             start_datetime=now(),
             end_datetime=now() + datetime.timedelta(hours=1),
         )
 
     with CaptureQueriesContext(connection) as ctx:
         resp = app.get('/api/agenda/', params={'with_open_events': '1'})
-        assert len(ctx.captured_queries) == 9
+        assert len(ctx.captured_queries) == 10
 
 
 def test_agendas_meetingtypes_api(app, some_data, meetings_agenda):
     resp = app.get('/api/agenda/%s/meetings/' % meetings_agenda.slug)
     expected_resp = {
         'data': [
             {
                 'text': 'Blah',
@@ -500,16 +506,17 @@ def test_virtual_agenda_detail(app, virtual_meetings_agenda):
         'data': {
             'text': 'Virtual Agenda',
             'id': 'virtual-agenda',
             'slug': 'virtual-agenda',
             'minimal_booking_delay': None,
             'maximal_booking_delay': None,
             'edit_role': None,
             'view_role': None,
+            'category': None,
             'kind': 'virtual',
             'api': {
                 'meetings_url': 'http://testserver/api/agenda/%s/meetings/' % virtual_meetings_agenda.slug,
                 'desks_url': 'http://testserver/api/agenda/%s/desks/' % virtual_meetings_agenda.slug,
                 'fillslots_url': 'http://testserver/api/agenda/%s/fillslots/' % virtual_meetings_agenda.slug,
             },
         },
     }
-- 
2.30.2

