api: comment template dependencies (#39178)

This commit is contained in:
Lauréline Guérin 2022-12-06 18:23:46 +01:00
parent 2152ddbe67
commit 8bfc31bc00
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 98 additions and 1 deletions

View File

@ -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']))

View File

@ -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)'),

View File

@ -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)

View File

@ -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)

View File

@ -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