Blame view

app/__init__.py 1.66 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
from flask import Flask
0d4479a4   hitier   Add login mechanism
5
from flask_login import LoginManager
3122f2f8   hitier   Running mode 1: f...
6

0d4479a4   hitier   Add login mechanism
7
8
9
10
11
12
13
14
15
16
17
from app.models import db, User

login_manager = LoginManager()

login_manager.login_view = "auth.login"
login_manager.login_message = "Veuillez vous connecter s'il vous plait"


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))
76ef4fbe   hitier   Click, Sqlalchemy...
18

716b68ce   hitier   Add db migrate fa...
19

7b83b0b5   hitier   Fix #19 Add confi...
20
# Please, set a config file on top project dir
d8a6b942   hitier   More install doc
21
# see in ../pdc_config.py
7b83b0b5   hitier   Fix #19 Add confi...
22
try:
919d8186   hitier   Change config fil...
23
    from pdc_config import Config, ProdConfig, DevConfig, TestConfig
7b83b0b5   hitier   Fix #19 Add confi...
24
except ImportError:
a40ff516   hitier   Typo: update name...
25
    # TODO: use logging system
d8a6b942   hitier   More install doc
26
    print("Please set a pdc_config.py file in you PYTHON_PATH")
919d8186   hitier   Change config fil...
27
    print("See INSTALL.md for more info")
a40ff516   hitier   Typo: update name...
28
    sys.exit(-1)
7b83b0b5   hitier   Fix #19 Add confi...
29

3122f2f8   hitier   Running mode 1: f...
30
31
32
33
34

def create_app(config_class=None):
    """ App Factory.
    can be called with configuration class as argument.
    """
7b83b0b5   hitier   Fix #19 Add confi...
35
36
37
38
39
40

    # 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...
41
42
43
        flask_env = os.environ.get('FLASK_ENV')
        if not flask_env:
            flask_env = 'testing'
7b83b0b5   hitier   Fix #19 Add confi...
44

a40ff516   hitier   Typo: update name...
45
        if flask_env == 'production':
7b83b0b5   hitier   Fix #19 Add confi...
46
            config_class = ProdConfig
a40ff516   hitier   Typo: update name...
47
        elif flask_env == 'development':
7b83b0b5   hitier   Fix #19 Add confi...
48
            config_class = DevConfig
a40ff516   hitier   Typo: update name...
49
        elif flask_env == 'testing':
7b83b0b5   hitier   Fix #19 Add confi...
50
51
52
53
54
55
            config_class = TestConfig
        else:
            config_class = Config

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

76ef4fbe   hitier   Click, Sqlalchemy...
59
    db.init_app(app)
0d4479a4   hitier   Add login mechanism
60
    login_manager.init_app(app)
76ef4fbe   hitier   Click, Sqlalchemy...
61

7b83b0b5   hitier   Fix #19 Add confi...
62
63
    # Get and activate blueprints
    #
8bd0f3cb   hitier   Fix #25: Flask Bl...
64
65
    from .main import bp as main_bp
    app.register_blueprint(main_bp)
fcac7c5d   hitier   New auth blueprin...
66
67
    from .auth import bp as auth_bp
    app.register_blueprint(auth_bp)
8bd0f3cb   hitier   Fix #25: Flask Bl...
68

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