cr-connect-workflow/crc/services/email_service.py

43 lines
1.2 KiB
Python
Raw Normal View History

2020-06-10 04:57:56 +00:00
from datetime import datetime
2020-06-25 22:18:42 +00:00
from flask_mail import Message
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
class EmailService(object):
"""Provides common tools for working with an Email"""
@staticmethod
2020-06-17 23:00:16 +00:00
def add_email(subject, sender, recipients, content, content_html, study_id):
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,
sender=sender,
recipients=recipients)
msg.body = content
msg.html = content_html
mail.send(msg)
except Exception as e:
app.logger.error(str(e))
2020-06-10 04:57:56 +00:00
db.session.add(email_model)
db.session.commit()