Blame view

flaskr/__init__.py 1.9 KB
ff444969   Goutte   Add the app factory.
1
2
3
#! ../venv/bin/python

from flask import Flask
f80ff370   Antoine Goutenoir   Add a nasty hack ...
4
from flask.cli import ScriptInfo
ff444969   Goutte   Add the app factory.
5
6
7
8
from webassets.loaders import PythonLoader as PythonAssetsLoader

from flaskr import assets
from flaskr.models import db
35a99ac7   Goutte   Move more text co...
9
from flaskr.controllers.main_controller import main
ff444969   Goutte   Add the app factory.
10
11
12
13
14
15
16
17

from flaskr.extensions import (
    cache,
    assets_env,
    debug_toolbar,
    login_manager
)

35a99ac7   Goutte   Move more text co...
18
from flaskr.content import content
ff444969   Goutte   Add the app factory.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

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 ...
35
36
37
    # We bypass object_name (for dev)
    if type(object_name) == ScriptInfo:
        object_name = 'flaskr.settings.DevelopmentConfig'
ff444969   Goutte   Add the app factory.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    # Load configuration
    app.config.from_object(object_name)

    # initialize the cache
    cache.init_app(app)

    # initialize the debug tool bar
    debug_toolbar.init_app(app)

    # initialize SQLAlchemy
    db.init_app(app)

    login_manager.init_app(app)

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

35a99ac7   Goutte   Move more text co...
62
    # VERSION (move to version.py is necessary)
ff444969   Goutte   Add the app factory.
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    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,
        )

    # Markdown jinja2 filter
    @app.template_filter('markdown')
    def markdown_filter(text):
        return markdown(text)

    return app