2021-03-11 16:32:21 +00:00
|
|
|
import markdown
|
|
|
|
import re
|
|
|
|
|
2020-06-10 04:57:56 +00:00
|
|
|
from datetime import datetime
|
2021-03-11 16:32:21 +00:00
|
|
|
from flask import render_template, request
|
2020-06-25 22:18:42 +00:00
|
|
|
from flask_mail import Message
|
2021-03-11 16:32:21 +00:00
|
|
|
from jinja2 import Template
|
2020-06-10 04:57:56 +00:00
|
|
|
from sqlalchemy import desc
|
|
|
|
|
2020-06-25 22:18:42 +00:00
|
|
|
from crc import app, db, mail, session
|
2020-06-10 04:57:56 +00:00
|
|
|
from crc.api.common import ApiError
|
|
|
|
|
2020-06-17 23:00:16 +00:00
|
|
|
from crc.models.study import StudyModel
|
2020-06-10 04:57:56 +00:00
|
|
|
from crc.models.email import EmailModel
|
|
|
|
|
|
|
|
|
2021-03-11 16:32:21 +00:00
|
|
|
|
2020-06-10 04:57:56 +00:00
|
|
|
class EmailService(object):
|
|
|
|
"""Provides common tools for working with an Email"""
|
|
|
|
|
|
|
|
@staticmethod
|
2021-03-11 16:32:21 +00:00
|
|
|
def add_email(subject, sender, recipients, content, content_html, cc=None, study_id=None):
|
2020-06-10 04:57:56 +00:00
|
|
|
"""We will receive all data related to an email and store it"""
|
|
|
|
|
2020-06-17 23:00:16 +00:00
|
|
|
# Find corresponding study - if any
|
|
|
|
study = None
|
|
|
|
if type(study_id) == int:
|
|
|
|
study = db.session.query(StudyModel).get(study_id)
|
2020-06-10 04:57:56 +00:00
|
|
|
|
|
|
|
# Create EmailModel
|
|
|
|
email_model = EmailModel(subject=subject, sender=sender, recipients=str(recipients),
|
2020-06-17 23:00:16 +00:00
|
|
|
content=content, content_html=content_html, study=study)
|
|
|
|
|
2020-06-25 22:18:42 +00:00
|
|
|
# Send mail
|
|
|
|
try:
|
|
|
|
msg = Message(subject,
|
2021-03-11 16:32:21 +00:00
|
|
|
sender=sender,
|
|
|
|
recipients=recipients)
|
2020-06-25 22:18:42 +00:00
|
|
|
|
|
|
|
msg.body = content
|
|
|
|
msg.html = content_html
|
2021-03-11 16:32:21 +00:00
|
|
|
msg.cc = cc
|
2020-06-25 22:18:42 +00:00
|
|
|
|
|
|
|
mail.send(msg)
|
|
|
|
except Exception as e:
|
2020-07-02 22:10:33 +00:00
|
|
|
app.logger.error('An exception happened in EmailService', exc_info=True)
|
2020-06-25 22:18:42 +00:00
|
|
|
app.logger.error(str(e))
|
2020-06-10 04:57:56 +00:00
|
|
|
|
|
|
|
db.session.add(email_model)
|
|
|
|
db.session.commit()
|
2021-03-11 16:32:21 +00: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):
|
|
|
|
template = Template(message)
|
|
|
|
rendered = template.render(data)
|
|
|
|
rendered_markdown = markdown.markdown(rendered)
|
|
|
|
wrapped = self.get_cr_connect_wrapper(rendered_markdown)
|
|
|
|
|
|
|
|
return rendered, wrapped
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_cr_connect_wrapper(email_body):
|
|
|
|
return render_template('mail_content_template.html', email_body=email_body, base_url=request.base_url)
|