171 lines
5.7 KiB
Python
171 lines
5.7 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2019 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import urllib.parse
|
|
|
|
from quixote import get_publisher
|
|
|
|
from wcs.workflows import WorkflowStatusItem, register_item_class, template_on_formdata
|
|
|
|
from ..qommon import N_, _, get_logger
|
|
from ..qommon.form import ComputedExpressionWidget, SingleSelectWidget, StringWidget, TextWidget, WidgetList
|
|
from ..qommon.template import TemplateError
|
|
from .wscall import WebserviceCallStatusItem
|
|
|
|
|
|
class SendNotificationWorkflowStatusItem(WebserviceCallStatusItem):
|
|
description = N_('User Notification')
|
|
key = 'notification'
|
|
category = 'interaction'
|
|
support_substitution_variables = True
|
|
|
|
# parameters
|
|
to = ['_submitter']
|
|
title = None
|
|
body = None
|
|
origin = None
|
|
|
|
# webservice parameters
|
|
varname = 'notification'
|
|
post = False
|
|
_method = 'POST'
|
|
response_type = 'json'
|
|
|
|
action_on_app_error = ':pass'
|
|
action_on_4xx = ':pass'
|
|
action_on_5xx = ':pass'
|
|
action_on_bad_data = ':pass'
|
|
action_on_network_errors = ':pass'
|
|
notify_on_errors = True
|
|
record_errors = False
|
|
|
|
@classmethod
|
|
def is_available(cls, workflow=None):
|
|
return bool(cls.get_api_url() is not None)
|
|
|
|
@classmethod
|
|
def get_api_url(cls):
|
|
for variable_name in ('_interco_portal_url', 'portal_url'):
|
|
url = get_publisher().get_site_option(variable_name, 'variables')
|
|
if url:
|
|
return urllib.parse.urljoin(url, '/api/notification/add/')
|
|
return None
|
|
|
|
def get_jump_label(self, target_id):
|
|
return _(self.description)
|
|
|
|
def get_parameters(self):
|
|
return ('title', 'body', 'origin', 'condition')
|
|
|
|
def add_parameters_widgets(self, form, parameters, prefix='', formdef=None, **kwargs):
|
|
if 'to' in parameters:
|
|
# never displayed in the current UI (no 'to' in get_parameters)
|
|
form.add(
|
|
WidgetList,
|
|
'%sto' % prefix,
|
|
title=_('To'),
|
|
element_type=SingleSelectWidget,
|
|
value=self.to,
|
|
add_element_label=self.get_add_role_label(),
|
|
element_kwargs={
|
|
'render_br': False,
|
|
'options': [(None, '---', None)] + self.get_list_of_roles(include_logged_in_users=False),
|
|
},
|
|
)
|
|
if 'title' in parameters:
|
|
form.add(
|
|
StringWidget,
|
|
'%stitle' % prefix,
|
|
title=_('Title'),
|
|
value=self.title,
|
|
size=80,
|
|
validation_function=ComputedExpressionWidget.validate_template,
|
|
)
|
|
if 'body' in parameters:
|
|
form.add(
|
|
TextWidget,
|
|
'%sbody' % prefix,
|
|
title=_('Body'),
|
|
value=self.body,
|
|
cols=80,
|
|
rows=5,
|
|
validation_function=ComputedExpressionWidget.validate_template,
|
|
)
|
|
if 'origin' in parameters:
|
|
form.add(
|
|
StringWidget,
|
|
'%sorigin' % prefix,
|
|
title=_('Origin'),
|
|
value=self.origin,
|
|
required=False,
|
|
advanced=not (self.origin),
|
|
)
|
|
WorkflowStatusItem.add_parameters_widgets(
|
|
self, form, parameters, prefix=prefix, formdef=formdef, **kwargs
|
|
)
|
|
|
|
def perform(self, formdata):
|
|
if not (self.is_available() and self.to and self.title and self.body):
|
|
return
|
|
|
|
try:
|
|
title = template_on_formdata(formdata, self.compute(self.title, render=False), autoescape=False)
|
|
except TemplateError as e:
|
|
get_logger().error(
|
|
'error in template for notification title, ' 'mail could not be generated: %s' % str(e)
|
|
)
|
|
return
|
|
|
|
try:
|
|
body = template_on_formdata(formdata, self.compute(self.body, render=False), autoescape=False)
|
|
except TemplateError as e:
|
|
get_logger().error(
|
|
'error in template for notification body, ' 'mail could not be generated: %s' % str(e)
|
|
)
|
|
return
|
|
|
|
self.post_data = {
|
|
'summary': title,
|
|
'body': body,
|
|
'url': formdata.get_url(),
|
|
'origin': self.origin or '',
|
|
'id': 'formdata:%s' % formdata.get_display_id(),
|
|
}
|
|
self.url = self.get_api_url()
|
|
|
|
users = []
|
|
for dest in self.to:
|
|
if dest == '_submitter':
|
|
users.append(formdata.get_user())
|
|
continue
|
|
|
|
for dest_id in formdata.get_function_roles(dest):
|
|
try:
|
|
role = get_publisher().role_class.get(dest_id)
|
|
except KeyError:
|
|
continue
|
|
users.extend(get_publisher().user_class.get_users_with_role(role.id))
|
|
|
|
for user in users:
|
|
if not user or not user.is_active:
|
|
continue
|
|
for name_id in user.name_identifiers or []:
|
|
self.qs_data = {'NameID': name_id}
|
|
super().perform(formdata)
|
|
|
|
|
|
register_item_class(SendNotificationWorkflowStatusItem)
|