workflows: allow sending notifications without body text (#44796) #561

Merged
fpeters merged 2 commits from wip/44796-send-notifications-without-body into main 2023-08-11 00:31:49 +02:00
2 changed files with 55 additions and 10 deletions

View File

@ -322,3 +322,46 @@ def test_notifications_target_url(pub, http_requests):
'summary': 'xxx',
'name_ids': ['xxx'],
}
def test_notifications_no_body(pub, http_requests):
pub.substitutions.feed(pub)
pub.user_class.wipe()
user = pub.user_class()
user.name_identifiers = ['xxx']
user.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'baz'
formdef.fields = []
formdef.store()
formdef.data_class().wipe()
formdata = formdef.data_class()()
formdata.user_id = user.id
formdata.just_created()
formdata.store()
pub.load_site_options()
if not pub.site_options.has_section('variables'):
pub.site_options.add_section('variables')
pub.site_options.set('variables', 'portal_url', 'https://portal/')
item = SendNotificationWorkflowStatusItem()
item.title = 'xxx'
item.body = None
http_requests.empty()
item.perform(formdata)
assert http_requests.count() == 1
assert http_requests.get_last('url') == 'https://portal/api/notification/add/'
assert json.loads(http_requests.get_last('body')) == {
'body': None,
'url': f'http://example.net/baz/{formdata.id}/',
'id': f'formdata:{formdata.get_display_id()}',
'origin': '',
'summary': 'xxx',
'name_ids': ['xxx'],
}

View File

@ -158,7 +158,7 @@ class SendNotificationWorkflowStatusItem(WebserviceCallStatusItem):
yield self.users_template
def perform(self, formdata, ignore_i18n=False):
if not (self.is_available() and (self.to or self.users_template) and self.title and self.body):
if not (self.is_available() and (self.to or self.users_template) and self.title):
return
if get_publisher().has_i18n_enabled() and not ignore_i18n:
@ -181,23 +181,25 @@ class SendNotificationWorkflowStatusItem(WebserviceCallStatusItem):
return
title = get_publisher().translate(self.title)
body = get_publisher().translate(self.body)
try:
title = template_on_formdata(formdata, self.compute(title, render=False), autoescape=False)
except TemplateError as e:
get_publisher().record_error(
_('error in template for notification title, mail could not be generated'), exception=e
_('error in template for title, notification could not be generated'), exception=e
Review

En bonus un commit qui corrige le texte des erreurs pour ne plus parler de mail.

En bonus un commit qui corrige le texte des erreurs pour ne plus parler de mail.
)
return
try:
body = template_on_formdata(formdata, self.compute(body, render=False), autoescape=False)
except TemplateError as e:
get_publisher().record_error(
_('error in template for notification body, mail could not be generated'), exception=e
)
return
body = self.body
if body:
body = get_publisher().translate(self.body)
try:
body = template_on_formdata(formdata, self.compute(body, render=False), autoescape=False)
except TemplateError as e:
get_publisher().record_error(
_('error in template for body, notification could not be generated'), exception=e
)
return
users = []
if self.to: