Restructure code and use APScheduler for daemon functionality

This commit is contained in:
Patrick Jentsch
2022-06-28 12:30:02 +02:00
parent 7c52d3f392
commit b8bf004684
20 changed files with 755 additions and 710 deletions

View File

@ -1,27 +1,27 @@
from flask import current_app, render_template
from app import mail
from flask import current_app, Flask, render_template
from flask_mail import Message
from typing import Any, Text
from . import mail
from .decorators import background
from threading import Thread
from typing import Any
def create_message(
recipient: str,
subject: str,
template: str,
**kwargs: Any
) -> Message:
def create_message(recipient: str, subject: str, template: str, **kwargs: Any) -> Message:
subject_prefix: str = current_app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
msg: Message = Message(
f'{subject_prefix} {subject}',
recipients=[recipient]
body=render_template(f'{template}.txt.j2', **kwargs),
html=render_template(f'{template}.html.j2', **kwargs),
recipients=[recipient],
subject=f'{subject_prefix} {subject}'
)
msg.body = render_template(f'{template}.txt.j2', **kwargs)
msg.html = render_template(f'{template}.html.j2', **kwargs)
return msg
@background
def send(msg: Message, *args, **kwargs):
with kwargs['app'].app_context():
def _send(app: Flask, msg):
with app.app_context():
mail.send(msg)
def send(msg: Message, *args, **kwargs):
thread = Thread(target=_send, args=[current_app._get_current_object(), msg])
thread.start()
return thread