extensions.py
1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import traceback
from os import getenv
from flask_admin import Admin
from flask_basicauth import BasicAuth
from flask_caching import Cache
from flask_debugtoolbar import DebugToolbarExtension
from flask_login import LoginManager
from flask_assets import Environment as Assets
from flask_mail import Mail, Message
from flask_sessionstore import Session
from flaskr.models import User
# Setup flask cache
cache = Cache()
# Flask assets
assets_env = Assets()
# Session Store for captcha and perhaps visits counter
session = Session()
# Mail handler
mail = Mail()
# Debug toolbar for easy dev (disabled in prod)
debug_toolbar = DebugToolbarExtension()
# Basic auth
login_manager = LoginManager()
login_manager.login_view = "main.login"
login_manager.login_message_category = "warning"
# Admin backoffice
admin = Admin()
basic_auth = BasicAuth()
@login_manager.user_loader
def load_user(userid):
return User.query.get(userid)
def send_email(to_recipient, subject, message):
if 'production' != getenv('FLASK_ENV', 'production'):
print("Skipping sending email because we are not in production.")
return
try:
msg = Message(
subject=subject,
html=message,
sender=getenv('MAIL_DEFAULT_SENDER'),
recipients=[to_recipient],
bcc=[
'antoine@goutenoir.com', # :(|)
],
)
mail.send(msg)
except Exception as e:
print("ERROR Sending email:\n%s" % str(e))
traceback.print_exc(file=sys.stderr)