Blame view

flaskr/__init__.py 2.72 KB
ff444969   Goutte   Add the app factory.
1
#! ../venv/bin/python
b9fc86c3   Antoine Goutenoir   Secure the admin ...
2
3
4
import os
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
ff444969   Goutte   Add the app factory.
5

b9fc86c3   Antoine Goutenoir   Secure the admin ...
6
7

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

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

ff444969   Goutte   Add the app factory.
12
from flaskr import assets
16f69d07   Antoine Goutenoir   Add an (unsecured...
13
from flaskr.models import db, Estimation, EstimationView
35a99ac7   Goutte   Move more text co...
14
from flaskr.controllers.main_controller import main
ff444969   Goutte   Add the app factory.
15
16

from flaskr.extensions import (
16f69d07   Antoine Goutenoir   Add an (unsecured...
17
    admin,
ff444969   Goutte   Add the app factory.
18
19
20
    cache,
    assets_env,
    debug_toolbar,
b9fc86c3   Antoine Goutenoir   Secure the admin ...
21
22
    login_manager,
    basic_auth
ff444969   Goutte   Add the app factory.
23
24
)

35a99ac7   Goutte   Move more text co...
25
from flaskr.content import content
0c5ac2ae   Antoine Goutenoir   Actually use the ...
26
from flaskr.core import increment_hit_counter, get_hit_counter
ff444969   Goutte   Add the app factory.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

from markdown import markdown


def create_app(object_name):
    """
    An flask application factory, as explained here:
    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__)

f80ff370   Antoine Goutenoir   Add a nasty hack ...
43
44
45
    # We bypass object_name (for dev)
    if type(object_name) == ScriptInfo:
        object_name = 'flaskr.settings.DevelopmentConfig'
ff444969   Goutte   Add the app factory.
46
47
48
49

    # Load configuration
    app.config.from_object(object_name)

16f69d07   Antoine Goutenoir   Add an (unsecured...
50
    # Initialize
ff444969   Goutte   Add the app factory.
51
    cache.init_app(app)
ff444969   Goutte   Add the app factory.
52
    debug_toolbar.init_app(app)
ff444969   Goutte   Add the app factory.
53
    db.init_app(app)
ff444969   Goutte   Add the app factory.
54
    login_manager.init_app(app)
16f69d07   Antoine Goutenoir   Add an (unsecured...
55
56
    admin.init_app(app)
    admin.add_view(EstimationView(Estimation, db.session))
b9fc86c3   Antoine Goutenoir   Secure the admin ...
57
    basic_auth.init_app(app)
ff444969   Goutte   Add the app factory.
58
59
60
61
62
63
64
65
66
67

    # 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)

    # register our blueprints
    app.register_blueprint(main)

b9fc86c3   Antoine Goutenoir   Secure the admin ...
68
69
70
    app.config['BASIC_AUTH_USERNAME'] = os.getenv('ADMIN_USERNAME')
    app.config['BASIC_AUTH_PASSWORD'] = os.getenv('ADMIN_PASSWORD')

35a99ac7   Goutte   Move more text co...
71
    # VERSION (move to version.py is necessary)
ff444969   Goutte   Add the app factory.
72
73
74
75
76
77
78
79
80
81
    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 ...
82
            visits=get_hit_counter(),
ff444969   Goutte   Add the app factory.
83
84
85
86
87
        )

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

b9fc86c3   Antoine Goutenoir   Secure the admin ...
90
91
92
    # Authentication Gate for the Admin
    @app.before_first_request
    def restrict_admin_url():
b9fc86c3   Antoine Goutenoir   Secure the admin ...
93
94
95
96
97
98
99
100
101
102
        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.
103
    return app