From 114251ccec1582009d3a234149a042125f9eb147 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 16 Feb 2021 14:15:41 +0100
Subject: [PATCH 2/5] backoffice: display agenda datasources (#48282)

---
 tests/admin_pages/test_datasource.py          | 48 +++++++++++++++++++
 wcs/admin/data_sources.py                     | 13 ++++-
 wcs/data_sources.py                           | 30 ++++++++----
 .../wcs/backoffice/data-sources.html          | 17 +++++++
 4 files changed, 98 insertions(+), 10 deletions(-)

diff --git a/tests/admin_pages/test_datasource.py b/tests/admin_pages/test_datasource.py
index a67f3260..c9535c65 100644
--- a/tests/admin_pages/test_datasource.py
+++ b/tests/admin_pages/test_datasource.py
@@ -98,6 +98,54 @@ def test_data_sources_from_carddefs(pub):
     )
 
 
+def test_data_sources_agenda_without_chrono(pub):
+    create_superuser(pub)
+    NamedDataSource.wipe()
+
+    data_source = NamedDataSource(name='foobar')
+    data_source.data_source = {'type': 'json', 'value': 'http://some.url'}
+    data_source.external = 'agenda'
+    data_source.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/settings/data-sources/')
+    assert 'Agendas' not in resp.text
+    assert 'There are no data sources from agendas.' not in resp.text
+
+
+def test_data_sources_agenda(pub, chrono_url):
+    create_superuser(pub)
+    NamedDataSource.wipe()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/settings/data-sources/')
+    assert 'Agendas' in resp.text
+    assert 'There are no agendas.' in resp.text
+
+    data_source = NamedDataSource(name='foobar')
+    data_source.data_source = {'type': 'json', 'value': 'http://some.url'}
+    data_source.store()
+    resp = app.get('/backoffice/settings/data-sources/')
+    assert 'Agendas' in resp.text
+    assert 'There are no agendas.' in resp.text
+
+    data_source.external = 'agenda'
+    data_source.store()
+    resp = app.get('/backoffice/settings/data-sources/')
+    assert 'Agendas' in resp.text
+    assert 'There are no agendas.' not in resp.text
+    assert '<li><a href="%s/">foobar (foobar)</a></li>' % data_source.id in resp.text
+
+    data_source.external_status = 'not-found'
+    data_source.store()
+    resp = app.get('/backoffice/settings/data-sources/')
+    assert (
+        '<li><a href="%s/">foobar (foobar) - <span class="extra-info">not found</span></a></li>'
+        % data_source.id
+        in resp.text
+    )
+
+
 def test_data_sources_new(pub):
     create_superuser(pub)
     NamedDataSource.wipe()
diff --git a/wcs/admin/data_sources.py b/wcs/admin/data_sources.py
index b806f163..a2e6eb23 100644
--- a/wcs/admin/data_sources.py
+++ b/wcs/admin/data_sources.py
@@ -32,7 +32,7 @@ from wcs.qommon.form import get_session
 from wcs.qommon import misc
 from wcs.qommon.backoffice.menu import html_top
 from wcs.carddef import CardDef
-from wcs.data_sources import NamedDataSource, DataSourceSelectionWidget, get_structured_items
+from wcs.data_sources import NamedDataSource, DataSourceSelectionWidget, get_structured_items, has_chrono
 from wcs.formdef import FormDef, get_formdefs_of_all_kinds
 from wcs.backoffice.snapshots import SnapshotsDirectory
 
@@ -369,12 +369,21 @@ class NamedDataSourcesDirectory(Directory):
 
     def _q_index(self):
         html_top('datasources', title=_('Data Sources'))
+        data_sources = []
+        agenda_data_sources = []
+        for ds in NamedDataSource.select(order_by='name'):
+            if ds.external == 'agenda':
+                agenda_data_sources.append(ds)
+            else:
+                data_sources.append(ds)
         generated_data_sources = list(CardDef.get_carddefs_as_data_source())
         generated_data_sources.sort(key=lambda x: misc.simplify(x[1]))
         return template.QommonTemplateResponse(
             templates=['wcs/backoffice/data-sources.html'],
             context={
-                'data_sources': NamedDataSource.select(order_by='name'),
+                'data_sources': data_sources,
+                'has_chrono': has_chrono(get_publisher()),
+                'agenda_data_sources': agenda_data_sources,
                 'generated_data_sources': generated_data_sources,
             },
         )
diff --git a/wcs/data_sources.py b/wcs/data_sources.py
index e012e4f4..95978635 100644
--- a/wcs/data_sources.py
+++ b/wcs/data_sources.py
@@ -72,15 +72,29 @@ class DataSourceSelectionWidget(CompositeWidget):
                 options.append(OptGroup(_('Cards')))
                 options.extend(cards_options)
 
-            nds_options = [
-                (
-                    x.slug,
-                    x.name,
-                    x.slug,
-                    {'data-type': x.type, 'data-maybe-datetimes': 'true' if x.maybe_datetimes() else 'false'},
+            nds_options = []
+            nds_agenda_options = []
+
+            for ds in NamedDataSource.select():
+                option = (
+                    ds.slug,
+                    ds.name,
+                    ds.slug,
+                    {
+                        'data-type': ds.type,
+                        'data-maybe-datetimes': 'true' if ds.maybe_datetimes() else 'false',
+                    },
                 )
-                for x in NamedDataSource.select()
-            ]
+                if ds.external == 'agenda':
+                    nds_agenda_options.append(option)
+                else:
+                    nds_options.append(option)
+
+            nds_agenda_options.sort(key=lambda x: misc.simplify(x[1]))
+            if nds_agenda_options:
+                options.append(OptGroup(_('Agendas')))
+                options.extend(nds_agenda_options)
+
             nds_options.sort(key=lambda x: misc.simplify(x[1]))
             if nds_options:
                 options.append(OptGroup(_('Manually Configured Data Sources')))
diff --git a/wcs/templates/wcs/backoffice/data-sources.html b/wcs/templates/wcs/backoffice/data-sources.html
index 5c8d22aa..deb75c35 100644
--- a/wcs/templates/wcs/backoffice/data-sources.html
+++ b/wcs/templates/wcs/backoffice/data-sources.html
@@ -24,6 +24,23 @@
 {% endif %}
 </div>
 
+{% if has_chrono %}
+<div class="section">
+<h2>{% trans "Agendas" %}</h2>
+{% if agenda_data_sources %}
+<ul class="objects-list single-links">
+  {% for data_source in agenda_data_sources %}
+  <li><a href="{{ data_source.id }}/">{{ data_source.name }} ({{ data_source.slug }}){% if data_source.external_status == 'not-found' %} - <span class="extra-info">{% trans "not found" %}</span>{% endif %}</a></li>
+  {% endfor %}
+</ul>
+{% else %}
+<div>
+{% trans "There are no agendas." %}
+</div>
+{% endif %}
+</div>
+{% endif %}
+
 <div class="section">
 <h2>{% trans "Manually Configured Data Sources" %}</h2>
 {% if data_sources %}
-- 
2.29.2

