From abdd06ad6b71c5a5cb5ccd72c68b5994ebbfeee5 Mon Sep 17 00:00:00 2001
From: Valentin Deniaud <vdeniaud@entrouvert.com>
Date: Tue, 8 Dec 2020 17:24:03 +0100
Subject: [PATCH] agendas: include event description in reminders (#49034)

---
 .../templates/agendas/events_reminder_body.html   | 14 ++++++++++++++
 .../templates/agendas/events_reminder_body.txt    | 10 ++++++++++
 chrono/manager/views.py                           | 13 ++++++++++++-
 tests/test_agendas.py                             | 15 ++++++++++++++-
 tests/test_manager.py                             | 10 ++++++++--
 5 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/chrono/agendas/templates/agendas/events_reminder_body.html b/chrono/agendas/templates/agendas/events_reminder_body.html
index 2134ca0..d396149 100644
--- a/chrono/agendas/templates/agendas/events_reminder_body.html
+++ b/chrono/agendas/templates/agendas/events_reminder_body.html
@@ -14,6 +14,20 @@ You have a booking for event "{{ event }}", on {{ date }} at {{ time }}.
 <p>{{ email_extra_info }}</p>
 {% endif %}
 
+{% if booking.event.description %}
+<p>{% trans "Additional information:" %}</p>
+{{ booking.event.description|linebreaks }}
+{% endif %}
+
+{% if booking.event.pricing %}
+<p>{% trans "Pricing:" %} {{ booking.event.pricing }}</p>
+{% endif %}
+
+{% if booking.event.url %}
+{% firstof event_url_button_label _("More information") as button_label %}
+{% include "emails/button-link.html" with url=booking.event.url label=button_label %}
+{% endif %}
+
 {% if booking.form_url %}
 {% with _("Edit or cancel booking") as button_label %}
 {% include "emails/button-link.html" with url=booking.form_url label=button_label %}
diff --git a/chrono/agendas/templates/agendas/events_reminder_body.txt b/chrono/agendas/templates/agendas/events_reminder_body.txt
index f61bd3f..3d3da21 100644
--- a/chrono/agendas/templates/agendas/events_reminder_body.txt
+++ b/chrono/agendas/templates/agendas/events_reminder_body.txt
@@ -9,6 +9,16 @@ You have a booking for event "{{ event }}", on {{ date }} at {{ time }}.
 {% if email_extra_info %}
 {{ email_extra_info }}
 {% endif %}
+{% if booking.event.description %}
+{% trans "Additional information:" %}
+{{ booking.event.description }}
+{% endif %}
+{% if booking.event.pricing %}
+{% trans "Pricing:" %} {{ booking.event.pricing }}
+{% endif %}
+{% if booking.event.url %}
+{% trans "More information:" %} {{ booking.event.url }}
+{% endif %}
 {% if booking.form_url %}
 {% trans "If in need to cancel it, you can do so here:" %} {{ booking.form_url }}
 {% endif %}
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index b23ddfd..74ad5e9 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -29,9 +29,12 @@ from django.db.models import Min, Max
 from django.http import Http404, HttpResponse, HttpResponseRedirect
 from django.shortcuts import get_object_or_404
 from django.shortcuts import redirect
+from django.template.defaultfilters import title
 from django.template.loader import render_to_string
 from django.urls import reverse, reverse_lazy
+from django.utils import lorem_ipsum
 from django.utils.dates import MONTHS
+from django.utils.html import format_html
 from django.utils.timezone import now, make_aware, make_naive, localtime
 from django.utils.translation import ugettext_lazy as _
 from django.utils.translation import ungettext
@@ -1595,18 +1598,26 @@ class AgendaReminderPreviewView(ManagedAgendaMixin, TemplateView):
         kind = self.agenda.kind
         days = self.agenda.reminder_settings.days
 
+        paragraph = lorem_ipsum.paragraphs(1)[0][:232]
+        label = title(lorem_ipsum.words(2))
         event = Event(
-            label='{{ event_label }}',
             start_datetime=datetime.datetime(year=2020, month=6, day=2, hour=14, minute=30),
+            label=format_html('{} <small>({})</small>', label, _('event label')),
+            description=format_html('{} <small>({})</small>', paragraph, _('event description, if present')),
+            pricing=format_html('{} <small>({})</small>', '...', _('event pricing, if present')),
+            url='#',
         )
+
         booking = Booking(user_display_label='{{ user_display_label }}', form_url='#')
         booking.event = event
+        button_label = format_html('{}<br>({})', _('More information'), _('link to event url, if present'))
         reminder_ctx = {
             'booking': booking,
             'in_x_days': _('tomorrow') if days == 1 else _('in %s days') % days,
             'date': booking.event.start_datetime,
             'email_extra_info': self.agenda.reminder_settings.email_extra_info,
             'sms_extra_info': self.agenda.reminder_settings.sms_extra_info,
+            'event_url_button_label': button_label,
         }
 
         if self.type_ == 'email':
diff --git a/tests/test_agendas.py b/tests/test_agendas.py
index 1b5cfee..794fbba 100644
--- a/tests/test_agendas.py
+++ b/tests/test_agendas.py
@@ -1491,7 +1491,15 @@ def test_agenda_reminders_email_content(mailoutbox, freezer):
         agenda=agenda, days=1, send_email=True, email_extra_info='Do no forget ID card.'
     )
     start_datetime = now() + datetime.timedelta(days=2)
-    event = Event.objects.create(agenda=agenda, start_datetime=start_datetime, places=10, label='Pool party')
+    event = Event.objects.create(
+        agenda=agenda,
+        start_datetime=start_datetime,
+        places=10,
+        label='Pool party',
+        description='Come !',
+        url='https://example.party',
+        pricing='10€',
+    )
 
     booking = Booking.objects.create(event=event, user_email='t@test.org')
 
@@ -1505,7 +1513,12 @@ def test_agenda_reminders_email_content(mailoutbox, freezer):
         assert 'Hi,' in body
         assert 'You have a booking for event "Pool party", on Friday 3 January at 2 p.m..' in body
         assert 'Do no forget ID card.' in body
+        assert 'Come !' in body
+        assert 'Pricing: 10€' in body
         assert not 'cancel' in body
+        assert not 'if present' in body  # assert separation with preview code
+    assert 'More information: https://example.party' in mail_bodies[0]
+    assert '<a href="https://example.party">More information</a>' in mail_bodies[1]
     mailoutbox.clear()
 
     freezer.move_to('2020-01-01 14:00')
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 427f22c..3db37b9 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -4762,14 +4762,20 @@ def test_manager_reminders_preview(app, admin_user):
     assert 'Users will receive the following email:' in resp.text
     assert len(resp.pyquery.find('p.email-subject')) == 1
     assert '<strong>Subject:</strong> Reminder for your booking tomorrow at 2:30 p.m.' in resp.text
-    assert 'You have a booking for event "{{ event_label }}", on Tuesday 2 June at 2:30 p.m..' in resp.text
+    assert (
+        'You have a booking for event "Lorem Ipsum <small>(event label)</small>", on Tuesday 2 June at 2:30 p.m..'
+        in resp.text
+    )
     assert 'An ID will be required' in resp.text
+    assert ' ea commodo consequat. <small>(event description, if present)</small>' in resp.text
+    assert 'Pricing: ... <small>(event pricing, if present)</small>' in resp.text
+    assert '<a href="#">More information<br>(link to event url, if present)</a>' in resp.text
 
     resp = resp.click('Return to settings')
     resp = resp.click('Preview SMS')
     assert 'Users will receive the following SMS:' in resp.text
     assert (
-        'Reminder: you have a booking for event "{{ event_label }}", on 02/06 at 2:30 p.m.. Take ID card.'
+        'Reminder: you have a booking for event "Lorem Ipsum <small>(event label)</small>", on 02/06 at 2:30 p.m.. Take ID card.'
         in resp.text
     )
 
-- 
2.20.1

