Blame view

flaskr/__init__.py 3.63 KB
ff444969   Goutte   Add the app factory.
1
#! ../venv/bin/python
b9fc86c3   Antoine Goutenoir   Secure the admin ...
2
import os
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
3
from markdown import markdown
30f6991a   Antoine Goutenoir   Continue moving s...
4

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
5
from dotenv import load_dotenv, find_dotenv, dotenv_values
30f6991a   Antoine Goutenoir   Continue moving s...
6
# Load config from .env ; do this before importing local libs (and flask)
76e12c62   Antoine Goutenoir   Continue tackling...
7
8
9
10
# 1. Write into OS environment -- if this fails you forgot to create .env file
load_dotenv(find_dotenv(raise_error_if_not_found=True), override=True)
# 2. Load it as well to inject it later into app.config
local_env = dotenv_values(find_dotenv())
30f6991a   Antoine Goutenoir   Continue moving s...
11
12
13
14
15
16
# 3. Cast integers to integers
for key in local_env.keys():
    try:
        local_env[key] = int(local_env[key])
    except ValueError:
        pass
ff444969   Goutte   Add the app factory.
17

b9fc86c3   Antoine Goutenoir   Secure the admin ...
18
19

from flask import Flask, url_for
f80ff370   Antoine Goutenoir   Add a nasty hack ...
20
from flask.cli import ScriptInfo
ff444969   Goutte   Add the app factory.
21
22
from webassets.loaders import PythonLoader as PythonAssetsLoader

16f69d07   Antoine Goutenoir   Add an (unsecured...
23

ff444969   Goutte   Add the app factory.
24
from flaskr import assets
16f69d07   Antoine Goutenoir   Add an (unsecured...
25
from flaskr.models import db, Estimation, EstimationView
35a99ac7   Goutte   Move more text co...
26
from flaskr.controllers.main_controller import main
ff444969   Goutte   Add the app factory.
27
from flaskr.extensions import (
16f69d07   Antoine Goutenoir   Add an (unsecured...
28
    admin,
ff444969   Goutte   Add the app factory.
29
    assets_env,
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
30
31
    basic_auth,
    cache,
ff444969   Goutte   Add the app factory.
32
    debug_toolbar,
b9fc86c3   Antoine Goutenoir   Secure the admin ...
33
    login_manager,
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
34
    mail,
df588491   Antoine Goutenoir   Add support for s...
35
    session,
cb22e72e   Antoine Goutenoir   Add a Captcha and...
36
    captcha,
aa04230c   Antoine Goutenoir   feat: add support...
37
    icon2html,
ff444969   Goutte   Add the app factory.
38
)
35a99ac7   Goutte   Move more text co...
39
from flaskr.content import content
0c5ac2ae   Antoine Goutenoir   Actually use the ...
40
from flaskr.core import increment_hit_counter, get_hit_counter
ff444969   Goutte   Add the app factory.
41

ff444969   Goutte   Add the app factory.
42
43
44

def create_app(object_name):
    """
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
45
    A flask application factory, as explained here:
ff444969   Goutte   Add the app factory.
46
47
48
49
50
51
52
53
54
    http://flask.pocoo.org/docs/patterns/appfactories/

    Arguments:
        object_name: the python path of the config object,
                     e.g. flaskr.settings.ProductionConfig
    """

    app = Flask(__name__)

7bc7b9ae   Antoine Goutenoir   More.
55
    # We bypass object_name (for dev with flask run)
f80ff370   Antoine Goutenoir   Add a nasty hack ...
56
57
    if type(object_name) == ScriptInfo:
        object_name = 'flaskr.settings.DevelopmentConfig'
ff444969   Goutte   Add the app factory.
58

76e12c62   Antoine Goutenoir   Continue tackling...
59
    # Load the configuration
ff444969   Goutte   Add the app factory.
60
    app.config.from_object(object_name)
76e12c62   Antoine Goutenoir   Continue tackling...
61
62
    app.config.update(**local_env)

7bc7b9ae   Antoine Goutenoir   More.
63
64
65
    app.config['BASIC_AUTH_USERNAME'] = os.getenv('ADMIN_USERNAME')
    app.config['BASIC_AUTH_PASSWORD'] = os.getenv('ADMIN_PASSWORD')

df588491   Antoine Goutenoir   Add support for s...
66
67
68
69
70
    app.config['CAPTCHA_ENABLE'] = True
    app.config['CAPTCHA_LENGTH'] = 5
    app.config['CAPTCHA_WIDTH'] = 256
    app.config['CAPTCHA_HEIGHT'] = 158
    app.config['SESSION_TYPE'] = 'sqlalchemy'
df588491   Antoine Goutenoir   Add support for s...
71

16f69d07   Antoine Goutenoir   Add an (unsecured...
72
    # Initialize
ff444969   Goutte   Add the app factory.
73
    cache.init_app(app)
df588491   Antoine Goutenoir   Add support for s...
74
    session.init_app(app)
cb22e72e   Antoine Goutenoir   Add a Captcha and...
75
    captcha.init_app(app)
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
76
    mail.init_app(app)
ff444969   Goutte   Add the app factory.
77
    debug_toolbar.init_app(app)
ff444969   Goutte   Add the app factory.
78
    db.init_app(app)
ff444969   Goutte   Add the app factory.
79
    login_manager.init_app(app)
16f69d07   Antoine Goutenoir   Add an (unsecured...
80
81
    admin.init_app(app)
    admin.add_view(EstimationView(Estimation, db.session))
b9fc86c3   Antoine Goutenoir   Secure the admin ...
82
    basic_auth.init_app(app)
ff444969   Goutte   Add the app factory.
83

381afb49   Antoine Goutenoir   Move the base URL...
84
    # For session storage
df588491   Antoine Goutenoir   Add support for s...
85
86
    app.session_interface.db.create_all()

ff444969   Goutte   Add the app factory.
87
88
89
90
91
92
    # Import and register the different asset bundles
    assets_env.init_app(app)
    assets_loader = PythonAssetsLoader(assets)
    for name, bundle in assets_loader.load_bundles().items():
        assets_env.register(name, bundle)

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
93
    # Register our blueprints
ff444969   Goutte   Add the app factory.
94
95
    app.register_blueprint(main)

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
96
    # VERSION (move to version.py ?)
ff444969   Goutte   Add the app factory.
97
98
99
100
101
102
103
104
105
106
    version = "0.0.0"
    with open('VERSION', 'r') as version_file:
        version = version_file.read().strip()

    # Inject it as global template var
    @app.context_processor
    def inject_template_global_variables():
        return dict(
            content=content,
            version=version,
0c5ac2ae   Antoine Goutenoir   Actually use the ...
107
            visits=get_hit_counter(),
ff444969   Goutte   Add the app factory.
108
109
110
111
112
        )

    # Markdown jinja2 filter
    @app.template_filter('markdown')
    def markdown_filter(text):
aa04230c   Antoine Goutenoir   feat: add support...
113
        text = icon2html(text)
b00951e6   Antoine Goutenoir   Enable markdown e...
114
        return markdown(text, extensions=['extra'])
ff444969   Goutte   Add the app factory.
115

b9fc86c3   Antoine Goutenoir   Secure the admin ...
116
117
118
    # Authentication Gate for the Admin
    @app.before_first_request
    def restrict_admin_url():
b9fc86c3   Antoine Goutenoir   Secure the admin ...
119
120
121
122
123
124
125
126
127
128
        endpoint = 'admin.index'
        url = url_for(endpoint)
        admin_index = app.view_functions.pop(endpoint)

        @app.route(url, endpoint=endpoint)
        @basic_auth.required
        # @roles_required('admin')
        def secure_admin_index():
            return admin_index()

ff444969   Goutte   Add the app factory.
129
    return app