From 2635964ecf3be2ecd7dd07e179d1e13c71342b05 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 2 Jun 2022 15:17:30 +0200
Subject: [PATCH] pricing: add links from agenda to pricing and reverse
 (#65323)

---
 lingo/manager/static/css/style.scss           |  6 ++++-
 .../lingo/pricing/manager_agenda_detail.html  |  5 +++-
 .../lingo/pricing/manager_pricing_detail.html | 23 +++++++++++++++++++
 lingo/pricing/views.py                        |  6 +++++
 tests/pricing/manager/test_agenda.py          |  4 ++++
 tests/pricing/manager/test_pricing.py         | 21 ++++++++++++++++-
 6 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/lingo/manager/static/css/style.scss b/lingo/manager/static/css/style.scss
index 7c9da0a..26d3a2f 100644
--- a/lingo/manager/static/css/style.scss
+++ b/lingo/manager/static/css/style.scss
@@ -31,7 +31,7 @@ div.paragraph {
 		width: 2em;
 		height: 100%;
 		box-sizing: border-box;
-        font-weight: normal;
+		font-weight: normal;
 	}
 }
 
@@ -43,3 +43,7 @@ ul.objects-list.sortable {
 		}
 	}
 }
+
+ul.objects-list.single-links li a.delete::before {
+	content: "\f08e"; /* fa-external-link  */
+}
diff --git a/lingo/pricing/templates/lingo/pricing/manager_agenda_detail.html b/lingo/pricing/templates/lingo/pricing/manager_agenda_detail.html
index 51a6081..d32a565 100644
--- a/lingo/pricing/templates/lingo/pricing/manager_agenda_detail.html
+++ b/lingo/pricing/templates/lingo/pricing/manager_agenda_detail.html
@@ -23,7 +23,10 @@
   {% if agenda_pricings %}
   <ul class="objects-list single-links">
   {% for agenda_pricing in agenda_pricings %}
-    <li><a href="{% url 'lingo-manager-agenda-pricing-detail' agenda.pk agenda_pricing.pk %}">{{ agenda_pricing.pricing }} ({{ agenda_pricing.date_start|date:'d/m/Y' }} - {{ agenda_pricing.date_end|date:'d/m/Y' }})</a></li>
+    <li>
+      <a href="{% url 'lingo-manager-agenda-pricing-detail' agenda.pk agenda_pricing.pk %}">{{ agenda_pricing.pricing }} ({{ agenda_pricing.date_start|date:'d/m/Y' }} - {{ agenda_pricing.date_end|date:'d/m/Y' }})</a>
+      <a href="{% url 'lingo-manager-pricing-detail' agenda_pricing.pricing.pk %}" class="link-action-icon delete">voir</a>
+    </li>
   {% endfor %}
   </ul>
   {% else %}
diff --git a/lingo/pricing/templates/lingo/pricing/manager_pricing_detail.html b/lingo/pricing/templates/lingo/pricing/manager_pricing_detail.html
index 12679b5..7910834 100644
--- a/lingo/pricing/templates/lingo/pricing/manager_pricing_detail.html
+++ b/lingo/pricing/templates/lingo/pricing/manager_pricing_detail.html
@@ -71,4 +71,27 @@
   {% endwith %}
   </div>
 </div>
+
+<div class="section">
+  <h3>{% trans "Used in agendas" %}</h3>
+  <div>
+  {% if agendas %}
+    <ul class="objects-list single-links">
+      {% for agenda in agendas %}
+      <li>
+        <a href="{% url 'lingo-manager-agenda-detail' pk=agenda.pk %}">
+          {{ agenda.label }}
+        </a>
+      </li>
+      {% endfor %}
+    </ul>
+  {% else %}
+  <div class="big-msg-info">
+    {% blocktrans %}
+    This Pricing model is not used yet.
+    {% endblocktrans %}
+  </div>
+  {% endif %}
+  </div>
+</div>
 {% endblock %}
diff --git a/lingo/pricing/views.py b/lingo/pricing/views.py
index dcd0e8f..bfc5ced 100644
--- a/lingo/pricing/views.py
+++ b/lingo/pricing/views.py
@@ -259,6 +259,12 @@ class PricingDetailView(DetailView):
             )
         )
 
+    def get_context_data(self, **kwargs):
+        kwargs['agendas'] = Agenda.objects.filter(
+            pk__in=AgendaPricing.objects.filter(pricing=self.object).values('agenda')
+        )
+        return super().get_context_data(**kwargs)
+
 
 pricing_detail = PricingDetailView.as_view()
 
diff --git a/tests/pricing/manager/test_agenda.py b/tests/pricing/manager/test_agenda.py
index 6235986..54b5578 100644
--- a/tests/pricing/manager/test_agenda.py
+++ b/tests/pricing/manager/test_agenda.py
@@ -58,6 +58,10 @@ def test_add_agenda_pricing(app, admin_user):
     assert agenda_pricing.date_start == datetime.date(2022, 9, 1)
     assert agenda_pricing.date_end == datetime.date(2023, 9, 1)
 
+    resp = app.get('/manage/pricing/agenda/%s/' % agenda.pk)
+    assert AgendaPricing.objects.filter(agenda=agenda, pricing=pricing).count() == 2
+    assert resp.text.count('"/manage/pricing/%s/"' % pricing.pk) == 2
+
 
 def test_edit_agenda_pricing(app, admin_user):
     agenda = Agenda.objects.create(label='Foo Bar')
diff --git a/tests/pricing/manager/test_pricing.py b/tests/pricing/manager/test_pricing.py
index e1f3b13..3dbc373 100644
--- a/tests/pricing/manager/test_pricing.py
+++ b/tests/pricing/manager/test_pricing.py
@@ -1,6 +1,9 @@
+import datetime
+
 import pytest
 
-from lingo.pricing.models import Criteria, CriteriaCategory, Pricing, PricingCriteriaCategory
+from lingo.agendas.models import Agenda
+from lingo.pricing.models import AgendaPricing, Criteria, CriteriaCategory, Pricing, PricingCriteriaCategory
 from tests.utils import login
 
 pytestmark = pytest.mark.django_db
@@ -28,6 +31,22 @@ def test_detail_pricing(app, admin_user):
     assert '/manage/pricing/%s/edit/' % pricing.pk in resp
     assert '/manage/pricing/%s/delete/' % pricing.pk in resp
 
+    agenda = Agenda.objects.create(label='Foo Bar')
+    AgendaPricing.objects.create(
+        agenda=agenda,
+        pricing=pricing,
+        date_start=datetime.date(year=2021, month=9, day=1),
+        date_end=datetime.date(year=2022, month=9, day=1),
+    )
+    AgendaPricing.objects.create(
+        agenda=agenda,
+        pricing=pricing,
+        date_start=datetime.date(year=2022, month=9, day=1),
+        date_end=datetime.date(year=2023, month=9, day=1),
+    )
+    resp = app.get('/manage/pricing/%s/' % pricing.pk)
+    assert resp.text.count('"/manage/pricing/agenda/%s/"' % agenda.pk) == 1
+
 
 def test_edit_pricing(app, admin_user):
     pricing = Pricing.objects.create(label='Model 1')
-- 
2.35.1

