2021-03-11 11:32:21 -05:00
|
|
|
import markdown
|
|
|
|
import re
|
|
|
|
|
2021-08-19 17:34:55 -04:00
|
|
|
from flask import render_template
|
2020-06-25 16:18:42 -06:00
|
|
|
from flask_mail import Message
|
2020-06-09 22:57:56 -06:00
|
|
|
|
2022-06-15 17:06:01 -04:00
|
|
|
from crc import app, mail, session
|
2020-06-09 22:57:56 -06:00
|
|
|
|
2022-06-15 17:06:01 -04:00
|
|
|
from crc.models.email import EmailModel, EmailDocCodesModel
|
2021-08-19 17:34:55 -04:00
|
|
|
from crc.models.study import StudyModel
|
2020-06-09 22:57:56 -06:00
|
|
|
|
2021-10-16 14:20:48 -04:00
|
|
|
from crc.services.jinja_service import JinjaService
|
|
|
|
|
2021-03-11 11:32:21 -05:00
|
|
|
|
2020-06-09 22:57:56 -06:00
|
|
|
class EmailService(object):
|
|
|
|
"""Provides common tools for working with an Email"""
|
|
|
|
|
|
|
|
@staticmethod
|
2021-08-19 17:34:55 -04:00
|
|
|
def add_email(subject, sender, recipients, content, content_html,
|
2022-04-25 15:38:15 -04:00
|
|
|
cc=None, bcc=None, study_id=None, reply_to=None, attachment_files=None, workflow_spec_id=None, name=None):
|
2020-06-09 22:57:56 -06:00
|
|
|
"""We will receive all data related to an email and store it"""
|
|
|
|
|
2020-06-17 17:00:16 -06:00
|
|
|
# Find corresponding study - if any
|
|
|
|
study = None
|
|
|
|
if type(study_id) == int:
|
2022-06-15 17:06:01 -04:00
|
|
|
study = session.query(StudyModel).get(study_id)
|
2020-06-09 22:57:56 -06:00
|
|
|
|
|
|
|
# Create EmailModel
|
|
|
|
email_model = EmailModel(subject=subject, sender=sender, recipients=str(recipients),
|
2021-10-07 09:22:05 -04:00
|
|
|
content=content, content_html=content_html, study=study,
|
2022-05-10 08:54:11 -04:00
|
|
|
cc=str(cc), bcc=str(bcc), workflow_spec_id=workflow_spec_id, name=name)
|
2020-06-17 17:00:16 -06:00
|
|
|
|
2022-06-15 17:06:01 -04:00
|
|
|
doc_codes = []
|
2020-06-25 16:18:42 -06:00
|
|
|
# Send mail
|
|
|
|
try:
|
|
|
|
msg = Message(subject,
|
2021-03-11 11:32:21 -05:00
|
|
|
sender=sender,
|
2021-08-19 17:34:55 -04:00
|
|
|
recipients=recipients,
|
|
|
|
body=content,
|
|
|
|
html=content_html,
|
|
|
|
cc=cc,
|
|
|
|
bcc=bcc,
|
|
|
|
reply_to=reply_to)
|
|
|
|
|
|
|
|
if attachment_files is not None:
|
|
|
|
for file in attachment_files:
|
2022-06-15 17:06:01 -04:00
|
|
|
if file['doc_code'] not in doc_codes:
|
|
|
|
doc_codes.append(file['doc_code'])
|
2022-04-12 13:50:04 -04:00
|
|
|
msg.attach(file['name'], file['type'], file['data'])
|
2020-06-25 16:18:42 -06:00
|
|
|
|
|
|
|
mail.send(msg)
|
2021-10-13 12:03:48 -04:00
|
|
|
|
2020-06-25 16:18:42 -06:00
|
|
|
except Exception as e:
|
2020-07-02 16:10:33 -06:00
|
|
|
app.logger.error('An exception happened in EmailService', exc_info=True)
|
2020-06-25 16:18:42 -06:00
|
|
|
app.logger.error(str(e))
|
2021-10-13 12:03:48 -04:00
|
|
|
raise e
|
2020-06-09 22:57:56 -06:00
|
|
|
|
2022-06-15 17:06:01 -04:00
|
|
|
session.add(email_model)
|
|
|
|
session.commit()
|
|
|
|
for doc_code in doc_codes:
|
|
|
|
email_doc_code = EmailDocCodesModel(email_id=email_model.id, doc_code=doc_code)
|
|
|
|
session.add(email_doc_code)
|
|
|
|
session.commit()
|
2021-10-07 09:22:05 -04:00
|
|
|
return email_model
|
2021-03-11 11:32:21 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def check_valid_email(email):
|
|
|
|
# regex from https://emailregex.com/
|
|
|
|
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
|
|
|
|
if re.search(regex, email):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_rendered_content(self, message, data):
|
2021-10-16 14:20:48 -04:00
|
|
|
content = JinjaService.get_content(message, data)
|
2021-10-08 11:26:30 -04:00
|
|
|
rendered_markdown = markdown.markdown(content, extensions=['nl2br'])
|
|
|
|
content_html = self.get_cr_connect_wrapper(rendered_markdown)
|
2021-03-11 11:32:21 -05:00
|
|
|
|
2021-10-08 11:26:30 -04:00
|
|
|
return content, content_html
|
2021-03-11 11:32:21 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_cr_connect_wrapper(email_body):
|
2021-07-22 13:25:06 -04:00
|
|
|
base_url = app.config['FRONTEND'] # The frontend url
|
|
|
|
return render_template('mail_content_template.html', email_body=email_body, base_url=base_url)
|