From b8522717b60dc2330215e600623e90747d729933 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 15 Apr 2021 12:41:55 +0200
Subject: [PATCH] backoffice: display mail templates usage on workflow page
 (#51898)

---
 tests/admin_pages/test_workflow.py         | 25 ++++++++++++++++++++++
 wcs/templates/wcs/backoffice/workflow.html | 12 +++++++++--
 wcs/workflows.py                           |  5 +++++
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/tests/admin_pages/test_workflow.py b/tests/admin_pages/test_workflow.py
index ba02cfa3..b055de2b 100644
--- a/tests/admin_pages/test_workflow.py
+++ b/tests/admin_pages/test_workflow.py
@@ -7,16 +7,17 @@ import re
 import mock
 import pytest
 from quixote.http_request import Upload as QuixoteUpload
 from webtest import Upload
 
 from wcs import fields
 from wcs.carddef import CardDef
 from wcs.formdef import FormDef
+from wcs.mail_templates import MailTemplate
 from wcs.qommon.errors import ConnectionError
 from wcs.qommon.form import UploadedFile
 from wcs.qommon.http_request import HTTPRequest
 from wcs.wf.create_carddata import CreateCarddataWorkflowStatusItem
 from wcs.wf.create_formdata import CreateFormdataWorkflowStatusItem, Mapping
 from wcs.wf.dispatch import DispatchWorkflowStatusItem
 from wcs.wf.edit_carddata import EditCarddataWorkflowStatusItem
 from wcs.wf.export_to_model import ExportToModel
@@ -26,16 +27,17 @@ from wcs.wf.jump import JumpWorkflowStatusItem
 from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
 from wcs.wf.register_comment import RegisterCommenterWorkflowStatusItem
 from wcs.wf.wscall import WebserviceCallStatusItem
 from wcs.workflows import (
     ChoiceWorkflowStatusItem,
     CommentableWorkflowStatusItem,
     DisplayMessageWorkflowStatusItem,
     JumpOnSubmitWorkflowStatusItem,
+    SendmailWorkflowStatusItem,
     Workflow,
     WorkflowBackofficeFieldsFormDef,
     WorkflowCriticalityLevel,
     WorkflowVariablesFieldsFormDef,
     item_classes,
 )
 
 from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
@@ -299,16 +301,20 @@ def test_workflows_delete(pub):
     assert resp.location == 'http://example.net/backoffice/workflows/'
     resp = resp.follow()
     assert Workflow.count() == 0
 
 
 def test_workflows_usage(pub):
     Workflow.wipe()
     workflow = Workflow(name='foo')
+    st1 = workflow.add_status('Status1')
+    item = SendmailWorkflowStatusItem()
+    st1.items.append(item)
+    item.parent = st1
     workflow.store()
 
     create_superuser(pub)
     app = login(get_app(pub))
 
     resp = app.get('/backoffice/workflows/%s/' % workflow.id)
     assert 'Usage' not in resp.text
 
@@ -333,16 +339,35 @@ def test_workflows_usage(pub):
     assert '/cards/%s/' % carddef.id in resp.text
 
     formdef.remove_self()
 
     resp = app.get('/backoffice/workflows/%s/' % workflow.id)
     assert 'Usage' in resp.text
     assert '/cards/%s/' % carddef.id in resp.text
 
+    mail_template = MailTemplate()
+    mail_template.name = 'Mail template title'
+    mail_template.store()
+
+    assert workflow.mail_templates() == []
+    item.mail_template = mail_template.slug
+    workflow.store()
+    assert [x.id for x in workflow.mail_templates()] == [mail_template.id]
+
+    resp = app.get('/backoffice/workflows/%s/' % workflow.id)
+    assert 'Usage' in resp.text
+    assert '/mail-templates/%s/' % mail_template.id in resp.text
+
+    carddef.remove_self()
+
+    resp = app.get('/backoffice/workflows/%s/' % workflow.id)
+    assert 'Usage' in resp.text
+    assert '/mail-templates/%s/' % mail_template.id in resp.text
+
 
 def test_workflows_export_import(pub):
     create_superuser(pub)
 
     Workflow.wipe()
     workflow = Workflow(name='foo')
     workflow.add_status(name='baz')
     workflow.store()
diff --git a/wcs/templates/wcs/backoffice/workflow.html b/wcs/templates/wcs/backoffice/workflow.html
index 319283cd..ac86d72a 100644
--- a/wcs/templates/wcs/backoffice/workflow.html
+++ b/wcs/templates/wcs/backoffice/workflow.html
@@ -143,18 +143,18 @@
 
 {% if workflow.possible_status %}
 <div class="bo-block">
 {{ view.graphviz|safe }}
 <div class="full-screen-link"><a href="schema.svg">{% trans "Full Screen" %}</a></div>
 </div>
 {% endif %}
 
-{% with formdefs=workflow.formdefs carddefs=workflow.carddefs %}
-{% if formdefs or carddefs %}
+{% with formdefs=workflow.formdefs carddefs=workflow.carddefs mail_templates=workflow.mail_templates %}
+{% if formdefs or carddefs or mail_templates %}
 <div class="section">
 <h3>{% trans "Usage" %}</h3>
 <div>
 {% if formdefs %}
 <p>{% trans "This workflow is used for the following forms:" %}
 <ul class="objects-list single-links">
 {% for formdef in formdefs %}
 <li><a href="{{ formdef.get_admin_url }}">{{ formdef.name }}</a></li>
@@ -164,14 +164,22 @@
 {% if carddefs %}
 <p>{% trans "This workflow is used for the following card models:" %}
 <ul class="objects-list single-links">
 {% for carddef in carddefs %}
 <li><a href="{{ carddef.get_admin_url }}">{{ carddef.name }}</a></li>
 {% endfor %}
 </ul>
 {% endif %}
+{% if mail_templates %}
+<p>{% trans "The following mail templates are used in this workflow:" %}
+<ul class="objects-list single-links">
+{% for mail_template in mail_templates %}
+<li><a href="{{ mail_template.get_admin_url }}">{{ mail_template.name }}</a></li>
+{% endfor %}
+</ul>
+{% endif %}
 </div>
 </div>
 {% endif %}
 {% endwith %}
 
 {% endblock %}
diff --git a/wcs/workflows.py b/wcs/workflows.py
index 5f2364e9..e2f9c70a 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -921,16 +921,21 @@ class Workflow(StorableObject):
     def formdefs(self, **kwargs):
         order_by = kwargs.pop('order_by', 'name')
         return list(FormDef.select(lambda x: x.workflow_id == self.id, order_by=order_by, **kwargs))
 
     def carddefs(self, **kwargs):
         order_by = kwargs.pop('order_by', 'name')
         return list(CardDef.select(lambda x: x.workflow_id == self.id, order_by=order_by, **kwargs))
 
+    def mail_templates(self):
+        slugs = [x.mail_template for x in self.get_all_items() if x.key == 'sendmail' and x.mail_template]
+        criterias = [Contains('slug', slugs)]
+        return list(MailTemplate.select(criterias, order_by='name'))
+
 
 class XmlSerialisable:
     node_name = None
     key = None
 
     def export_to_xml(self, charset, include_id=False):
         node = ET.Element(self.node_name)
         if self.key:
-- 
2.30.2

