From deced2942627d42be98580850673d1668488d35c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 20 Apr 2021 10:52:54 +0200
Subject: [PATCH 1/2] manager: configure filters for check page (#53238)

---
 .../migrations/0087_booking_check_filters.py  | 21 +++++++++++++++++++
 chrono/agendas/models.py                      | 11 ++++++++++
 chrono/manager/forms.py                       |  2 +-
 .../manager_events_agenda_settings.html       | 13 ++++++++++++
 tests/manager/test_all.py                     | 15 ++++++++++++-
 5 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100644 chrono/agendas/migrations/0087_booking_check_filters.py

diff --git a/chrono/agendas/migrations/0087_booking_check_filters.py b/chrono/agendas/migrations/0087_booking_check_filters.py
new file mode 100644
index 0000000..c115e4d
--- /dev/null
+++ b/chrono/agendas/migrations/0087_booking_check_filters.py
@@ -0,0 +1,21 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0086_booking_user_block_template'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='agenda',
+            name='booking_check_filters',
+            field=models.CharField(
+                blank=True,
+                help_text='Comma separated list of keys defined in extra_data.',
+                max_length=250,
+                verbose_name='Filters',
+            ),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 77dfd5e..16984e1 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -204,6 +204,12 @@ class Agenda(models.Model):
         _('Booking form URL'), max_length=200, blank=True, validators=[django_template_validator]
     )
     desk_simple_management = models.BooleanField(default=False)
+    booking_check_filters = models.CharField(
+        _('Filters'),
+        max_length=250,
+        blank=True,
+        help_text=_('Comma separated list of keys defined in extra_data.'),
+    )
     booking_user_block_template = models.TextField(
         _('User block template'),
         help_text=_('Displayed for each booking in event check page'),
@@ -720,6 +726,11 @@ class Agenda(models.Model):
         except (VariableDoesNotExist, TemplateSyntaxError):
             return
 
+    def get_booking_check_filters(self):
+        if not self.booking_check_filters:
+            return []
+        return [x.strip() for x in self.booking_check_filters.split(',')]
+
     def get_booking_user_block_template(self):
         return (
             self.booking_user_block_template
diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py
index 0af7e9b..641c9d8 100644
--- a/chrono/manager/forms.py
+++ b/chrono/manager/forms.py
@@ -747,7 +747,7 @@ class EventCancelForm(forms.ModelForm):
 class AgendaBookingCheckSettingsForm(forms.ModelForm):
     class Meta:
         model = Agenda
-        fields = ['absence_reasons_group', 'booking_user_block_template']
+        fields = ['absence_reasons_group', 'booking_check_filters', 'booking_user_block_template']
         widgets = {'booking_user_block_template': forms.Textarea(attrs={'rows': 3})}
 
     def __init__(self, *args, **kwargs):
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_settings.html b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
index 0831484..efbb164 100644
--- a/chrono/manager/templates/chrono/manager_events_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
@@ -48,6 +48,19 @@
         {% endif %}
     {% endif %}
 
+    {% with agenda.get_booking_check_filters as check_filters %}
+        {% if check_filters %}
+        <p>{% trans "Filters:" %}</p>
+        <ul>
+            {% for key in check_filters %}
+            <li>{{ key }}</li>
+            {% endfor %}
+        </ul>
+        {% else %}
+        <p>{% trans "No filters configured for this agenda." %}</p>
+        {% endif %}
+    {% endwith %}
+
     <p>{% trans "User block template" %}:</p>
     <pre>{{ agenda.get_booking_user_block_template }}</pre>
 </div>
diff --git a/tests/manager/test_all.py b/tests/manager/test_all.py
index 85184d2..c7124ff 100644
--- a/tests/manager/test_all.py
+++ b/tests/manager/test_all.py
@@ -519,8 +519,10 @@ def test_options_virtual_agenda_can_unset_delays(app, admin_user):
     assert agenda.maximal_booking_delay is None
 
 
-def test_options_agenda_booking_user_block_template(app, admin_user):
+def test_options_agenda_booking_check_options(app, admin_user):
     agenda = Agenda.objects.create(label='Foo bar', kind='events')
+
+    # check user template
     assert agenda.booking_user_block_template == ''
     assert (
         agenda.get_booking_user_block_template()
@@ -546,6 +548,17 @@ def test_options_agenda_booking_user_block_template(app, admin_user):
         == '{{ booking.user_name|default:booking.label|default:"Unknown" }}'
     )
 
+    # check filters
+    assert agenda.booking_check_filters == ''
+    assert agenda.get_booking_check_filters() == []
+
+    resp = app.get(url)
+    resp.form['booking_check_filters'] = 'foo,bar,baz'
+    resp = resp.form.submit()
+    agenda.refresh_from_db()
+    assert agenda.booking_check_filters == 'foo,bar,baz'
+    assert agenda.get_booking_check_filters() == ['foo', 'bar', 'baz']
+
     # check kind
     agenda.kind = 'meetings'
     agenda.save()
-- 
2.30.2

