mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-14 16:55:42 +00:00
Add typehints to email code
This commit is contained in:
parent
eeb5a280b3
commit
551b928dca
31
app/email.py
31
app/email.py
@ -1,25 +1,32 @@
|
||||
from flask import current_app, render_template
|
||||
from flask import current_app, Flask, render_template
|
||||
from flask_mail import Message
|
||||
from threading import Thread
|
||||
from app import mail
|
||||
|
||||
|
||||
def create_message(recipient, subject, template, **kwargs):
|
||||
subject_prefix: str = current_app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
|
||||
msg: Message = Message(
|
||||
body=render_template(f'{template}.txt.j2', **kwargs),
|
||||
html=render_template(f'{template}.html.j2', **kwargs),
|
||||
def create_message(
|
||||
recipient: str,
|
||||
subject: str,
|
||||
template: str,
|
||||
**context
|
||||
) -> Message:
|
||||
message = Message(
|
||||
body=render_template(f'{template}.txt.j2', **context),
|
||||
html=render_template(f'{template}.html.j2', **context),
|
||||
recipients=[recipient],
|
||||
subject=f'{subject_prefix} {subject}'
|
||||
subject=f'[nopaque] {subject}'
|
||||
)
|
||||
return msg
|
||||
return message
|
||||
|
||||
|
||||
def send(msg, *args, **kwargs):
|
||||
def _send(app, msg):
|
||||
def send(message: Message) -> Thread:
|
||||
def _send(app: Flask, message: Message):
|
||||
with app.app_context():
|
||||
mail.send(msg)
|
||||
mail.send(message)
|
||||
|
||||
thread = Thread(target=_send, args=[current_app._get_current_object(), msg])
|
||||
thread = Thread(
|
||||
target=_send,
|
||||
args=[current_app._get_current_object(), message]
|
||||
)
|
||||
thread.start()
|
||||
return thread
|
||||
|
Loading…
Reference in New Issue
Block a user