From d4a285883f016a9e1d86e1b090962c69700a8332 Mon Sep 17 00:00:00 2001 From: Carlos Lopez Date: Tue, 16 Jun 2020 18:42:36 -0600 Subject: [PATCH] Email script --- crc/scripts/email.py | 81 +++++++++++++++++++++++++++++++++++++ crc/scripts/fact_service.py | 2 +- tests/data/email/email.bpmn | 58 ++++++++++++++++++++++++++ tests/test_email_script.py | 30 ++++++++++++++ 4 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 crc/scripts/email.py create mode 100644 tests/data/email/email.bpmn create mode 100644 tests/test_email_script.py diff --git a/crc/scripts/email.py b/crc/scripts/email.py new file mode 100644 index 00000000..2958fb29 --- /dev/null +++ b/crc/scripts/email.py @@ -0,0 +1,81 @@ +from jinja2 import Template + +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): + self.get_emails(task, args) + + def do_task(self, task, *args, **kwargs): + subject = self.get_subject(task, args) + recipients = self.get_emails(task, args) + content = self.get_content(task) + if recipients: + send_mail( + subject='Test Subject', + sender='sender@sartography.com', + recipients=recipients, + content=content, + content_html=content + ) + + def get_emails(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[1:]: + uid = task.workflow.script_engine.evaluate_expression(task, arg) + 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 + + def get_subject(self, task, args): + if len(args) < 1: + raise ApiError(code="missing_argument", + message="Email script requires at least one subject argument. The " + "name of the variable in the task data that contains subject" + " to process. Multiple arguments are accepted.") + subject = task.workflow.script_engine.evaluate_expression(task, args[0]) + if not isinstance(subject, str): + raise ApiError(code="invalid_argument", + message="The Email script requires 1 argument. The " + "the name of the variable in the task data that contains user" + "ids to process. This must point to an array or a string, but " + "it currently points to a %s " % emails.__class__.__name__) + + return subject + + def get_content(self, task): + content = task.task_spec.documentation + template = Template(content) + rendered = template.render({'approver': 'Bossman', 'not_here': 22}) + + return rendered diff --git a/crc/scripts/fact_service.py b/crc/scripts/fact_service.py index c4468721..b3701312 100644 --- a/crc/scripts/fact_service.py +++ b/crc/scripts/fact_service.py @@ -5,7 +5,7 @@ from crc.scripts.script import Script class FactService(Script): def get_description(self): - return """Just your basic class that can pull in data from a few api endpoints and + return """Just your basic class that can pull in data from a few api endpoints and do a basic task.""" def get_cat(self): diff --git a/tests/data/email/email.bpmn b/tests/data/email/email.bpmn new file mode 100644 index 00000000..b2221f24 --- /dev/null +++ b/tests/data/email/email.bpmn @@ -0,0 +1,58 @@ + + + + + Flow_1synsig + + + Flow_1xlrgne + + + Email content to be delivered to {{ approver }} + Flow_08n2npe + Flow_1xlrgne + Email Subject ApprvlApprvr1 PIComputingID + + + + + + + + + + + + Flow_1synsig + Flow_08n2npe + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/test_email_script.py b/tests/test_email_script.py new file mode 100644 index 00000000..9ac93e07 --- /dev/null +++ b/tests/test_email_script.py @@ -0,0 +1,30 @@ +from tests.base_test import BaseTest + +from crc.services.file_service import FileService +from crc.scripts.email import Email +from crc.services.workflow_processor import WorkflowProcessor +from crc.api.common import ApiError + +from crc import db +# from crc.models.approval import ApprovalModel + + +class TestEmailScript(BaseTest): + + def test_do_task(self): + self.load_example_data() + self.create_reference_document() + workflow = self.create_workflow('email') + processor = WorkflowProcessor(workflow) + task = processor.next_task() + processor.complete_task(task) + task = processor.next_task() + task.data = { + 'PIComputingID': 'dhf8r', + 'ApprvlApprvr1': 'lb3dp', + 'Subject': 'Email Script needs your help' + } + + script = Email() + script.do_task(task, 'Subject', 'PIComputingID', 'ApprvlApprvr1') + self.assertTrue(True)