From 078612122baf4d414edd11b2bbefedfa3e1dd551 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 6 May 2021 16:37:03 +0200
Subject: [PATCH] manager: add ReturnURL in booking form url (#52786)

---
 chrono/agendas/models.py                      | 22 +++++++++++--------
 .../chrono/manager_agenda_event_fragment.html |  2 +-
 .../chrono/manager_event_detail.html          |  2 +-
 tests/manager/test_event.py                   | 22 ++++++++++++-------
 4 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 934f0a0..c6d2703 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -43,6 +43,7 @@ from django.utils.encoding import force_text
 from django.utils.formats import date_format
 from django.utils.functional import cached_property
 from django.utils.module_loading import import_string
+from django.utils.safestring import mark_safe
 from django.utils.text import slugify
 from django.utils.timezone import is_aware, localtime, make_aware, make_naive, now, utc
 from django.utils.translation import ugettext
@@ -705,15 +706,6 @@ class Agenda(models.Model):
             recurring_events.filter(recurrence_end_date__isnull=False), excluded_datetimes
         )
 
-    def get_booking_form_url(self):
-        if not self.booking_form_url:
-            return
-        template_vars = Context(settings.TEMPLATE_VARS)
-        try:
-            return Template(self.booking_form_url).render(template_vars)
-        except (VariableDoesNotExist, TemplateSyntaxError):
-            return
-
     def get_recurrence_exceptions(self, min_start, max_start):
         return TimePeriodException.objects.filter(
             Q(desk__slug='_exceptions_holder', desk__agenda=self)
@@ -1341,6 +1333,18 @@ class Event(models.Model):
     def get_absolute_view_url(self):
         return reverse('chrono-manager-event-view', kwargs={'pk': self.agenda.id, 'event_pk': self.id})
 
+    def get_booking_form_url(self):
+        if not self.agenda.booking_form_url:
+            return
+        template_vars = Context(settings.TEMPLATE_VARS)
+        try:
+            url = Template(self.agenda.booking_form_url).render(template_vars)
+            url += '&' if '?' in url else '?'
+            url += 'agenda=%s&event=%s' % (self.agenda.slug, self.slug)
+            return mark_safe(url)
+        except (VariableDoesNotExist, TemplateSyntaxError):
+            return
+
     @classmethod
     def import_json(cls, data):
         data['start_datetime'] = make_aware(
diff --git a/chrono/manager/templates/chrono/manager_agenda_event_fragment.html b/chrono/manager/templates/chrono/manager_agenda_event_fragment.html
index 4bd12c2..5dc12cf 100644
--- a/chrono/manager/templates/chrono/manager_agenda_event_fragment.html
+++ b/chrono/manager/templates/chrono/manager_agenda_event_fragment.html
@@ -49,7 +49,7 @@
     {% if view_mode == 'settings_view' %}
     <a rel="popup" class="delete" href="{% url 'chrono-manager-event-delete' pk=agenda.pk event_pk=event.pk %}?next=settings">{% trans "remove" %}</a>
     {% elif agenda.booking_form_url %}
-    <a class="link-action-text" href="{{ agenda.get_booking_form_url }}?agenda={{ agenda.slug }}&event={{ event.slug }}">{% trans "Booking form" %}</a>
+    <a class="link-action-text" href="{{ event.get_booking_form_url }}&ReturnURL={{ request.build_absolute_uri }}">{% trans "Booking form" %}</a>
     {% endif %}
     <span class="occupation-bar"></span>
 </li>
diff --git a/chrono/manager/templates/chrono/manager_event_detail.html b/chrono/manager/templates/chrono/manager_event_detail.html
index dc72b17..dbe06ce 100644
--- a/chrono/manager/templates/chrono/manager_event_detail.html
+++ b/chrono/manager/templates/chrono/manager_event_detail.html
@@ -42,7 +42,7 @@
 <a href="{% url 'chrono-manager-event-check' pk=agenda.pk event_pk=object.pk %}">{% trans "Check" %}</a>
 {% endif %}
 {% if object.agenda.booking_form_url %}
-<a href="{{ object.agenda.get_booking_form_url }}?agenda={{ object.agenda.slug }}&event={{ event.slug }}">{% trans "Booking form" %}</a>
+<a href="{{ object.get_booking_form_url }}&ReturnURL={{ request.build_absolute_uri }}">{% trans "Booking form" %}</a>
 {% endif %}
 </span>
 {% endblock %}
diff --git a/tests/manager/test_event.py b/tests/manager/test_event.py
index 5dc79b7..a26f304 100644
--- a/tests/manager/test_event.py
+++ b/tests/manager/test_event.py
@@ -987,7 +987,7 @@ def test_event_booking_form_url(settings, app, admin_user):
 
     login(app)
 
-    assert agenda.get_booking_form_url() is None
+    assert event.get_booking_form_url() is None
     resp = app.get('/manage/agendas/%d/%d/%d/' % (agenda.pk, day.year, day.month))
     assert 'Booking form' not in resp.text
     resp = app.get('/manage/agendas/%d/events/open/' % agenda.pk)
@@ -998,24 +998,30 @@ def test_event_booking_form_url(settings, app, admin_user):
     agenda.booking_form_url = '{{ eservices_url }}backoffice/submission/inscription-aux-activites/'
     agenda.save()
     assert (
-        agenda.get_booking_form_url() == 'http://demarches/backoffice/submission/inscription-aux-activites/'
+        event.get_booking_form_url()
+        == 'http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s'
+        % (agenda.slug, event.slug)
     )
     resp = app.get('/manage/agendas/%d/%d/%d/' % (agenda.pk, day.year, day.month))
     assert (
-        '<a class="link-action-text" href="http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s">Booking form</a>'
-        % (agenda.slug, event.slug)
+        '<a class="link-action-text" href="http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s&ReturnURL=%s">Booking form</a>'
+        % (
+            agenda.slug,
+            event.slug,
+            'http://testserver/manage/agendas/%d/%d/%d/' % (agenda.pk, day.year, day.month),
+        )
         in resp.text
     )
     resp = app.get('/manage/agendas/%d/events/open/' % agenda.pk)
     assert (
-        '<a class="link-action-text" href="http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s">Booking form</a>'
-        % (agenda.slug, event.slug)
+        '<a class="link-action-text" href="http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s&ReturnURL=%s">Booking form</a>'
+        % (agenda.slug, event.slug, 'http://testserver/manage/agendas/%d/events/open/' % agenda.pk)
         in resp.text
     )
     resp = app.get('/manage/agendas/%d/events/%d/' % (agenda.pk, event.pk))
     assert (
-        '<a href="http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s">Booking form</a>'
-        % (agenda.slug, event.slug)
+        '<a href="http://demarches/backoffice/submission/inscription-aux-activites/?agenda=%s&event=%s&ReturnURL=%s">Booking form</a>'
+        % (agenda.slug, event.slug, 'http://testserver/manage/agendas/%d/events/%d/' % (agenda.pk, event.pk))
         in resp.text
     )
 
-- 
2.30.2

