From 58da7de59218cd66f628a9f25b42b12239374b73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 28 Feb 2022 18:11:29 +0100
Subject: [PATCH] family: add button to booking form (#62244)

---
 combo/apps/family/forms.py                    |  1 +
 .../migrations/0012_booking_form_url.py       | 73 +++++++++++++++++++
 combo/apps/family/models.py                   | 28 +++++--
 .../templates/combo/family/weekly_agenda.html |  1 +
 tests/test_family.py                          | 34 +++++++++
 5 files changed, 131 insertions(+), 6 deletions(-)
 create mode 100644 combo/apps/family/migrations/0012_booking_form_url.py

diff --git a/combo/apps/family/forms.py b/combo/apps/family/forms.py
index d6c5d75b..bd865077 100644
--- a/combo/apps/family/forms.py
+++ b/combo/apps/family/forms.py
@@ -30,6 +30,7 @@ class WeeklyAgendaCellForm(forms.ModelForm):
             'start_date_filter',
             'end_date_filter',
             'user_external_template',
+            'booking_form_url',
         ]
 
     def save(self, *args, **kwargs):
diff --git a/combo/apps/family/migrations/0012_booking_form_url.py b/combo/apps/family/migrations/0012_booking_form_url.py
new file mode 100644
index 00000000..c8a52d85
--- /dev/null
+++ b/combo/apps/family/migrations/0012_booking_form_url.py
@@ -0,0 +1,73 @@
+from django.db import migrations, models
+
+import combo.data.models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('family', '0011_agenda_subscribed_and_dates'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='weeklyagendacell',
+            name='booking_form_url',
+            field=models.CharField(
+                blank=True,
+                max_length=2000,
+                validators=[combo.data.models.django_template_validator],
+                verbose_name='URL to the booking Form',
+            ),
+        ),
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='agenda_categories',
+            field=models.CharField(
+                blank=True,
+                max_length=2000,
+                validators=[combo.data.models.django_template_validator],
+                verbose_name='Agenda categories',
+            ),
+        ),
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='agenda_references_template',
+            field=models.CharField(
+                blank=True,
+                max_length=2000,
+                validators=[combo.data.models.django_template_validator],
+                verbose_name='Agenda references template',
+            ),
+        ),
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='end_date_filter',
+            field=models.CharField(
+                blank=True,
+                max_length=250,
+                validators=[combo.data.models.django_template_validator],
+                verbose_name='End date filter template',
+            ),
+        ),
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='start_date_filter',
+            field=models.CharField(
+                blank=True,
+                max_length=250,
+                validators=[combo.data.models.django_template_validator],
+                verbose_name='Start date filter template',
+            ),
+        ),
+        migrations.AlterField(
+            model_name='weeklyagendacell',
+            name='user_external_template',
+            field=models.CharField(
+                blank=True,
+                max_length=255,
+                validators=[combo.data.models.django_template_validator],
+                verbose_name='User external reference template',
+            ),
+        ),
+    ]
diff --git a/combo/apps/family/models.py b/combo/apps/family/models.py
index 87bf3072..adee2d75 100644
--- a/combo/apps/family/models.py
+++ b/combo/apps/family/models.py
@@ -19,7 +19,8 @@ from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
 from combo.data.library import register_cell_class
-from combo.data.models import JsonCellBase
+from combo.data.models import JsonCellBase, django_template_validator
+from combo.utils import get_templated_url
 
 
 @register_cell_class
@@ -35,13 +36,25 @@ class WeeklyAgendaCell(JsonCellBase):
         ],
     )
     agenda_references_template = models.CharField(
-        _('Agenda references template'), max_length=2000, blank=True
+        _('Agenda references template'), max_length=2000, blank=True, validators=[django_template_validator]
+    )
+    agenda_categories = models.CharField(
+        _('Agenda categories'), max_length=2000, blank=True, validators=[django_template_validator]
+    )
+    start_date_filter = models.CharField(
+        _('Start date filter template'), max_length=250, blank=True, validators=[django_template_validator]
+    )
+    end_date_filter = models.CharField(
+        _('End date filter template'), max_length=250, blank=True, validators=[django_template_validator]
     )
-    agenda_categories = models.CharField(_('Agenda categories'), max_length=2000, blank=True)
-    start_date_filter = models.CharField(_('Start date filter template'), max_length=250, blank=True)
-    end_date_filter = models.CharField(_('End date filter template'), max_length=250, blank=True)
     user_external_template = models.CharField(
-        _('User external reference template'), max_length=255, blank=True
+        _('User external reference template'),
+        max_length=255,
+        blank=True,
+        validators=[django_template_validator],
+    )
+    booking_form_url = models.CharField(
+        _('URL to the booking Form'), max_length=2000, blank=True, validators=[django_template_validator]
     )
 
     default_template_name = 'combo/family/weekly_agenda.html'
@@ -90,6 +103,9 @@ class WeeklyAgendaCell(JsonCellBase):
     def get_cell_extra_context(self, context):
         if context.get('placeholder_search_mode'):
             return {}
+        if self.booking_form_url:
+            context['booking_form_url'] = get_templated_url(self.booking_form_url, context=context)
+
         return super().get_cell_extra_context(context)
 
     def get_default_form_class(self):
diff --git a/combo/apps/family/templates/combo/family/weekly_agenda.html b/combo/apps/family/templates/combo/family/weekly_agenda.html
index abf90862..851e1f26 100644
--- a/combo/apps/family/templates/combo/family/weekly_agenda.html
+++ b/combo/apps/family/templates/combo/family/weekly_agenda.html
@@ -37,6 +37,7 @@
   {% endwith %}
 {% if day.weekday == 6 %}
 </ul>
+{% if booking_form_url %}<p class="booking-btn"><a class="pk-button" href="{{ booking_form_url }}{% if '?' in booking_form_url %}&{% else %}?{% endif %}current={{ day|adjust_to_week_monday|date:"Y-m-d" }}">{% trans "Update bookings" %}</a></p>{% endif %}
 <p class="no-activity">{% trans "No activity this week" %}</p>
 </li>
 {% endif %}
diff --git a/tests/test_family.py b/tests/test_family.py
index 356203a4..84bcb082 100644
--- a/tests/test_family.py
+++ b/tests/test_family.py
@@ -264,3 +264,37 @@ def test_weeklyagenda_cell_filter_dates(context, agenda_type, agenda_param):
             'http://chrono.example.org/api/agendas/datetimes/'
             '?%s&date_start=&date_end=xyz&user_external_id=&show_past_events=true' % agenda_param
         )
+
+
+def test_weeklyagenda_cell_booking_form_url(context):
+    page = Page.objects.create(title='Family', slug='index', template_name='standard')
+    cell = WeeklyAgendaCell.objects.create(page=page, placeholder='content', order=0)
+    data = {
+        'data': [
+            {'text': 'Foo', 'date': '2022-02-28'},
+        ]
+    }
+
+    context['synchronous'] = True  # to get fresh content
+    context['request'].user = MockUserWithNameId()
+
+    with mock.patch('combo.utils.requests.get') as requests_get:
+        requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
+        result = cell.render(context)
+        assert 'booking-btn' not in result
+
+    cell.booking_form_url = 'http://example.com/foobar/'
+    cell.save()
+    with mock.patch('combo.utils.requests.get') as requests_get:
+        requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
+        result = cell.render(context)
+        assert 'booking-btn' in result
+        assert 'http://example.com/foobar/?current=2022-02-28' in result
+
+    cell.booking_form_url = 'http://example.com/foobar/?user={{ user_nameid }}'
+    cell.save()
+    with mock.patch('combo.utils.requests.get') as requests_get:
+        requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
+        result = cell.render(context)
+        assert 'booking-btn' in result
+        assert 'http://example.com/foobar/?user=xyz&current=2022-02-28' in result
-- 
2.34.1

