Blame view

app/__init__.py 1.37 KB
7b83b0b5   hitier   Fix #19 Add confi...
1
import os
a40ff516   hitier   Typo: update name...
2
import sys
7b83b0b5   hitier   Fix #19 Add confi...
3

3122f2f8   hitier   Running mode 1: f...
4
5
from flask import Flask

76ef4fbe   hitier   Click, Sqlalchemy...
6
7
from app.models import db

716b68ce   hitier   Add db migrate fa...
8

7b83b0b5   hitier   Fix #19 Add confi...
9
# Please, set a config file on top project dir
919d8186   hitier   Change config fil...
10
# find example in ressources/pdc_config.py
7b83b0b5   hitier   Fix #19 Add confi...
11
try:
919d8186   hitier   Change config fil...
12
    from pdc_config import Config, ProdConfig, DevConfig, TestConfig
7b83b0b5   hitier   Fix #19 Add confi...
13
except ImportError:
a40ff516   hitier   Typo: update name...
14
    # TODO: use logging system
919d8186   hitier   Change config fil...
15
16
    print("Please set an pdc_config.py file in you PYTHON_PATH")
    print("See INSTALL.md for more info")
a40ff516   hitier   Typo: update name...
17
    sys.exit(-1)
7b83b0b5   hitier   Fix #19 Add confi...
18

3122f2f8   hitier   Running mode 1: f...
19
20
21
22
23

def create_app(config_class=None):
    """ App Factory.
    can be called with configuration class as argument.
    """
7b83b0b5   hitier   Fix #19 Add confi...
24
25
26
27
28
29

    # Configuration Switcher
    # try to set configuration depending on environment
    # if no configuration class was given
    #
    if config_class is None:
a40ff516   hitier   Typo: update name...
30
31
32
        flask_env = os.environ.get('FLASK_ENV')
        if not flask_env:
            flask_env = 'testing'
7b83b0b5   hitier   Fix #19 Add confi...
33

a40ff516   hitier   Typo: update name...
34
        if flask_env == 'production':
7b83b0b5   hitier   Fix #19 Add confi...
35
            config_class = ProdConfig
a40ff516   hitier   Typo: update name...
36
        elif flask_env == 'development':
7b83b0b5   hitier   Fix #19 Add confi...
37
            config_class = DevConfig
a40ff516   hitier   Typo: update name...
38
        elif flask_env == 'testing':
7b83b0b5   hitier   Fix #19 Add confi...
39
40
41
42
43
44
            config_class = TestConfig
        else:
            config_class = Config

    # Main flask app
    #
3122f2f8   hitier   Running mode 1: f...
45
    app = Flask(__name__)
7b83b0b5   hitier   Fix #19 Add confi...
46
    app.config.from_object(config_class)
8bd0f3cb   hitier   Fix #25: Flask Bl...
47

76ef4fbe   hitier   Click, Sqlalchemy...
48
49
    db.init_app(app)

7b83b0b5   hitier   Fix #19 Add confi...
50
51
    # Get and activate blueprints
    #
8bd0f3cb   hitier   Fix #25: Flask Bl...
52
53
    from .main import bp as main_bp
    app.register_blueprint(main_bp)
fcac7c5d   hitier   New auth blueprin...
54
55
    from .auth import bp as auth_bp
    app.register_blueprint(auth_bp)
8bd0f3cb   hitier   Fix #25: Flask Bl...
56

3122f2f8   hitier   Running mode 1: f...
57
    return app