2020-12-09 17:11:46 +00:00
|
|
|
import re
|
|
|
|
|
2020-06-17 23:00:16 +00:00
|
|
|
import markdown
|
2020-06-17 00:42:36 +00:00
|
|
|
from jinja2 import Template
|
|
|
|
|
2020-06-17 14:53:02 +00:00
|
|
|
from crc import app
|
2020-06-17 00:42:36 +00:00
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc.scripts.script import Script
|
|
|
|
from crc.services.ldap_service import LdapService
|
|
|
|
from crc.services.mails import send_mail
|
|
|
|
|
|
|
|
|
|
|
|
class Email(Script):
|
|
|
|
"""This Script allows to be introduced as part of a workflow and called from there, specifying
|
|
|
|
recipients and content """
|
|
|
|
|
|
|
|
def get_description(self):
|
|
|
|
return """
|
|
|
|
Creates an email, using the provided arguments (a list of UIDs)"
|
|
|
|
Each argument will be used to look up personal information needed for
|
|
|
|
the email creation.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
Email Subject ApprvlApprvr1 PIComputingID
|
|
|
|
"""
|
|
|
|
|
|
|
|
def do_task_validate_only(self, task, *args, **kwargs):
|
2020-06-17 22:09:38 +00:00
|
|
|
self.get_subject(task, args)
|
2020-12-09 17:11:46 +00:00
|
|
|
self.get_email_recipients(task, args)
|
2020-06-25 04:43:44 +00:00
|
|
|
self.get_content(task)
|
2020-06-17 00:42:36 +00:00
|
|
|
|
|
|
|
def do_task(self, task, *args, **kwargs):
|
2020-12-09 17:11:46 +00:00
|
|
|
args = [arg for arg in args if type(arg) == str or type(arg) == list]
|
|
|
|
|
|
|
|
subject = args[0]
|
|
|
|
recipients = None
|
|
|
|
try:
|
|
|
|
recipients = self.get_email_recipients(task, args)
|
|
|
|
except ApiError:
|
|
|
|
raise
|
2020-06-25 04:43:44 +00:00
|
|
|
content, content_html = self.get_content(task)
|
2020-06-17 00:42:36 +00:00
|
|
|
if recipients:
|
|
|
|
send_mail(
|
2020-06-17 14:53:02 +00:00
|
|
|
subject=subject,
|
|
|
|
sender=app.config['DEFAULT_SENDER'],
|
2020-06-17 00:42:36 +00:00
|
|
|
recipients=recipients,
|
|
|
|
content=content,
|
2020-06-17 23:00:16 +00:00
|
|
|
content_html=content_html
|
2020-06-17 00:42:36 +00:00
|
|
|
)
|
|
|
|
|
2020-12-09 17:11:46 +00:00
|
|
|
def check_valid_email(self, email):
|
|
|
|
# regex from https://emailregex.com/
|
|
|
|
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
|
|
|
|
if (re.search(regex, email)):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2020-12-07 21:23:41 +00:00
|
|
|
def get_email_recipients(self, task, args):
|
|
|
|
emails = []
|
|
|
|
|
|
|
|
if len(args[1]) < 1:
|
|
|
|
raise ApiError(code="missing_argument",
|
2020-12-09 17:11:46 +00:00
|
|
|
message="Email script requires at least one email address as an argument. "
|
2020-12-07 21:23:41 +00:00
|
|
|
"Multiple email addresses are accepted.")
|
2020-12-09 17:11:46 +00:00
|
|
|
|
2020-12-07 21:23:41 +00:00
|
|
|
if isinstance(args[1], str):
|
2020-12-09 17:11:46 +00:00
|
|
|
if self.check_valid_email(args[1]):
|
|
|
|
emails.append(args[1])
|
2020-12-07 21:23:41 +00:00
|
|
|
else:
|
2020-06-17 00:42:36 +00:00
|
|
|
raise ApiError(code="invalid_argument",
|
2020-12-09 17:11:46 +00:00
|
|
|
message="The email address you provided could not be parsed. "
|
|
|
|
"The value you provided is '%s" % args[1])
|
|
|
|
|
|
|
|
if isinstance(args[1], list):
|
|
|
|
for address in args[1]:
|
|
|
|
if self.check_valid_email(address):
|
|
|
|
emails.append(address)
|
|
|
|
else:
|
|
|
|
raise ApiError(code="invalid_argument",
|
|
|
|
message="The email address you provided could not be parsed. "
|
|
|
|
"The value you provided is '%s" % address)
|
|
|
|
|
|
|
|
if len(emails) > 0:
|
|
|
|
return emails
|
|
|
|
else:
|
|
|
|
raise ApiError(code="invalid_argument",
|
|
|
|
message="Email script requires a valid email address.")
|
|
|
|
|
|
|
|
# def get_users_info(self, task, args):
|
|
|
|
# if len(args) < 1:
|
|
|
|
# raise ApiError(code="missing_argument",
|
|
|
|
# message="Email script requires at least one argument. The "
|
|
|
|
# "name of the variable in the task data that contains user"
|
|
|
|
# "id to process. Multiple arguments are accepted.")
|
|
|
|
# emails = []
|
|
|
|
# for arg in args:
|
|
|
|
# try:
|
|
|
|
# uid = task.workflow.script_engine.evaluate_expression(task, arg)
|
|
|
|
# except Exception as e:
|
|
|
|
# app.logger.error(f'Workflow engines could not parse {arg}', exc_info=True)
|
|
|
|
# continue
|
|
|
|
# user_info = LdapService.user_info(uid)
|
|
|
|
# email = user_info.email_address
|
|
|
|
# emails.append(user_info.email_address)
|
|
|
|
# if not isinstance(email, str):
|
|
|
|
# raise ApiError(code="invalid_argument",
|
|
|
|
# message="The Email script requires at least 1 UID argument. The "
|
|
|
|
# "name of the variable in the task data that contains subject and"
|
|
|
|
# " user ids to process. This must point to an array or a string, but "
|
|
|
|
# "it currently points to a %s " % emails.__class__.__name__)
|
|
|
|
#
|
|
|
|
# return emails
|
2020-06-17 00:42:36 +00:00
|
|
|
|
|
|
|
def get_subject(self, task, args):
|
2020-12-09 17:11:46 +00:00
|
|
|
# subject = ''
|
|
|
|
if len(args[0]) < 1:
|
2020-06-17 00:42:36 +00:00
|
|
|
raise ApiError(code="missing_argument",
|
2020-12-09 17:11:46 +00:00
|
|
|
message="No subject was provided for the email message.")
|
|
|
|
|
2020-06-25 18:01:24 +00:00
|
|
|
subject = args[0]
|
2020-12-09 17:11:46 +00:00
|
|
|
if not subject or not isinstance(subject, str):
|
2020-06-18 03:11:47 +00:00
|
|
|
raise ApiError(code="invalid_argument",
|
2020-12-09 17:11:46 +00:00
|
|
|
message="The subject you provided could not be parsed. "
|
|
|
|
"The value is \"%s\" " % subject)
|
2020-06-17 00:42:36 +00:00
|
|
|
|
2020-06-18 18:53:50 +00:00
|
|
|
return subject
|
2020-06-17 00:42:36 +00:00
|
|
|
|
2020-06-25 04:43:44 +00:00
|
|
|
def get_content(self, task):
|
2020-06-17 00:42:36 +00:00
|
|
|
content = task.task_spec.documentation
|
|
|
|
template = Template(content)
|
2020-06-25 04:43:44 +00:00
|
|
|
rendered = template.render(task.data)
|
2020-06-17 23:00:16 +00:00
|
|
|
rendered_markdown = markdown.markdown(rendered).replace('\n', '<br>')
|
|
|
|
return rendered, rendered_markdown
|