Hooking up emails into process - start

This commit is contained in:
Carlos Lopez 2020-06-04 20:37:28 -06:00
parent 8c36d9f367
commit 4727d87adb
10 changed files with 126 additions and 90 deletions

View File

@ -44,4 +44,5 @@ PB_STUDY_DETAILS_URL = environ.get('PB_STUDY_DETAILS_URL', default=PB_BASE_URL +
LDAP_URL = environ.get('LDAP_URL', default="ldap.virginia.edu").strip('/') # No trailing slash or http://
LDAP_TIMEOUT_SEC = int(environ.get('LDAP_TIMEOUT_SEC', default=3))
# Fallback emails
FALLBACK_EMAILS = ['askresearch@virginia.edu', 'sartographysupport@googlegroups.com']

View File

@ -6,6 +6,7 @@ import connexion
from jinja2 import Environment, FileSystemLoader
from flask_cors import CORS
from flask_marshmallow import Marshmallow
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sentry_sdk.integrations.flask import FlaskIntegration
@ -52,6 +53,14 @@ if app.config['ENABLE_SENTRY']:
# Jinja environment definition, used to render mail templates
template_dir = os.getcwd() + '/crc/static/templates/mails'
env = Environment(loader=FileSystemLoader(template_dir))
# Mail settings
app.config['MAIL_SERVER']='smtp.mailtrap.io'
app.config['MAIL_PORT'] = 2525
app.config['MAIL_USERNAME'] = '5f012d0108d374'
app.config['MAIL_PASSWORD'] = '08442c04e98d50'
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
mail = Mail(app)
print('=== USING THESE CONFIG SETTINGS: ===')
print('DB_HOST = ', )

View File

@ -2,13 +2,21 @@ from datetime import datetime
from sqlalchemy import desc
from crc import db, session
from crc import app, db, session
from crc.api.common import ApiError
from crc.models.approval import ApprovalModel, ApprovalStatus, ApprovalFile, Approval
from crc.models.study import StudyModel
from crc.models.workflow import WorkflowModel
from crc.services.file_service import FileService
from crc.services.ldap_service import LdapService
from crc.services.mails import (
send_ramp_up_submission_email,
# send_ramp_up_approval_request_email,
send_ramp_up_approval_request_first_review_email,
# send_ramp_up_approved_email,
# send_ramp_up_denied_email
)
class ApprovalService(object):
@ -97,8 +105,8 @@ class ApprovalService(object):
if first_approval:
# Second approver denies
# send rrp denied by second approver email to first approver
pass
# send rrp denied email
pass
# TODO: Log update action by approver_uid - maybe ?
return db_approval
@ -151,18 +159,32 @@ class ApprovalService(object):
# Check approvals count
approvals_count = ApprovalModel().query.filter_by(study_id=study_id, workflow_id=workflow_id,
status=ApprovalStatus.PENDING.value,
version=version).count()
# Send first email
if approvals_count == 0:
# send rrp submission
# send rrp approval request for first approver
pass
db.session.add(model)
db.session.add_all(approval_files)
db.session.commit()
# Send first email
if approvals_count == 0:
ldap_service = LdapService()
pi_user_info = ldap_service.user_info(model.study.primary_investigator_id)
approver_info = ldap_service.user_info(approver_uid)
# send rrp submission
send_ramp_up_submission_email(
'askresearch@virginia.edu',
[pi_user_info.email_address],
f'{approver_info.display_name} - ({approver_info.uid})'
)
# send rrp approval request for first approver
# enhance the second part in case it bombs
approver_email = [approver_info.email_address] if approver_info.email_address else app.config['FALLBACK_EMAILS']
send_ramp_up_approval_request_first_review_email(
'askresearch@virginia.edu',
approver_email,
f'{pi_user_info.display_name} - ({pi_user_info.uid})'
)
@staticmethod
def _create_approval_files(workflow_data_files, approval):
"""Currently based exclusively on the status of files associated with a workflow."""

View File

@ -64,7 +64,7 @@ class LdapService(object):
# Search by user_id or last name
search_string = LdapService.user_or_last_name_search % (query, query)
results = []
print(search_string)
app.logger.info(search_string)
try:
self.conn.search(LdapService.search_base, search_string, attributes=LdapService.attributes)
# Entries are returned as a generator, accessing entries

View File

@ -1,99 +1,92 @@
import os
from crc import app, env
from jinja2 import Environment, FileSystemLoader
from flask import render_template, render_template_string
from flask_mail import Mail, Message
from flask_mail import Message
# TODO: Extract common mailing code into its own function
def send_ramp_up_submission_email(sender, recipients, approver_1, approver_2=None):
with app.app_context():
try:
msg = Message('Research Ramp-up Plan Submitted',
sender=sender,
recipients=recipients)
try:
msg = Message('Research Ramp-up Plan Submitted',
sender=sender,
recipients=recipients)
template = env.get_template('ramp_up_submission.txt')
template_vars = {'approver_1': approver_1, 'approver_2': approver_2}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_submission.html')
msg.html = template.render(template_vars)
from crc import env, mail
template = env.get_template('ramp_up_submission.txt')
template_vars = {'approver_1': approver_1, 'approver_2': approver_2}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_submission.html')
msg.html = template.render(template_vars)
mail = Mail(app)
mail.send(msg)
except Exception as e:
app.logger.error(str(e))
mail.send(msg)
except Exception as e:
return str(e)
def send_ramp_up_approval_request_email(sender, recipients, primary_investigator):
with app.app_context():
try:
msg = Message('Research Ramp-up Plan Approval Request',
sender=sender,
recipients=recipients)
try:
msg = Message('Research Ramp-up Plan Approval Request',
sender=sender,
recipients=recipients)
template = env.get_template('ramp_up_approval_request.txt')
template_vars = {'primary_investigator': primary_investigator}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_approval_request.html')
msg.html = template.render(template_vars)
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)
template = env.get_template('ramp_up_approval_request.html')
msg.html = template.render(template_vars)
mail = Mail(app)
mail.send(msg)
except Exception as e:
app.logger.error(str(e))
mail.send(msg)
except Exception as e:
return str(e)
def send_ramp_up_approval_request_first_review_email(sender, recipients, primary_investigator, approver):
with app.app_context():
try:
msg = Message('Research Ramp-up Plan Approval Request',
sender=sender,
recipients=recipients)
def send_ramp_up_approval_request_first_review_email(sender, recipients, primary_investigator):
try:
msg = Message('Research Ramp-up Plan Approval Request',
sender=sender,
recipients=recipients)
template = env.get_template('ramp_up_approval_request_first_review.txt')
template_vars = {'primary_investigator': primary_investigator, 'approver': approver}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_approval_request_first_review.html')
msg.html = template.render(template_vars)
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)
template = env.get_template('ramp_up_approval_request_first_review.html')
msg.html = template.render(template_vars)
mail = Mail(app)
mail.send(msg)
except Exception as e:
app.logger.error(str(e))
mail.send(msg)
except Exception as e:
return str(e)
def send_ramp_up_approved_email(sender, recipients, approver_1, approver_2=None):
with app.app_context():
try:
msg = Message('Research Ramp-up Plan Approved',
sender=sender,
recipients=recipients)
try:
msg = Message('Research Ramp-up Plan Approved',
sender=sender,
recipients=recipients)
template = env.get_template('ramp_up_approved.txt')
template_vars = {'approver_1': approver_1, 'approver_2': approver_2}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_approved.html')
msg.html = template.render(template_vars)
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)
template = env.get_template('ramp_up_approved.html')
msg.html = template.render(template_vars)
mail = Mail(app)
mail.send(msg)
except Exception as e:
app.logger.error(str(e))
mail.send(msg)
except Exception as e:
return str(e)
def send_ramp_up_denied_email(sender, recipients, approver):
with app.app_context():
try:
msg = Message('Research Ramp-up Plan Denied',
sender=sender,
recipients=recipients)
try:
msg = Message('Research Ramp-up Plan Denied',
sender=sender,
recipients=recipients)
template = env.get_template('ramp_up_denied.txt')
template_vars = {'approver': approver}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_denied.html')
msg.html = template.render(template_vars)
from crc import env, mail
template = env.get_template('ramp_up_denied.txt')
template_vars = {'approver': approver}
msg.body = template.render(template_vars)
template = env.get_template('ramp_up_denied.html')
msg.html = template.render(template_vars)
mail = Mail(app)
mail.send(msg)
except Exception as e:
app.logger.error(str(e))
mail.send(msg)
except Exception as e:
return str(e)

View File

@ -1 +1 @@
<p>A Research Ramp-up approval request from {{ primary_investigator }} has been approve by {{ approver }} and is now available for your review in your [Research Ramp-up Toolkit](https://rrt.uvadcos.io/app/approvals).</p>
<p>A Research Ramp-up approval request from {{ primary_investigator }} and is now available for your review in your [Research Ramp-up Toolkit](https://rrt.uvadcos.io/app/approvals).</p>

View File

@ -1 +1 @@
A Research Ramp-up approval request from {{ primary_investigator }} has been approve by {{ approver }} and is now available for your review in your [Research Ramp-up Toolkit](https://rrt.uvadcos.io/app/approvals).
A Research Ramp-up approval request from {{ primary_investigator }} is now available for your review in your [Research Ramp-up Toolkit](https://rrt.uvadcos.io/app/approvals).

View File

@ -119,7 +119,6 @@ class BaseTest(unittest.TestCase):
"""use_crc_data will cause this to load the mammoth collection of documents
we built up developing crc, use_rrt_data will do the same for hte rrt project,
otherwise it depends on a small setup for running tests."""
from example_data import ExampleDataLoader
ExampleDataLoader.clean_db()
if use_crc_data:
@ -228,7 +227,7 @@ class BaseTest(unittest.TestCase):
if study is None:
user = self.create_user(uid=uid)
study = StudyModel(title=title, protocol_builder_status=ProtocolBuilderStatus.ACTIVE,
user_uid=user.uid)
user_uid=user.uid, primary_investigator_id='lb3dp')
db.session.add(study)
db.session.commit()
return study

View File

@ -15,6 +15,7 @@ class TestApprovalsService(BaseTest):
name="anything.png", content_type="text",
binary_data=b'5678', irb_doc_code="UVACompl_PRCAppr" )
ApprovalService.add_approval(study_id=workflow.study_id, workflow_id=workflow.id, approver_uid="dhf8r")
self.assertEquals(1, db.session.query(ApprovalModel).count())
model = db.session.query(ApprovalModel).first()
@ -56,4 +57,15 @@ class TestApprovalsService(BaseTest):
self.assertEquals(1, models[0].version)
self.assertEquals(2, models[1].version)
def test_new_approval_sends_proper_emails(self):
self.assertEqual(1, 1)
def test_new_approval_failed_ldap_lookup(self):
# failed lookup should send email to sartographysupport@googlegroups.com + Cheryl
self.assertEqual(1, 1)
def test_approve_approval_sends_proper_emails(self):
self.assertEqual(1, 1)
def test_deny_approval_sends_proper_emails(self):
self.assertEqual(1, 1)

View File

@ -23,7 +23,7 @@ class TestMails(BaseTest):
send_ramp_up_submission_email(self.sender, self.recipients, self.approver_1)
self.assertTrue(True)
send_ramp_up_submission_email(self.sender, self.recipients, self.approver_1)
send_ramp_up_submission_email(self.sender, self.recipients, self.approver_1, self.approver_2)
self.assertTrue(True)
def test_send_ramp_up_approval_request_email(self):
@ -32,7 +32,7 @@ class TestMails(BaseTest):
def test_send_ramp_up_approval_request_first_review_email(self):
send_ramp_up_approval_request_first_review_email(
self.sender, self.recipients, self.primary_investigator, self.approver_1
self.sender, self.recipients, self.primary_investigator
)
self.assertTrue(True)