From 127e0e5dd9c12ae05e88e34ef493b4a465bcfa85 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 3 Jun 2022 15:57:54 +0200
Subject: [PATCH 1/2] manager: add a link to go on agenda's lingo config
 (#65986)

---
 chrono/agendas/models.py                       |  9 +++++++++
 .../chrono/manager_agenda_settings.html        |  2 +-
 .../chrono/manager_events_agenda_settings.html |  6 ++++++
 tests/manager/test_all.py                      | 18 ++++++++++++++++++
 4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 53f1b008..5412b3fe 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -283,6 +283,15 @@ class Agenda(models.Model):
     def get_settings_url(self):
         return reverse('chrono-manager-agenda-settings', kwargs={'pk': self.id})
 
+    def get_lingo_url(self):
+        if not settings.KNOWN_SERVICES.get('lingo'):
+            return
+        lingo = list(settings.KNOWN_SERVICES['lingo'].values())[0]
+        lingo_url = lingo.get('url') or ''
+        if not lingo_url.endswith('/'):
+            lingo_url += '/'
+        return '%smanage/pricing/agenda/%s/' % (lingo_url, self.slug)
+
     def can_be_managed(self, user):
         if user.is_staff:
             return True
diff --git a/chrono/manager/templates/chrono/manager_agenda_settings.html b/chrono/manager/templates/chrono/manager_agenda_settings.html
index d1f85854..781e3ad8 100644
--- a/chrono/manager/templates/chrono/manager_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_agenda_settings.html
@@ -20,6 +20,7 @@
   {% endblock %}
   <ul class="extra-actions-menu">
     <li><a rel="popup" href="{% url 'chrono-manager-agenda-edit' pk=object.id %}">{% trans 'Options' %}</a></li>
+    {% block agenda-extra-menu-actions %}{% endblock %}
     <li><a rel="popup" class="action-duplicate" href="{% url 'chrono-manager-agenda-duplicate' pk=object.pk %}">{% trans 'Duplicate' %}</a></li>
     <li><a download href="{% url 'chrono-manager-agenda-export' pk=object.id %}">{% trans 'Export Configuration (JSON)' %}</a></li>
     {% if object.kind == 'events' %}
@@ -28,7 +29,6 @@
     {% if user.is_staff %}
       <li><a rel="popup" href="{% url 'chrono-manager-agenda-delete' pk=object.id %}">{% trans 'Delete' %}</a></li>
     {% endif %}
-    {% block agenda-extra-menu-actions %}{% endblock %}
   </ul>
 </span>
 {% endblock %}
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_settings.html b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
index e7a02d6d..cd1b0c3b 100644
--- a/chrono/manager/templates/chrono/manager_events_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
@@ -6,6 +6,12 @@
   <a rel="popup" href="{% url 'chrono-manager-agenda-add-event' pk=object.id %}">{% trans 'New Event' %}</a>
 {% endblock %}
 
+{% block agenda-extra-menu-actions %}
+  {% with lingo_url=object.get_lingo_url %}{% if lingo_url %}
+    <li><a href="{{ lingo_url }}">{% trans 'Pricing' context 'pricing' %}</a></li>
+  {% endif %}{% endwith %}
+{% endblock %}
+
 {% block agenda-settings %}
 
 <div class="section">
diff --git a/tests/manager/test_all.py b/tests/manager/test_all.py
index 96736450..2cbeba91 100644
--- a/tests/manager/test_all.py
+++ b/tests/manager/test_all.py
@@ -465,6 +465,24 @@ def test_options_events_agenda_delays(settings, app, admin_user):
     assert agenda.minimal_booking_delay_in_working_days is True
 
 
+def test_options_events_agenda_lingo_link(settings, app, admin_user):
+    settings.KNOWN_SERVICES = {}
+    agenda = Agenda.objects.create(label='Foo bar')
+
+    app = login(app)
+    resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
+    assert 'Pricing' not in resp
+    assert '/manage/pricing/agenda/%s/' % agenda.slug not in resp
+
+    settings.KNOWN_SERVICES['lingo'] = {'default': {'url': 'https://lingo.dev'}}
+    resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
+    assert 'Pricing' in resp
+    assert 'https://lingo.dev/manage/pricing/agenda/%s/' % agenda.slug in resp
+    settings.KNOWN_SERVICES['lingo'] = {'default': {'url': 'https://lingo.dev/'}}
+    resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
+    assert 'https://lingo.dev/manage/pricing/agenda/%s/' % agenda.slug in resp
+
+
 def test_options_virtual_agenda_delays(app, admin_user):
     agenda = Agenda.objects.create(label='Foo bar', kind='virtual', maximal_booking_delay=2)
     assert agenda.maximal_booking_delay == 2
-- 
2.35.1

