More enhancements

This commit is contained in:
Carlos Lopez 2020-06-12 12:17:08 -06:00
parent e9e805b2c9
commit 5f13b96079
3 changed files with 138 additions and 95 deletions

View File

@ -23,131 +23,113 @@ def send_test_email(sender, recipients):
except Exception as e:
return str(e)
def send_ramp_up_submission_email(sender, recipients, approval_id, approver_1, approver_2=None):
def send_mail(subject, sender, recipients, content, content_html):
from crc import mail
try:
subject = 'Research Ramp-up Plan Submitted'
msg = Message(subject,
sender=sender,
recipients=recipients,
bcc=['rrt_emails@googlegroups.com'])
from crc import env, mail
msg.body = content
msg.html = content_html
mail.send(msg)
except Exception as e:
return str(e)
def send_ramp_up_submission_email(sender, recipients, approval_id, approver_1, approver_2=None):
from crc import env
subject = 'Research Ramp-up Plan Submitted'
template = env.get_template('ramp_up_submission.txt')
template_vars = {'approver_1': approver_1, 'approver_2': approver_2}
msg.body = template.render(template_vars)
content = template.render(template_vars)
template = env.get_template('ramp_up_submission.html')
msg.html = template.render(template_vars)
content_html = template.render(template_vars)
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=msg.body, content_html=msg.html, approval_id=approval_id)
content=content, content_html=content_html, approval_id=approval_id)
mail.send(msg)
except Exception as e:
return str(e)
result = send_mail(subject, sender, recipients, content, content_html)
return result
def send_ramp_up_approval_request_email(sender, recipients, approval_id, primary_investigator):
try:
from crc import env
subject = 'Research Ramp-up Plan Approval Request'
msg = Message(subject,
sender=sender,
recipients=recipients,
bcc=['rrt_emails@googlegroups.com'])
from crc import env, mail
template = env.get_template('ramp_up_approval_request.txt')
template_vars = {'primary_investigator': primary_investigator}
msg.body = template.render(template_vars)
content = template.render(template_vars)
template = env.get_template('ramp_up_approval_request.html')
msg.html = template.render(template_vars)
content_html = template.render(template_vars)
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=msg.body, content_html=msg.html, approval_id=approval_id)
content=content, content_html=content_html, approval_id=approval_id)
mail.send(msg)
except Exception as e:
return str(e)
result = send_mail(subject, sender, recipients, content, content_html)
return result
def send_ramp_up_approval_request_first_review_email(sender, recipients, approval_id, primary_investigator):
try:
from crc import env
subject = 'Research Ramp-up Plan Approval Request'
msg = Message(subject,
sender=sender,
recipients=recipients,
bcc=['rrt_emails@googlegroups.com'])
from crc import env, mail
template = env.get_template('ramp_up_approval_request_first_review.txt')
template_vars = {'primary_investigator': primary_investigator}
msg.body = template.render(template_vars)
content = template.render(template_vars)
template = env.get_template('ramp_up_approval_request_first_review.html')
msg.html = template.render(template_vars)
content_html = template.render(template_vars)
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=msg.body, content_html=msg.html, approval_id=approval_id)
content=content, content_html=content_html, approval_id=approval_id)
mail.send(msg)
except Exception as e:
return str(e)
result = send_mail(subject, sender, recipients, content, content_html)
return result
def send_ramp_up_approved_email(sender, recipients, approval_id, approver_1, approver_2=None):
try:
from crc import env
subject = 'Research Ramp-up Plan Approved'
msg = Message(subject,
sender=sender,
recipients=recipients,
bcc=['rrt_emails@googlegroups.com'])
from crc import env, mail
template = env.get_template('ramp_up_approved.txt')
template_vars = {'approver_1': approver_1, 'approver_2': approver_2}
msg.body = template.render(template_vars)
content = template.render(template_vars)
template = env.get_template('ramp_up_approved.html')
msg.html = template.render(template_vars)
content_html = template.render(template_vars)
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=msg.body, content_html=msg.html, approval_id=approval_id)
content=content, content_html=content_html, approval_id=approval_id)
mail.send(msg)
except Exception as e:
return str(e)
result = send_mail(subject, sender, recipients, content, content_html)
return result
def send_ramp_up_denied_email(sender, recipients, approval_id, approver):
try:
from crc import env
subject = 'Research Ramp-up Plan Denied'
msg = Message(subject,
sender=sender,
recipients=recipients,
bcc=['rrt_emails@googlegroups.com'])
from crc import env, mail
template = env.get_template('ramp_up_denied.txt')
template_vars = {'approver': approver}
msg.body = template.render(template_vars)
content = template.render(template_vars)
template = env.get_template('ramp_up_denied.html')
msg.html = template.render(template_vars)
content_html = template.render(template_vars)
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=msg.body, content_html=msg.html, approval_id=approval_id)
content=content, content_html=content_html, approval_id=approval_id)
mail.send(msg)
except Exception as e:
return str(e)
result = send_mail(subject, sender, recipients, content, content_html)
return result
def send_ramp_up_denied_email_to_approver(sender, recipients, approval_id, primary_investigator, approver_2):
try:
from crc import env
subject = 'Research Ramp-up Plan Denied'
msg = Message(subject,
sender=sender,
recipients=recipients,
bcc=['rrt_emails@googlegroups.com'])
from crc import env, mail
template = env.get_template('ramp_up_denied_first_approver.txt')
template_vars = {'primary_investigator': primary_investigator, 'approver_2': approver_2}
msg.body = template.render(template_vars)
content = template.render(template_vars)
template = env.get_template('ramp_up_denied_first_approver.html')
msg.html = template.render(template_vars)
content_html = template.render(template_vars)
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=msg.body, content_html=msg.html, approval_id=approval_id)
content=content, content_html=content_html, approval_id=approval_id)
mail.send(msg)
except Exception as e:
return str(e)
result = send_mail(subject, sender, recipients, content, content_html)
return result

View File

@ -0,0 +1,42 @@
from tests.base_test import BaseTest
from crc import session
from crc.models.approval import ApprovalModel, ApprovalStatus
from crc.models.email import EmailModel
from crc.services.email_service import EmailService
class TestEmailService(BaseTest):
def test_add_email(self):
self.load_example_data()
study = self.create_study()
workflow = self.create_workflow('random_fact')
approval = ApprovalModel(
study=study,
workflow=workflow,
approver_uid='lb3dp',
status=ApprovalStatus.PENDING.value,
version=1
)
session.add(approval)
session.commit()
subject = 'Email Subject'
sender = 'sender@sartography.com'
recipients = ['recipient@sartography.com', 'back@sartography.com']
content = 'Content for this email'
content_html = '<p>Hypertext Markup Language content for this email</p>'
EmailService.add_email(subject=subject, sender=sender, recipients=recipients,
content=content, content_html=content_html, approval_id=approval.id)
email_model = EmailModel.query.first()
self.assertEqual(email_model.subject, subject)
self.assertEqual(email_model.sender, sender)
self.assertEqual(email_model.recipients, str(recipients))
self.assertEqual(email_model.content, content)
self.assertEqual(email_model.content_html, content_html)
self.assertEqual(email_model.approval, approval)

View File

@ -3,6 +3,7 @@ from tests.base_test import BaseTest
from crc import mail, session
from crc.models.approval import ApprovalModel, ApprovalStatus
from crc.models.email import EmailModel
from crc.services.mails import (
send_ramp_up_submission_email,
send_ramp_up_approval_request_email,
@ -54,6 +55,9 @@ class TestMails(BaseTest):
self.assertIn(self.approver_2, outbox[1].body)
self.assertIn(self.approver_2, outbox[1].html)
db_emails = EmailModel.query.count()
self.assertEqual(db_emails, 2)
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,
@ -64,6 +68,9 @@ class TestMails(BaseTest):
self.assertIn(self.primary_investigator, outbox[0].body)
self.assertIn(self.primary_investigator, outbox[0].html)
db_emails = EmailModel.query.count()
self.assertEqual(db_emails, 1)
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(
@ -75,6 +82,9 @@ class TestMails(BaseTest):
self.assertIn(self.primary_investigator, outbox[0].body)
self.assertIn(self.primary_investigator, outbox[0].html)
db_emails = EmailModel.query.count()
self.assertEqual(db_emails, 1)
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)
@ -91,6 +101,9 @@ class TestMails(BaseTest):
self.assertIn(self.approver_2, outbox[1].body)
self.assertIn(self.approver_2, outbox[1].html)
db_emails = EmailModel.query.count()
self.assertEqual(db_emails, 2)
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)
@ -98,6 +111,9 @@ class TestMails(BaseTest):
self.assertIn(self.approver_1, outbox[0].body)
self.assertIn(self.approver_1, outbox[0].html)
db_emails = EmailModel.query.count()
self.assertEqual(db_emails, 1)
def test_send_send_ramp_up_denied_email_to_approver(self):
with mail.record_messages() as outbox:
send_ramp_up_denied_email_to_approver(
@ -109,3 +125,6 @@ class TestMails(BaseTest):
self.assertIn(self.primary_investigator, outbox[0].html)
self.assertIn(self.approver_2, outbox[0].body)
self.assertIn(self.approver_2, outbox[0].html)
db_emails = EmailModel.query.count()
self.assertEqual(db_emails, 1)