diff --git a/crc/__init__.py b/crc/__init__.py index d169b547..d56085d0 100644 --- a/crc/__init__.py +++ b/crc/__init__.py @@ -58,10 +58,6 @@ if app.config['ENABLE_SENTRY']: integrations=[FlaskIntegration()] ) -# Jinja environment definition, used to render mail templates -template_dir = os.getcwd() + '/crc/static/templates/mails' -env = Environment(loader=FileSystemLoader(template_dir)) - print('=== USING THESE CONFIG SETTINGS: ===') print('APPLICATION_ROOT = ', app.config['APPLICATION_ROOT']) print('CORS_ALLOW_ORIGINS = ', app.config['CORS_ALLOW_ORIGINS']) diff --git a/crc/api/workflow.py b/crc/api/workflow.py index 2e35dad2..68b84f56 100644 --- a/crc/api/workflow.py +++ b/crc/api/workflow.py @@ -142,6 +142,8 @@ def update_task(workflow_id, task_id, body, terminate_loop=None): processor = WorkflowProcessor(workflow_model) task_id = uuid.UUID(task_id) spiff_task = processor.bpmn_workflow.get_task(task_id) + if not spiff_task: + raise ApiError("empty_task", "Processor failed to obtain task.", status_code=404) if spiff_task.state != spiff_task.READY: raise ApiError("invalid_state", "You may not update a task unless it is in the READY state. " "Consider calling a token reset to make this task Ready.") diff --git a/crc/services/mails.py b/crc/services/mails.py index a1570035..7bffacee 100644 --- a/crc/services/mails.py +++ b/crc/services/mails.py @@ -1,18 +1,20 @@ -import os - -from flask import render_template, render_template_string from flask_mail import Message +from jinja2 import Environment, FileSystemLoader +from crc import app, mail from crc.services.email_service import EmailService +# Jinja environment definition, used to render mail templates +template_dir = app.root_path + '/static/templates/mails' +env = Environment(loader=FileSystemLoader(template_dir)) + def send_test_email(sender, recipients): try: msg = Message('Research Ramp-up Plan test', - sender=sender, - recipients=recipients, - bcc=['rrt_emails@googlegroups.com']) - from crc import env, mail + sender=sender, + recipients=recipients, + bcc=['rrt_emails@googlegroups.com']) template = env.get_template('ramp_up_approval_request_first_review.txt') template_vars = {'primary_investigator': "test"} msg.body = template.render(template_vars) @@ -22,12 +24,13 @@ def send_test_email(sender, recipients): except Exception as e: return str(e) + def send_mail(subject, sender, recipients, content, content_html, study_id=None): EmailService.add_email(subject=subject, sender=sender, recipients=recipients, - content=content, content_html=content_html, study_id=study_id) + content=content, content_html=content_html, study_id=study_id) + def send_ramp_up_submission_email(sender, recipients, approver_1, approver_2=None): - from crc import env subject = 'Research Ramp-up Plan Submitted' template = env.get_template('ramp_up_submission.txt') @@ -36,11 +39,10 @@ def send_ramp_up_submission_email(sender, recipients, approver_1, approver_2=Non template = env.get_template('ramp_up_submission.html') content_html = template.render(template_vars) - result = send_mail(subject, sender, recipients, content, content_html) - return result + send_mail(subject, sender, recipients, content, content_html) + def send_ramp_up_approval_request_email(sender, recipients, primary_investigator): - from crc import env subject = 'Research Ramp-up Plan Approval Request' template = env.get_template('ramp_up_approval_request.txt') @@ -49,11 +51,10 @@ def send_ramp_up_approval_request_email(sender, recipients, primary_investigator template = env.get_template('ramp_up_approval_request.html') content_html = template.render(template_vars) - result = send_mail(subject, sender, recipients, content, content_html) - return result + send_mail(subject, sender, recipients, content, content_html) + def send_ramp_up_approval_request_first_review_email(sender, recipients, primary_investigator): - from crc import env subject = 'Research Ramp-up Plan Approval Request' template = env.get_template('ramp_up_approval_request_first_review.txt') @@ -62,11 +63,10 @@ def send_ramp_up_approval_request_first_review_email(sender, recipients, primary template = env.get_template('ramp_up_approval_request_first_review.html') content_html = template.render(template_vars) - result = send_mail(subject, sender, recipients, content, content_html) - return result + send_mail(subject, sender, recipients, content, content_html) + def send_ramp_up_approved_email(sender, recipients, approver_1, approver_2=None): - from crc import env subject = 'Research Ramp-up Plan Approved' template = env.get_template('ramp_up_approved.txt') @@ -75,11 +75,10 @@ def send_ramp_up_approved_email(sender, recipients, approver_1, approver_2=None) template = env.get_template('ramp_up_approved.html') content_html = template.render(template_vars) - result = send_mail(subject, sender, recipients, content, content_html) - return result + send_mail(subject, sender, recipients, content, content_html) + def send_ramp_up_denied_email(sender, recipients, approver): - from crc import env subject = 'Research Ramp-up Plan Denied' template = env.get_template('ramp_up_denied.txt') @@ -88,11 +87,10 @@ def send_ramp_up_denied_email(sender, recipients, approver): template = env.get_template('ramp_up_denied.html') content_html = template.render(template_vars) - result = send_mail(subject, sender, recipients, content, content_html) - return result + send_mail(subject, sender, recipients, content, content_html) + def send_ramp_up_denied_email_to_approver(sender, recipients, primary_investigator, approver_2): - from crc import env subject = 'Research Ramp-up Plan Denied' template = env.get_template('ramp_up_denied_first_approver.txt') @@ -101,5 +99,4 @@ def send_ramp_up_denied_email_to_approver(sender, recipients, primary_investigat template = env.get_template('ramp_up_denied_first_approver.html') content_html = template.render(template_vars) - result = send_mail(subject, sender, recipients, content, content_html) - return result + send_mail(subject, sender, recipients, content, content_html) diff --git a/tests/emails/test_mails.py b/tests/emails/test_mails.py index 0710e02e..e9320f4d 100644 --- a/tests/emails/test_mails.py +++ b/tests/emails/test_mails.py @@ -1,8 +1,4 @@ - -from tests.base_test import BaseTest - -from crc import mail, session -from crc.models.approval import ApprovalModel, ApprovalStatus +from crc import mail from crc.models.email import EmailModel from crc.services.mails import ( send_ramp_up_submission_email, @@ -12,6 +8,7 @@ from crc.services.mails import ( send_ramp_up_denied_email, send_ramp_up_denied_email_to_approver ) +from tests.base_test import BaseTest class TestMails(BaseTest): @@ -30,7 +27,6 @@ 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.approver_1) self.assertEqual(len(outbox), 1) self.assertEqual(outbox[0].subject, 'Research Ramp-up Plan Submitted')