We can now send emails to users associated with a study, by using 'associated' as the recipient
We now use keyword arguments in the email script; subject and recipients
This commit is contained in:
parent
7d97fe107d
commit
44ac55bc32
|
@ -3,11 +3,13 @@ import re
|
||||||
import markdown
|
import markdown
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
|
|
||||||
from crc import app
|
from crc import app, session
|
||||||
from crc.api.common import ApiError
|
from crc.api.common import ApiError
|
||||||
|
from crc.models.user import UserModel
|
||||||
from crc.scripts.script import Script
|
from crc.scripts.script import Script
|
||||||
from crc.services.ldap_service import LdapService
|
from crc.services.ldap_service import LdapService
|
||||||
from crc.services.email_service import EmailService
|
from crc.services.email_service import EmailService
|
||||||
|
from crc.services.study_service import StudyService
|
||||||
|
|
||||||
from flask import render_template, request
|
from flask import render_template, request
|
||||||
|
|
||||||
|
@ -27,17 +29,34 @@ email ("My Subject", "dhf8r@virginia.edu", pi.email)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
||||||
self.get_subject(args)
|
self.get_subject(kwargs['subject'])
|
||||||
self.get_email_recipients(task, args)
|
self.get_email_recipients(kwargs['recipients'])
|
||||||
self.get_content(task)
|
self.get_content(task)
|
||||||
|
|
||||||
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
||||||
|
|
||||||
if len(args) < 2:
|
if 'subject' in kwargs and 'recipients' in kwargs:
|
||||||
|
subject = self.get_subject(kwargs['subject'])
|
||||||
|
|
||||||
|
# we can send the email to all the people associated with a study
|
||||||
|
# who have send_email set to True
|
||||||
|
if kwargs['recipients'] == 'associated':
|
||||||
|
recipient_uids = []
|
||||||
|
recipients = []
|
||||||
|
associates = StudyService.get_study_associates(study_id)
|
||||||
|
for associate in associates:
|
||||||
|
if associate['send_email'] is True:
|
||||||
|
recipient_uids.append(associate['uid'])
|
||||||
|
# Shoe.query.filter(Shoe.id.in_(my_list_of_ids)).all()
|
||||||
|
returned = UserModel.query.filter(UserModel.uid.in_(recipient_uids)).all()
|
||||||
|
for item in returned:
|
||||||
|
recipients.append(item.email_address)
|
||||||
|
else:
|
||||||
|
recipients = self.get_email_recipients(kwargs['recipients'])
|
||||||
|
|
||||||
|
else:
|
||||||
raise ApiError(code="missing_argument",
|
raise ApiError(code="missing_argument",
|
||||||
message="Email script requires a subject and at least one email address as arguments")
|
message="Email script requires a subject and at least one email address as arguments")
|
||||||
subject = self.get_subject(args)
|
|
||||||
recipients = self.get_email_recipients(task, args)
|
|
||||||
|
|
||||||
if recipients:
|
if recipients:
|
||||||
content, content_html = self.get_content(task)
|
content, content_html = self.get_content(task)
|
||||||
|
@ -50,50 +69,39 @@ email ("My Subject", "dhf8r@virginia.edu", pi.email)
|
||||||
study_id=study_id
|
study_id=study_id
|
||||||
)
|
)
|
||||||
|
|
||||||
def check_valid_email(self, email):
|
@staticmethod
|
||||||
|
def check_valid_email(email):
|
||||||
# regex from https://emailregex.com/
|
# regex from https://emailregex.com/
|
||||||
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
|
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
|
||||||
if (re.search(regex, email)):
|
if re.search(regex, email):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_email_recipients(self, task, args):
|
def get_email_recipients(self, recipients):
|
||||||
emails = []
|
emails = []
|
||||||
|
|
||||||
if len(args) < 2:
|
# Recipient can be an email address or list of email addresses
|
||||||
raise ApiError(code="missing_argument",
|
if isinstance(recipients, str):
|
||||||
message="Email script requires at least one email address as an argument. "
|
emails_to_check = [recipients]
|
||||||
"Multiple email addresses are accepted.")
|
elif isinstance(recipients, list):
|
||||||
|
emails_to_check = recipients
|
||||||
|
else:
|
||||||
|
raise ApiError(code="invalid_argument",
|
||||||
|
message=f"Email script requires a valid email address (or list of addresses), but received '{recipients}'")
|
||||||
|
|
||||||
# Every argument following the subject should be an email, or a list of emails.
|
for e in emails_to_check:
|
||||||
for arg in args[1:]:
|
if self.check_valid_email(e):
|
||||||
if isinstance(arg, str):
|
emails.append(e)
|
||||||
emails_to_check = [arg]
|
|
||||||
elif isinstance(arg, list):
|
|
||||||
emails_to_check = arg
|
|
||||||
else:
|
else:
|
||||||
raise ApiError(code="invalid_argument",
|
raise ApiError(code="invalid_argument",
|
||||||
message=f"Email script requires a valid email address, but received '{arg}'")
|
message="The email address you provided could not be parsed. "
|
||||||
|
"The value you provided is '%s" % e)
|
||||||
for e in emails_to_check:
|
|
||||||
if self.check_valid_email(e):
|
|
||||||
emails.append(e)
|
|
||||||
else:
|
|
||||||
raise ApiError(code="invalid_argument",
|
|
||||||
message="The email address you provided could not be parsed. "
|
|
||||||
"The value you provided is '%s" % e)
|
|
||||||
|
|
||||||
return emails
|
return emails
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_subject(args):
|
def get_subject(subject):
|
||||||
# subject = ''
|
|
||||||
if len(args[0]) < 1:
|
|
||||||
raise ApiError(code="missing_argument",
|
|
||||||
message="No subject was provided for the email message.")
|
|
||||||
|
|
||||||
subject = args[0]
|
|
||||||
if not subject or not isinstance(subject, str):
|
if not subject or not isinstance(subject, str):
|
||||||
raise ApiError(code="invalid_argument",
|
raise ApiError(code="invalid_argument",
|
||||||
message="The subject you provided could not be parsed. "
|
message="The subject you provided could not be parsed. "
|
||||||
|
|
Loading…
Reference in New Issue