From 8fc42793caeae031ea160c0ddf3f0c252b061e65 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 6 Dec 2022 18:23:46 +0100
Subject: [PATCH 2/2] api: comment template dependencies (#39178)

---
 tests/api/test_export_import.py | 54 ++++++++++++++++++++++++++++++++-
 wcs/api_export_import.py        | 16 ++++++++++
 wcs/comment_templates.py        | 11 +++++++
 wcs/mail_templates.py           |  9 ++++++
 wcs/wf/register_comment.py      |  9 ++++++
 5 files changed, 98 insertions(+), 1 deletion(-)

diff --git a/tests/api/test_export_import.py b/tests/api/test_export_import.py
index d5213c162..a9a1f89a7 100644
--- a/tests/api/test_export_import.py
+++ b/tests/api/test_export_import.py
@@ -12,10 +12,12 @@ from wcs.categories import (
     BlockCategory,
     CardDefCategory,
     Category,
+    CommentTemplateCategory,
     DataSourceCategory,
     MailTemplateCategory,
     WorkflowCategory,
 )
+from wcs.comment_templates import CommentTemplate
 from wcs.data_sources import NamedDataSource
 from wcs.fields import BlockField, CommentField, PageField, StringField
 from wcs.formdef import FormDef
@@ -49,6 +51,8 @@ coucou = 1234
     Workflow.wipe()
     MailTemplateCategory.wipe()
     MailTemplate.wipe()
+    CommentTemplateCategory.wipe()
+    CommentTemplate.wipe()
     DataSourceCategory.wipe()
     NamedDataSource.wipe()
     NamedWsCall.wipe()
@@ -132,6 +136,8 @@ def test_export_import_dependencies(pub):
     wscall.store()
     wscall = NamedWsCall(name='Test quinquies')
     wscall.store()
+    wscall = NamedWsCall(name='Test sexies')
+    wscall.store()
 
     carddef = CardDef()
     carddef.name = 'Test'
@@ -142,6 +148,9 @@ def test_export_import_dependencies(pub):
     carddef = CardDef()
     carddef.name = 'Test ter'
     carddef.store()
+    carddef = CardDef()
+    carddef.name = 'Test quater'
+    carddef.store()
 
     formdef = FormDef()
     formdef.name = 'Test bis'
@@ -153,6 +162,9 @@ def test_export_import_dependencies(pub):
     formdef.name = 'Test quater'
     formdef.store()
     formdef = FormDef()
+    formdef.name = 'Test quinquies'
+    formdef.store()
+    formdef = FormDef()
     formdef.name = 'Test'
     formdef.store()
 
@@ -237,6 +249,12 @@ def test_export_import_dependencies(pub):
         'value': '{{ cards|objects:"test-ter" }} {{ webservice.test_ter }}',
     }
 
+    register_comment = status.add_action('register-comment')
+    register_comment.to = [role.id]
+    register_comment.comment = (
+        '{{ cards|objects:"test-quater" }} {{ forms|objects:"test-quinquies" }} {{ webservice.test_sexies }}'
+    )
+
     dispatch_auto = status.add_action('dispatch')
     dispatch_auto.rules = [{'role_id': role.id, 'value': 'xxx'}]
 
@@ -304,11 +322,14 @@ def test_export_import_dependencies(pub):
         ('test', 'wscalls'),
         ('test_bis', 'wscalls'),
         ('test_ter', 'wscalls'),
+        ('test_sexies', 'wscalls'),
         ('test', 'cards'),
         ('test-bis', 'cards'),
         ('test-ter', 'cards'),
+        ('test-quater', 'cards'),
         ('test-ter', 'forms'),
         ('test-bis', 'forms'),
+        ('test-quinquies', 'forms'),
         ('test', 'blocks'),
         ('test-role', 'roles'),
     }
@@ -323,6 +344,9 @@ def test_export_import_dependencies(pub):
     mail_template = MailTemplate(name='test mail template')
     mail_template.store()
     send_mail.mail_template = mail_template.slug
+    comment_template = CommentTemplate(name='test comment template')
+    comment_template.store()
+    register_comment.comment_template = comment_template.slug
     workflow.store()
     resp = get_app(pub).get(sign_uri('/api/export-import/workflows/'))
     resp = get_app(pub).get(sign_uri(resp.json['data'][0]['urls']['dependencies']))
@@ -335,6 +359,7 @@ def test_export_import_dependencies(pub):
         ('test-bis', 'forms'),
         ('test', 'blocks'),
         ('test-mail-template', 'mail-templates'),
+        ('test-comment-template', 'comment-templates'),
         ('test-role', 'roles'),
     }
     for dependency in resp.json['data']:
@@ -345,12 +370,39 @@ def test_export_import_dependencies(pub):
     cat = MailTemplateCategory(name='Cat')
     cat.store()
     mail_template.category_id = cat.id
+    mail_template.subject = '{{ webservice.test }}'
+    mail_template.body = '{{ cards|objects:"test" }} {{ forms|objects:"test-ter" }}'
     mail_template.store()
     resp = get_app(pub).get(sign_uri('/api/export-import/workflows/'))
     resp = get_app(pub).get(sign_uri(resp.json['data'][0]['urls']['dependencies']))
     mail_template_entry = [x for x in resp.json['data'] if x['type'] == 'mail-templates'][0]
     resp = get_app(pub).get(sign_uri(mail_template_entry['urls']['dependencies']))
-    assert {(x['id'], x['type']) for x in resp.json['data']} == {('cat', 'mail-templates-categories')}
+    assert {(x['id'], x['type']) for x in resp.json['data']} == {
+        ('cat', 'mail-templates-categories'),
+        ('test', 'cards'),
+        ('test', 'wscalls'),
+        ('test-ter', 'forms'),
+    }
+    for dependency in resp.json['data']:
+        get_app(pub).get(sign_uri(dependency['urls']['export']))
+
+    cat = CommentTemplateCategory(name='Cat')
+    cat.store()
+    comment_template.category_id = cat.id
+    comment_template.comment = (
+        '{{ cards|objects:"test-quater" }} {{ forms|objects:"test-quinquies" }} {{ webservice.test_sexies }}'
+    )
+    comment_template.store()
+    resp = get_app(pub).get(sign_uri('/api/export-import/workflows/'))
+    resp = get_app(pub).get(sign_uri(resp.json['data'][0]['urls']['dependencies']))
+    comment_template_entry = [x for x in resp.json['data'] if x['type'] == 'comment-templates'][0]
+    resp = get_app(pub).get(sign_uri(comment_template_entry['urls']['dependencies']))
+    assert {(x['id'], x['type']) for x in resp.json['data']} == {
+        ('cat', 'comment-templates-categories'),
+        ('test-quater', 'cards'),
+        ('test-quinquies', 'forms'),
+        ('test_sexies', 'wscalls'),
+    }
     for dependency in resp.json['data']:
         get_app(pub).get(sign_uri(dependency['urls']['export']))
 
diff --git a/wcs/api_export_import.py b/wcs/api_export_import.py
index 3c89aefe6..d0126dbe7 100644
--- a/wcs/api_export_import.py
+++ b/wcs/api_export_import.py
@@ -30,10 +30,12 @@ from wcs.categories import (
     BlockCategory,
     CardDefCategory,
     Category,
+    CommentTemplateCategory,
     DataSourceCategory,
     MailTemplateCategory,
     WorkflowCategory,
 )
+from wcs.comment_templates import CommentTemplate
 from wcs.data_sources import NamedDataSource, StubNamedDataSource
 from wcs.formdef import FormDef
 from wcs.mail_templates import MailTemplate
@@ -57,6 +59,8 @@ klasses = {
     'roles': Role,
     'mail-templates-categories': MailTemplateCategory,
     'mail-templates': MailTemplate,
+    'comment-templates-categories': CommentTemplateCategory,
+    'comment-templates': CommentTemplate,
     'workflows-categories': WorkflowCategory,
     'workflows': Workflow,
     'wscalls': NamedWsCall,
@@ -83,6 +87,12 @@ def index(request):
         {'id': 'blocks', 'text': _('Blocks'), 'singular': _('Block of fields'), 'minor': True},
         {'id': 'data-sources', 'text': _('Data Sources'), 'singular': _('Data Source'), 'minor': True},
         {'id': 'mail-templates', 'text': _('Mail Templates'), 'singular': _('Mail Template'), 'minor': True},
+        {
+            'id': 'comment-templates',
+            'text': _('Comment Templates'),
+            'singular': _('Mail Template'),
+            'minor': True,
+        },
         {'id': 'wscalls', 'text': _('Webservice Calls'), 'singular': _('Webservice Call'), 'minor': True},
         {
             'id': 'blocks-categories',
@@ -114,6 +124,12 @@ def index(request):
             'singular': _('Category (mail templates)'),
             'minor': True,
         },
+        {
+            'id': 'comment-templates-categories',
+            'text': _('Categories (comment templates)'),
+            'singular': _('Category (comment templates)'),
+            'minor': True,
+        },
         {
             'id': 'data-sources-categories',
             'text': _('Categories (data sources)'),
diff --git a/wcs/comment_templates.py b/wcs/comment_templates.py
index 8a2fc473d..069adf07c 100644
--- a/wcs/comment_templates.py
+++ b/wcs/comment_templates.py
@@ -21,6 +21,7 @@ from quixote import get_publisher
 from wcs.categories import CommentTemplateCategory
 from wcs.qommon import _, get_logger
 from wcs.qommon.form import OptGroup
+from wcs.qommon.misc import check_carddefs, check_formdefs, check_wscalls
 from wcs.qommon.xml_storage import XmlStorableObject
 
 
@@ -126,6 +127,16 @@ class CommentTemplate(XmlStorableObject):
             get_logger().warning("comment template '%s' does not exist" % slug)
         return comment_template
 
+    def get_dependencies(self):
+        yield self.category
+        for string in self.get_computed_strings():
+            yield from check_wscalls(string)
+            yield from check_carddefs(string)
+            yield from check_formdefs(string)
+
+    def get_computed_strings(self):
+        yield self.comment
+
     def export_to_xml(self, include_id=False):
         root = super().export_to_xml(include_id=include_id)
         CommentTemplateCategory.object_category_xml_export(self, root, include_id=include_id)
diff --git a/wcs/mail_templates.py b/wcs/mail_templates.py
index f514cc6a8..b54ee99d2 100644
--- a/wcs/mail_templates.py
+++ b/wcs/mail_templates.py
@@ -21,6 +21,7 @@ from quixote import get_publisher
 from wcs.categories import MailTemplateCategory
 from wcs.qommon import _, get_logger
 from wcs.qommon.form import OptGroup
+from wcs.qommon.misc import check_carddefs, check_formdefs, check_wscalls
 from wcs.qommon.xml_storage import XmlStorableObject
 
 
@@ -130,6 +131,14 @@ class MailTemplate(XmlStorableObject):
 
     def get_dependencies(self):
         yield self.category
+        for string in self.get_computed_strings():
+            yield from check_wscalls(string)
+            yield from check_carddefs(string)
+            yield from check_formdefs(string)
+
+    def get_computed_strings(self):
+        yield self.subject
+        yield self.body
 
     def export_to_xml(self, include_id=False):
         root = super().export_to_xml(include_id=include_id)
diff --git a/wcs/wf/register_comment.py b/wcs/wf/register_comment.py
index d54ef5739..735c97cf8 100644
--- a/wcs/wf/register_comment.py
+++ b/wcs/wf/register_comment.py
@@ -89,6 +89,10 @@ class RegisterCommenterWorkflowStatusItem(WorkflowStatusItem):
     to = None
     attachments = None
 
+    def get_dependencies(self):
+        yield from super().get_dependencies()
+        yield CommentTemplate.get_by_slug(self.comment_template)
+
     def add_parameters_widgets(self, form, parameters, prefix='', formdef=None, **kwargs):
         super().add_parameters_widgets(form, parameters, prefix=prefix, formdef=formdef, **kwargs)
         subject_body_attrs = {}
@@ -144,6 +148,11 @@ class RegisterCommenterWorkflowStatusItem(WorkflowStatusItem):
                 get_publisher().record_error(exception=e, context='[comment/attachments]', notify=True)
             continue
 
+    def get_computed_strings(self):
+        yield from super().get_computed_strings()
+        if not self.comment_template:
+            yield self.comment
+
     def perform(self, formdata):
         if not formdata.evolution:
             return
-- 
2.35.1

