backoffice: display mail templates usage on workflow page (#51898)

This commit is contained in:
Nicolas Roche 2021-04-15 12:41:55 +02:00
parent 86550e9259
commit ee403b7840
3 changed files with 40 additions and 2 deletions

View File

@ -12,6 +12,7 @@ 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
@ -31,6 +32,7 @@ from wcs.workflows import (
CommentableWorkflowStatusItem,
DisplayMessageWorkflowStatusItem,
JumpOnSubmitWorkflowStatusItem,
SendmailWorkflowStatusItem,
Workflow,
WorkflowBackofficeFieldsFormDef,
WorkflowCriticalityLevel,
@ -311,6 +313,10 @@ def test_workflows_delete(pub):
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)
@ -345,6 +351,25 @@ def test_workflows_usage(pub):
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)

View File

@ -148,8 +148,8 @@
</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>
@ -169,6 +169,14 @@
{% 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 %}

View File

@ -946,6 +946,11 @@ class Workflow(StorableObject):
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