From 18c18b66a6d980099cb0e0ed153bed0da95cdc2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 19 Apr 2021 15:47:38 +0200
Subject: [PATCH] manager: configuer user block on check page (#53237)

---
 .../0079_booking_user_block_template.py       | 23 ++++++++++++
 chrono/agendas/models.py                      | 24 +++++++++++++
 .../templates/chrono/manager_event_check.html | 16 +++++----
 .../manager_event_check_booking_fragment.html |  2 +-
 .../manager_events_agenda_settings.html       |  9 +++++
 chrono/manager/urls.py                        |  5 +++
 chrono/manager/views.py                       | 15 ++++++++
 tests/manager/test_all.py                     | 36 +++++++++++++++++++
 tests/manager/test_event.py                   |  6 ++++
 9 files changed, 128 insertions(+), 8 deletions(-)
 create mode 100644 chrono/agendas/migrations/0079_booking_user_block_template.py

diff --git a/chrono/agendas/migrations/0079_booking_user_block_template.py b/chrono/agendas/migrations/0079_booking_user_block_template.py
new file mode 100644
index 0000000..a0f27b4
--- /dev/null
+++ b/chrono/agendas/migrations/0079_booking_user_block_template.py
@@ -0,0 +1,23 @@
+from django.db import migrations, models
+
+import chrono.agendas.models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0078_absence_reasons'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='agenda',
+            name='booking_user_block_template',
+            field=models.TextField(
+                blank=True,
+                help_text='Displayed for each booking in event check page',
+                verbose_name='User block template',
+                validators=[chrono.agendas.models.django_template_validator],
+            ),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 03b05f7..c843590 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_user_block_template = models.TextField(
+        _('User block template'),
+        help_text=_('Displayed for each booking in event check page'),
+        blank=True,
+        validators=[django_template_validator],
+    )
 
     class Meta:
         ordering = ['label']
@@ -664,6 +670,12 @@ class Agenda(models.Model):
         except (VariableDoesNotExist, TemplateSyntaxError):
             return
 
+    def get_booking_user_block_template(self):
+        return (
+            self.booking_user_block_template
+            or '{{ booking.user_name|default:booking.label|default:"%s" }}' % _('Unknown')
+        )
+
     def prefetch_desks_and_exceptions(self, with_sources=False):
         if self.kind == 'meetings':
             desks = self.desk_set.all()
@@ -1548,6 +1560,18 @@ class Booking(models.Model):
             self.secondary_booking_set.update(in_waiting_list=True)
             self.save()
 
+    def get_user_block(self):
+        template_vars = Context(settings.TEMPLATE_VARS)
+        template_vars.update(
+            {
+                'booking': self,
+            }
+        )
+        try:
+            return Template(self.event.agenda.get_booking_user_block_template()).render(template_vars)
+        except (VariableDoesNotExist, TemplateSyntaxError):
+            return
+
     @classmethod
     def anonymize_bookings(cls, bookings_queryset):
         bookings_queryset.update(
diff --git a/chrono/manager/templates/chrono/manager_event_check.html b/chrono/manager/templates/chrono/manager_event_check.html
index 33b74d2..c113bf5 100644
--- a/chrono/manager/templates/chrono/manager_event_check.html
+++ b/chrono/manager/templates/chrono/manager_event_check.html
@@ -31,13 +31,15 @@
   <h3>
     {% blocktrans with places=object.waiting_list_places booked_places=waiting|length %}Waiting List ({{ booked_places }}/{{ places }}){% endblocktrans %}
   </h3>
-<div>
-  <ul class="objects-list single-links">
-    {% for booking in waiting %}
-    <li><a>{{ booking.user_name|default:booking.label|default:_('Unknown') }}</a></li>
-    {% endfor %}
-  </ul>
-</div>
+  <div>
+    <table class="main check-bookings">
+      <tbody>
+        {% for booking in waiting %}
+        <tr><td class="booking-username">{{ booking.get_user_block }}</td></tr>
+        {% endfor %}
+      </tbody>
+    </table>
+  </div>
 </div>
 {% endif %}
 {% endblock %}
diff --git a/chrono/manager/templates/chrono/manager_event_check_booking_fragment.html b/chrono/manager/templates/chrono/manager_event_check_booking_fragment.html
index 4add784..3dbc2bd 100644
--- a/chrono/manager/templates/chrono/manager_event_check_booking_fragment.html
+++ b/chrono/manager/templates/chrono/manager_event_check_booking_fragment.html
@@ -1,6 +1,6 @@
 {% load i18n %}
 
-<td class="booking-username">{{ booking.user_name|default:booking.label|default:_('Unknown') }}</td>
+<td class="booking-username">{{ booking.get_user_block }}</td>
 <td class="booking-status">
   {{ booking.user_was_present|yesno:_('Present,Absent,-') }}
   {% if booking.user_was_present is False and booking.user_absence_reason %}
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_settings.html b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
index aff91b4..191566a 100644
--- a/chrono/manager/templates/chrono/manager_events_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
@@ -50,6 +50,15 @@
 </div>
 {% endif %}
 
+<div class="section">
+<h3>{% trans "Booking user block template" %}
+<a rel="popup" class="button" href="{% url 'chrono-manager-agenda-booking-user-template' pk=object.pk %}">{% trans 'Configure' %}</a>
+</h3>
+<div>
+    <pre>{{ agenda.get_booking_user_block_template }}</pre>
+</div>
+</div>
+
 <div class="section">
 <h3>{% trans "Notifications" %}
 <a rel="popup" class="button" href="{% url 'chrono-manager-agenda-notifications-settings' pk=object.id %}">{% trans 'Configure' %}</a>
diff --git a/chrono/manager/urls.py b/chrono/manager/urls.py
index 848e188..90428e8 100644
--- a/chrono/manager/urls.py
+++ b/chrono/manager/urls.py
@@ -152,6 +152,11 @@ urlpatterns = [
         views.agenda_absence_reasons,
         name='chrono-manager-agenda-absence-reasons',
     ),
+    url(
+        r'^agendas/(?P<pk>\d+)/booking-user-template$',
+        views.agenda_booking_user_template,
+        name='chrono-manager-agenda-booking-user-template',
+    ),
     url(r'^agendas/(?P<pk>\d+)/delete$', views.agenda_delete, name='chrono-manager-agenda-delete'),
     url(r'^agendas/(?P<pk>\d+)/export$', views.agenda_export, name='chrono-manager-agenda-export'),
     url(r'^agendas/(?P<pk>\d+)/add-event$', views.agenda_add_event, name='chrono-manager-agenda-add-event'),
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index f7e39fa..a974c17 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -924,6 +924,21 @@ class AgendaAbsenceReasonsView(AgendaEditView):
 agenda_absence_reasons = AgendaAbsenceReasonsView.as_view()
 
 
+class AgendaBookingUserTemplateView(AgendaEditView):
+    form_class = None
+    fields = ['booking_user_block_template']
+    title = _("Configure booking's user block template")
+
+    def set_agenda(self, **kwargs):
+        self.agenda = get_object_or_404(Agenda, pk=kwargs.get('pk'), kind='events')
+
+    def get_initial(self):
+        return {'booking_user_block_template': self.agenda.get_booking_user_block_template()}
+
+
+agenda_booking_user_template = AgendaBookingUserTemplateView.as_view()
+
+
 class AgendaDeleteView(DeleteView):
     template_name = 'chrono/manager_confirm_delete.html'
     model = Agenda
diff --git a/tests/manager/test_all.py b/tests/manager/test_all.py
index 46a1d70..dbb5d44 100644
--- a/tests/manager/test_all.py
+++ b/tests/manager/test_all.py
@@ -492,6 +492,42 @@ def test_options_virtuale_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):
+    agenda = Agenda.objects.create(label='Foo bar', kind='events')
+    assert agenda.booking_user_block_template == ''
+    assert (
+        agenda.get_booking_user_block_template()
+        == '{{ booking.user_name|default:booking.label|default:"Unknown" }}'
+    )
+
+    app = login(app)
+    url = '/manage/agendas/%s/booking-user-template' % agenda.pk
+    resp = app.get(url)
+    resp.form['booking_user_block_template'] = '{{ booking.user_name }} Foo Bar'
+    resp = resp.form.submit()
+    agenda.refresh_from_db()
+    assert agenda.booking_user_block_template == '{{ booking.user_name }} Foo Bar'
+    assert agenda.get_booking_user_block_template() == '{{ booking.user_name }} Foo Bar'
+
+    resp = app.get(url)
+    resp.form['booking_user_block_template'] = ''
+    resp = resp.form.submit()
+    agenda.refresh_from_db()
+    assert agenda.booking_user_block_template == ''
+    assert (
+        agenda.get_booking_user_block_template()
+        == '{{ booking.user_name|default:booking.label|default:"Unknown" }}'
+    )
+
+    # check kind
+    agenda.kind = 'meetings'
+    agenda.save()
+    app.get(url, status=404)
+    agenda.kind = 'virtual'
+    agenda.save()
+    app.get(url, status=404)
+
+
 def test_options_agenda_as_manager(app, manager_user):
     agenda = Agenda(label=u'Foo bar')
     agenda.view_role = manager_user.groups.all()[0]
diff --git a/tests/manager/test_event.py b/tests/manager/test_event.py
index 9427b65..248faf0 100644
--- a/tests/manager/test_event.py
+++ b/tests/manager/test_event.py
@@ -1102,6 +1102,12 @@ def test_event_check(app, admin_user):
     )  # user ordering is not optimal ...
     assert 'User Cancelled' not in resp
 
+    agenda.booking_user_block_template = '{{ booking.user_name }} Foo Bar'
+    agenda.save()
+    resp = app.get('/manage/agendas/%s/events/%s/check' % (agenda.pk, event.pk))
+    assert 'User 1 Foo Bar' in resp
+    assert 'User Waiting Foo Bar' in resp
+
     # cancelled booking
     token = resp.context['csrf_token']
     app.post(
-- 
2.30.2

