Commit 135fafd4 authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Refactor mailer

parent 18c26d99
......@@ -11,13 +11,12 @@ from lib.functions import Functions
import requests
import wget
from jinja2 import Template
from flask_mail import Message, Mail
import traceback
class JobManager:
def __init__(self, id_job: str, email: str=None, query: Fasta=None, target: Fasta=None, app=None, mailer: Mail=None):
def __init__(self, id_job: str, email: str=None, query: Fasta=None, target: Fasta=None, mailer=None):
self.id_job = id_job
self.email = email
self.query = query
......@@ -41,11 +40,6 @@ class JobManager:
self.idx_t = os.path.join(self.output_dir, "target.idx")
self.logs = os.path.join(self.output_dir, "logs.txt")
self.mailer = mailer
self.app = app
def send_async_email(self, msg):
with self.app.app_context():
self.mailer.send(msg)
def __check_job_success_local(self):
if os.path.exists(self.paf):
......@@ -85,15 +79,8 @@ class JobManager:
return "DGenies - Job failed: %s" % self.id_job
def send_mail(self, status):
msg = Message(
subject=self.get_mail_subject(status),
recipients=[self.email],
html=self.get_mail_content_html(status),
body=self.get_mail_content(status),
sender=(self.mail_org, self.mail_status) if self.mail_org is not None else self.mail_status,
reply_to=self.mail_reply
)
self.send_async_email(msg)
self.mailer.send_mail([self.email], self.get_mail_subject(status), self.get_mail_content(status),
self.get_mail_content_html(status))
@db_session
def __launch_local(self):
......
from config_reader import AppConfigReader
from flask_mail import Mail, Message
class Mailer:
def __init__(self, app):
self.app = app
self.mail = Mail(app)
config_reader = AppConfigReader()
self.mail_status = config_reader.get_mail_status_sender()
self.mail_reply = config_reader.get_mail_reply()
self.mail_org = config_reader.get_mail_org()
def __send_async_email(self, msg):
with self.app.app_context():
self.mail.send(msg)
def send_mail(self, recipients: list, subject: str, message: str, message_html: str=None):
msg = Message(
subject= subject,
recipients=recipients,
html=message_html,
body=message,
sender=(self.mail_org, self.mail_status) if self.mail_org is not None else self.mail_status,
reply_to=self.mail_reply
)
self.__send_async_email(msg)
......@@ -13,6 +13,7 @@ from lib.job_manager import JobManager
from lib.functions import Functions, ALLOWED_EXTENSIONS
from lib.upload_file import UploadFile
from lib.Fasta import Fasta
from lib.mailer import Mailer
import sys
......@@ -37,7 +38,7 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = 'dsqdsq-255sdA-fHfg52-25Asd5'
# Init mail:
mail = Mail(app)
mailer = Mailer(app)
# Folder containing data:
app_data = config_reader.get_app_data()
......@@ -117,7 +118,7 @@ def launch_analysis():
target = Fasta(name=target_name, path=target_path, type_f=file_target_type)
# Launch job:
job = JobManager(id_job, email, query, target, app, mail)
job = JobManager(id_job, email, query, target, mailer)
job.launch()
return jsonify({"success": True, "redirect": url_for(".status", id_job=id_job)})
else:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment