From 6b4e4eba8cb9eb0156d2b6f6aad7733450797063 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 21 Oct 2021 14:38:31 +0200
Subject: [PATCH] family: agenda_references_template field (#57927)

---
 .../0008_agenda_references_template.py        | 23 +++++++++++++++++++
 .../0009_agenda_references_template.py        | 16 +++++++++++++
 combo/apps/family/models.py                   | 10 +++++---
 tests/test_family.py                          | 22 +++++++++++++++---
 4 files changed, 65 insertions(+), 6 deletions(-)
 create mode 100644 combo/apps/family/migrations/0008_agenda_references_template.py
 create mode 100644 combo/apps/family/migrations/0009_agenda_references_template.py

diff --git a/combo/apps/family/migrations/0008_agenda_references_template.py b/combo/apps/family/migrations/0008_agenda_references_template.py
new file mode 100644
index 00000000..f263b552
--- /dev/null
+++ b/combo/apps/family/migrations/0008_agenda_references_template.py
@@ -0,0 +1,23 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('family', '0007_weekly_agenda_user_template'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='weeklyagendacell',
+            old_name='agenda_reference',
+            new_name='agenda_references_template',
+        ),
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='user_external_template',
+            field=models.CharField(
+                blank=True, max_length=255, verbose_name='User external reference template'
+            ),
+        ),
+    ]
diff --git a/combo/apps/family/migrations/0009_agenda_references_template.py b/combo/apps/family/migrations/0009_agenda_references_template.py
new file mode 100644
index 00000000..1c019717
--- /dev/null
+++ b/combo/apps/family/migrations/0009_agenda_references_template.py
@@ -0,0 +1,16 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('family', '0008_agenda_references_template'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='agenda_references_template',
+            field=models.CharField(blank=True, max_length=2000, verbose_name='Agenda references template'),
+        ),
+    ]
diff --git a/combo/apps/family/models.py b/combo/apps/family/models.py
index 12a75a30..1998fc17 100644
--- a/combo/apps/family/models.py
+++ b/combo/apps/family/models.py
@@ -57,8 +57,12 @@ class FamilyInfosCell(CellBase):
 @register_cell_class
 class WeeklyAgendaCell(JsonCellBase):
     title = models.CharField(_('Title'), max_length=150, blank=True)
-    agenda_reference = models.CharField(_('Agenda'), max_length=128)
-    user_external_template = models.CharField(max_length=255)
+    agenda_references_template = models.CharField(
+        _('Agenda references template'), max_length=2000, blank=True
+    )
+    user_external_template = models.CharField(
+        _('User external reference template'), max_length=255, blank=True
+    )
 
     default_template_name = 'family/weekly_agenda.html'
     force_async = True
@@ -82,7 +86,7 @@ class WeeklyAgendaCell(JsonCellBase):
             chrono_url += '/'
         return '%sapi/agendas/datetimes/?agendas=%s&user_external_id=%s&show_past_events=true' % (
             chrono_url,
-            self.agenda_reference,
+            self.agenda_references_template,
             self.user_external_template,
         )
 
diff --git a/tests/test_family.py b/tests/test_family.py
index 681c29c3..b9518a57 100644
--- a/tests/test_family.py
+++ b/tests/test_family.py
@@ -6,6 +6,7 @@ from django.core.cache import cache
 from django.test.client import RequestFactory
 
 from combo.apps.family.models import WeeklyAgendaCell
+from combo.apps.wcs.context_processors import Cards
 from combo.data.models import Page
 from combo.utils import NothingInCacheException
 
@@ -14,7 +15,7 @@ pytestmark = pytest.mark.django_db
 
 @pytest.fixture
 def context():
-    ctx = {'request': RequestFactory().get('/')}
+    ctx = {'cards': Cards(), 'request': RequestFactory().get('/')}
     ctx['request'].user = None
     ctx['request'].session = {}
     return ctx
@@ -70,7 +71,7 @@ def test_weeklyagenda_cell(settings, context):
             == 'http://chrono.example.org/api/agendas/datetimes/?agendas=&user_external_id=&show_past_events=true'
         )
 
-    cell.agenda_reference = 'some-agenda'
+    cell.agenda_references_template = 'some-agenda,other-agenda'
     cell.save()
     context['request'].user = MockUserWithNameId()
     with mock.patch('combo.utils.requests.get') as requests_get:
@@ -78,9 +79,24 @@ def test_weeklyagenda_cell(settings, context):
         cell.render(context)
         assert (
             requests_get.call_args_list[0][0][0]
-            == 'http://chrono.example.org/api/agendas/datetimes/?agendas=some-agenda&user_external_id=&show_past_events=true'
+            == 'http://chrono.example.org/api/agendas/datetimes/?agendas=some-agenda,other-agenda&user_external_id=&show_past_events=true'
         )
 
+    cell.agenda_references_template = (
+        '{% load wcs %}{{ cards|objects:"foo"|get_full|first|get:"fields"|get:"bar"|default:"" }}'
+        ',some-agenda,other-agenda,{{ user_nameid }}'
+    )
+    cell.save()
+    context['request'].user = MockUserWithNameId()
+    with mock.patch('combo.utils.requests.get') as requests_get:
+        requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
+        cell.render(context)
+        assert (
+            requests_get.call_args_list[1][0][0]
+            == 'http://chrono.example.org/api/agendas/datetimes/?agendas=,some-agenda,other-agenda,xyz&user_external_id=&show_past_events=true'
+        )
+
+    cell.agenda_references_template = 'some-agenda'
     cell.user_external_template = 'some-key:{{ user_nameid }}'
     cell.save()
     with mock.patch('combo.utils.requests.get') as requests_get:
-- 
2.33.0

