Blame view

flaskr/extensions.py 1.89 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
df588491   Antoine Goutenoir   Add support for s...
13
from flask_sessionstore import Session
cb22e72e   Antoine Goutenoir   Add a Captcha and...
14
from flask_session_captcha import FlaskSessionCaptcha
94906fa5   Goutte   Add flask extensi...
15
16
17

from flaskr.models import User

b952b644   Antoine Goutenoir   Disable email sen...
18

94906fa5   Goutte   Add flask extensi...
19
20
21
# Setup flask cache
cache = Cache()

df588491   Antoine Goutenoir   Add support for s...
22
# Flask assets
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
23
assets_env = Assets()
94906fa5   Goutte   Add flask extensi...
24

df588491   Antoine Goutenoir   Add support for s...
25
26
27
# Session Store for captcha and perhaps visits counter
session = Session()

cb22e72e   Antoine Goutenoir   Add a Captcha and...
28
29
30
# Captcha
captcha = FlaskSessionCaptcha()

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
31
32
33
34
# Mail handler
mail = Mail()

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

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
37
# Basic auth
94906fa5   Goutte   Add flask extensi...
38
39
40
41
login_manager = LoginManager()
login_manager.login_view = "main.login"
login_manager.login_message_category = "warning"

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
42
# Admin backoffice
16f69d07   Antoine Goutenoir   Add an (unsecured...
43
admin = Admin()
b9fc86c3   Antoine Goutenoir   Secure the admin ...
44
basic_auth = BasicAuth()
16f69d07   Antoine Goutenoir   Add an (unsecured...
45

94906fa5   Goutte   Add flask extensi...
46
47
48
49

@login_manager.user_loader
def load_user(userid):
    return User.query.get(userid)
30f6991a   Antoine Goutenoir   Continue moving s...
50
51
52


def send_email(to_recipient, subject, message):
b952b644   Antoine Goutenoir   Disable email sen...
53
54
55
    if 'production' != getenv('FLASK_ENV', 'production'):
        print("Skipping sending email because we are not in production.")
        return
30f6991a   Antoine Goutenoir   Continue moving s...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

    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)

aa04230c   Antoine Goutenoir   feat: add support...
72
73
74
75
76
77
78
79
80

def icon2html(text):
    import re
    icon_html = r"""<svg class="bi" width="16" height="16" fill="currentColor"><use xlink:href="/static/bootstrap-icons-1.0.0/bootstrap-icons.svg#\1"/></svg>"""
    return re.sub(
        "<icon +([^ ]+)>",
        icon_html,
        text
    )