Blame view

flaskr/extensions.py 1.42 KB
30f6991a   Antoine Goutenoir   Continue moving s...
1
2
3
4
import sys
import traceback
from os import getenv

b952b644   Antoine Goutenoir   Disable email sen...
5

16f69d07   Antoine Goutenoir   Add an (unsecured...
6
from flask_admin import Admin
b9fc86c3   Antoine Goutenoir   Secure the admin ...
7
from flask_basicauth import BasicAuth
94906fa5   Goutte   Add flask extensi...
8
9
10
from flask_caching import Cache
from flask_debugtoolbar import DebugToolbarExtension
from flask_login import LoginManager
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
11
from flask_assets import Environment as Assets
30f6991a   Antoine Goutenoir   Continue moving s...
12
from flask_mail import Mail, Message
94906fa5   Goutte   Add flask extensi...
13
14
15

from flaskr.models import User

b952b644   Antoine Goutenoir   Disable email sen...
16

94906fa5   Goutte   Add flask extensi...
17
18
19
# Setup flask cache
cache = Cache()

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
20
21
# Init flask assets
assets_env = Assets()
94906fa5   Goutte   Add flask extensi...
22

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
23
24
25
26
# Mail handler
mail = Mail()

# Debug toolbar for easy dev (disabled in prod)
94906fa5   Goutte   Add flask extensi...
27
28
debug_toolbar = DebugToolbarExtension()

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
29
# Basic auth
94906fa5   Goutte   Add flask extensi...
30
31
32
33
login_manager = LoginManager()
login_manager.login_view = "main.login"
login_manager.login_message_category = "warning"

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
34
# Admin backoffice
16f69d07   Antoine Goutenoir   Add an (unsecured...
35
admin = Admin()
b9fc86c3   Antoine Goutenoir   Secure the admin ...
36
basic_auth = BasicAuth()
16f69d07   Antoine Goutenoir   Add an (unsecured...
37

94906fa5   Goutte   Add flask extensi...
38
39
40
41

@login_manager.user_loader
def load_user(userid):
    return User.query.get(userid)
30f6991a   Antoine Goutenoir   Continue moving s...
42
43
44
45


def send_email(to_recipient, subject, message):

b952b644   Antoine Goutenoir   Disable email sen...
46
47
48
    if 'production' != getenv('FLASK_ENV', 'production'):
        print("Skipping sending email because we are not in production.")
        return
30f6991a   Antoine Goutenoir   Continue moving s...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

    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)