mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-07-05 20:23:17 +00:00
Restructure code and use APScheduler for daemon functionality
This commit is contained in:
34
app/email.py
34
app/email.py
@ -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
|
||||
|
Reference in New Issue
Block a user