From 2c642cc103518fcbe6dfb6093c4bfae549f8fc3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 13 Jan 2022 16:27:08 +0100
Subject: [PATCH] datasource: all agendas & agendas of a category (#58884)

---
 tests/test_datasource_chrono.py | 36 ++++++++++++++++++++++++++++++++-
 wcs/data_sources.py             | 21 +++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/tests/test_datasource_chrono.py b/tests/test_datasource_chrono.py
index b29551b9..a3419add 100644
--- a/tests/test_datasource_chrono.py
+++ b/tests/test_datasource_chrono.py
@@ -54,6 +54,7 @@ AGENDA_EVENTS_DATA = [
         "id": "events-B",
         "kind": "events",
         "text": "Events B",
+        "category": "category-b",
     },
 ]
 
@@ -64,6 +65,7 @@ AGENDA_MEETINGS_DATA = [
         "id": "meetings-A",
         "kind": "meetings",
         "text": "Meetings A",
+        "category": "category-a",
     },
     {
         "api": {
@@ -72,6 +74,7 @@ AGENDA_MEETINGS_DATA = [
         "id": "virtual-B",
         "kind": "virtual",
         "text": "Virtual B",
+        "category": "category-b",
     },
 ]
 
@@ -111,7 +114,13 @@ def test_collect_agenda_data(urlopen, pub, chrono_url):
     NamedDataSource.wipe()
 
     urlopen.side_effect = lambda *args: io.StringIO('{"data": []}')
-    assert collect_agenda_data(pub) == []
+    assert collect_agenda_data(pub) == [
+        {
+            'slug': 'agenda-all',
+            'text': 'All agendas',
+            'url': 'http://chrono.example.net/api/agenda/',
+        },
+    ]
     assert urlopen.call_args_list == [mock.call('http://chrono.example.net/api/agenda/')]
 
     urlopen.side_effect = ConnectionError
@@ -123,6 +132,16 @@ def test_collect_agenda_data(urlopen, pub, chrono_url):
     urlopen.side_effect = lambda *args: io.StringIO(json.dumps({"data": AGENDA_EVENTS_DATA}))
     urlopen.reset_mock()
     assert collect_agenda_data(pub) == [
+        {
+            'slug': 'agenda-all',
+            'text': 'All agendas',
+            'url': 'http://chrono.example.net/api/agenda/',
+        },
+        {
+            'slug': 'agenda-category-category-b',
+            'text': 'All agendas of category category-b',
+            'url': 'http://chrono.example.net/api/agenda/?category=category-b',
+        },
         {
             'slug': 'agenda-events-events-A',
             'text': 'Events A',
@@ -144,6 +163,21 @@ def test_collect_agenda_data(urlopen, pub, chrono_url):
     ]
     urlopen.reset_mock()
     assert collect_agenda_data(pub) == [
+        {
+            'slug': 'agenda-all',
+            'text': 'All agendas',
+            'url': 'http://chrono.example.net/api/agenda/',
+        },
+        {
+            'slug': 'agenda-category-category-a',
+            'text': 'All agendas of category category-a',
+            'url': 'http://chrono.example.net/api/agenda/?category=category-a',
+        },
+        {
+            'slug': 'agenda-category-category-b',
+            'text': 'All agendas of category category-b',
+            'url': 'http://chrono.example.net/api/agenda/?category=category-b',
+        },
         {
             'slug': 'agenda-meetings-meetings-A-meetingtypes',
             'text': 'Meetings A - Meeting types',
diff --git a/wcs/data_sources.py b/wcs/data_sources.py
index 361e4147..3c5290e1 100644
--- a/wcs/data_sources.py
+++ b/wcs/data_sources.py
@@ -887,7 +887,10 @@ def collect_agenda_data(publisher):
 
     # build datasources from chrono
     agenda_data = []
+    categories = set()
     for agenda in result.get('data') or []:
+        if agenda.get('category'):
+            categories.add(agenda['category'])
         if agenda['kind'] == 'events':
             agenda_data.append(
                 {
@@ -917,6 +920,24 @@ def collect_agenda_data(publisher):
                         'url': meetingtype['api']['datetimes_url'],
                     }
                 )
+
+    agenda_data = [
+        {
+            'slug': 'agenda-category-%s' % category,
+            'text': _('All agendas of category %s' % category),
+            'url': '%s?category=%s' % (agenda_url, category),
+        }
+        for category in sorted(categories)
+    ] + agenda_data
+
+    agenda_data = [
+        {
+            'slug': 'agenda-all',
+            'text': _('All agendas'),
+            'url': agenda_url,
+        }
+    ] + agenda_data
+
     return agenda_data
 
 
-- 
2.34.1

