Blame view

flaskr/__init__.py 3.24 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,
ff444969   Goutte   Add the app factory.
35
)
35a99ac7   Goutte   Move more text co...
36
from flaskr.content import content
0c5ac2ae   Antoine Goutenoir   Actually use the ...
37
from flaskr.core import increment_hit_counter, get_hit_counter
ff444969   Goutte   Add the app factory.
38

ff444969   Goutte   Add the app factory.
39
40
41

def create_app(object_name):
    """
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
42
    A flask application factory, as explained here:
ff444969   Goutte   Add the app factory.
43
44
45
46
47
48
49
50
51
    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.
52
    # We bypass object_name (for dev with flask run)
f80ff370   Antoine Goutenoir   Add a nasty hack ...
53
54
    if type(object_name) == ScriptInfo:
        object_name = 'flaskr.settings.DevelopmentConfig'
ff444969   Goutte   Add the app factory.
55

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

7bc7b9ae   Antoine Goutenoir   More.
60
61
62
    app.config['BASIC_AUTH_USERNAME'] = os.getenv('ADMIN_USERNAME')
    app.config['BASIC_AUTH_PASSWORD'] = os.getenv('ADMIN_PASSWORD')

16f69d07   Antoine Goutenoir   Add an (unsecured...
63
    # Initialize
ff444969   Goutte   Add the app factory.
64
    cache.init_app(app)
c2a01bd2   Antoine Goutenoir   Prepare mail flas...
65
    mail.init_app(app)
ff444969   Goutte   Add the app factory.
66
    debug_toolbar.init_app(app)
ff444969   Goutte   Add the app factory.
67
    db.init_app(app)
ff444969   Goutte   Add the app factory.
68
    login_manager.init_app(app)
16f69d07   Antoine Goutenoir   Add an (unsecured...
69
70
    admin.init_app(app)
    admin.add_view(EstimationView(Estimation, db.session))
b9fc86c3   Antoine Goutenoir   Secure the admin ...
71
    basic_auth.init_app(app)
ff444969   Goutte   Add the app factory.
72
73
74
75
76
77
78

    # 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...
79
    # Register our blueprints
ff444969   Goutte   Add the app factory.
80
81
    app.register_blueprint(main)

c2a01bd2   Antoine Goutenoir   Prepare mail flas...
82
    # VERSION (move to version.py ?)
ff444969   Goutte   Add the app factory.
83
84
85
86
87
88
89
90
91
92
    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 ...
93
            visits=get_hit_counter(),
ff444969   Goutte   Add the app factory.
94
95
96
97
98
        )

    # Markdown jinja2 filter
    @app.template_filter('markdown')
    def markdown_filter(text):
b00951e6   Antoine Goutenoir   Enable markdown e...
99
        return markdown(text, extensions=['extra'])
ff444969   Goutte   Add the app factory.
100

b9fc86c3   Antoine Goutenoir   Secure the admin ...
101
102
103
    # Authentication Gate for the Admin
    @app.before_first_request
    def restrict_admin_url():
b9fc86c3   Antoine Goutenoir   Secure the admin ...
104
105
106
107
108
109
110
111
112
113
        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.
114
    return app