Slight refactor on data passed to template rendering

This commit is contained in:
Carlos Lopez 2020-06-24 22:43:44 -06:00
parent a0d877e02f
commit 5d1ae402b6
3 changed files with 14 additions and 31 deletions

View File

@ -25,13 +25,13 @@ Email Subject ApprvlApprvr1 PIComputingID
def do_task_validate_only(self, task, *args, **kwargs):
self.get_subject(task, args)
self.get_users_info(task, args)
self.get_content(task, {})
self.get_content(task)
def do_task(self, task, *args, **kwargs):
args = [arg for arg in args if type(arg) == str]
subject = self.get_subject(task, args)
recipients, display_keys = self.get_users_info(task, args)
content, content_html = self.get_content(task, display_keys)
recipients = self.get_users_info(task, args)
content, content_html = self.get_content(task)
if recipients:
send_mail(
subject=subject,
@ -48,7 +48,6 @@ Email Subject ApprvlApprvr1 PIComputingID
"name of the variable in the task data that contains user"
"id to process. Multiple arguments are accepted.")
emails = []
display_keys = {}
for arg in args:
try:
uid = task.workflow.script_engine.evaluate_expression(task, arg)
@ -59,7 +58,6 @@ Email Subject ApprvlApprvr1 PIComputingID
user_info = LdapService.user_info(uid)
email = user_info.email_address
emails.append(user_info.email_address)
display_keys[arg] = user_info.proper_name()
if not isinstance(email, str):
raise ApiError(code="invalid_argument",
message="The Email script requires at least 1 UID argument. The "
@ -67,7 +65,7 @@ Email Subject ApprvlApprvr1 PIComputingID
" 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, display_keys
return emails
def get_subject(self, task, args):
if len(args) < 1:
@ -97,9 +95,9 @@ Email Subject ApprvlApprvr1 PIComputingID
return subject
def get_content(self, task, display_keys):
def get_content(self, task):
content = task.task_spec.documentation
template = Template(content)
rendered = template.render(display_keys)
rendered = template.render(task.data)
rendered_markdown = markdown.markdown(rendered).replace('\n', '<br>')
return rendered, rendered_markdown

View File

@ -231,7 +231,6 @@ class ApprovalService(object):
mail_result = send_ramp_up_submission_email(
'askresearch@virginia.edu',
[pi_user_info.email_address],
model.id,
f'{approver_info.display_name} - ({approver_info.uid})'
)
if mail_result:
@ -242,7 +241,6 @@ class ApprovalService(object):
mail_result = send_ramp_up_approval_request_first_review_email(
'askresearch@virginia.edu',
approver_email,
model.id,
f'{pi_user_info.display_name} - ({pi_user_info.uid})'
)
if mail_result:

View File

@ -22,16 +22,6 @@ class TestMails(BaseTest):
self.study = self.create_study()
self.workflow = self.create_workflow('random_fact')
self.approval = ApprovalModel(
study=self.study,
workflow=self.workflow,
approver_uid='lb3dp',
status=ApprovalStatus.PENDING.value,
version=1
)
session.add(self.approval)
session.commit()
self.sender = 'sender@sartography.com'
self.recipients = ['recipient@sartography.com']
self.primary_investigator = 'Dr. Bartlett'
@ -41,14 +31,13 @@ class TestMails(BaseTest):
def test_send_ramp_up_submission_email(self):
with mail.record_messages() as outbox:
send_ramp_up_submission_email(self.sender, self.recipients, self.approval.id, self.approver_1)
send_ramp_up_submission_email(self.sender, self.recipients, self.approver_1)
self.assertEqual(len(outbox), 1)
self.assertEqual(outbox[0].subject, 'Research Ramp-up Plan Submitted')
self.assertIn(self.approver_1, outbox[0].body)
self.assertIn(self.approver_1, outbox[0].html)
send_ramp_up_submission_email(self.sender, self.recipients, self.approval.id,
self.approver_1, self.approver_2)
send_ramp_up_submission_email(self.sender, self.recipients, self.approver_1, self.approver_2)
self.assertEqual(len(outbox), 2)
self.assertIn(self.approver_1, outbox[1].body)
self.assertIn(self.approver_1, outbox[1].html)
@ -60,8 +49,7 @@ class TestMails(BaseTest):
def test_send_ramp_up_approval_request_email(self):
with mail.record_messages() as outbox:
send_ramp_up_approval_request_email(self.sender, self.recipients, self.approval.id,
self.primary_investigator)
send_ramp_up_approval_request_email(self.sender, self.recipients, self.primary_investigator)
self.assertEqual(len(outbox), 1)
self.assertEqual(outbox[0].subject, 'Research Ramp-up Plan Approval Request')
@ -74,7 +62,7 @@ class TestMails(BaseTest):
def test_send_ramp_up_approval_request_first_review_email(self):
with mail.record_messages() as outbox:
send_ramp_up_approval_request_first_review_email(
self.sender, self.recipients, self.approval.id, self.primary_investigator
self.sender, self.recipients, self.primary_investigator
)
self.assertEqual(len(outbox), 1)
@ -87,14 +75,13 @@ class TestMails(BaseTest):
def test_send_ramp_up_approved_email(self):
with mail.record_messages() as outbox:
send_ramp_up_approved_email(self.sender, self.recipients, self.approval.id, self.approver_1)
send_ramp_up_approved_email(self.sender, self.recipients, self.approver_1)
self.assertEqual(len(outbox), 1)
self.assertEqual(outbox[0].subject, 'Research Ramp-up Plan Approved')
self.assertIn(self.approver_1, outbox[0].body)
self.assertIn(self.approver_1, outbox[0].html)
send_ramp_up_approved_email(self.sender, self.recipients, self.approval.id,
self.approver_1, self.approver_2)
send_ramp_up_approved_email(self.sender, self.recipients, self.approver_1, self.approver_2)
self.assertEqual(len(outbox), 2)
self.assertIn(self.approver_1, outbox[1].body)
self.assertIn(self.approver_1, outbox[1].html)
@ -106,7 +93,7 @@ class TestMails(BaseTest):
def test_send_ramp_up_denied_email(self):
with mail.record_messages() as outbox:
send_ramp_up_denied_email(self.sender, self.recipients, self.approval.id, self.approver_1)
send_ramp_up_denied_email(self.sender, self.recipients, self.approver_1)
self.assertEqual(outbox[0].subject, 'Research Ramp-up Plan Denied')
self.assertIn(self.approver_1, outbox[0].body)
self.assertIn(self.approver_1, outbox[0].html)
@ -117,7 +104,7 @@ class TestMails(BaseTest):
def test_send_send_ramp_up_denied_email_to_approver(self):
with mail.record_messages() as outbox:
send_ramp_up_denied_email_to_approver(
self.sender, self.recipients, self.approval.id, self.primary_investigator, self.approver_2
self.sender, self.recipients, self.primary_investigator, self.approver_2
)
self.assertEqual(outbox[0].subject, 'Research Ramp-up Plan Denied')