Blame view

app/__init__.py 2.79 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

f186bcd6   hitier   first logging try
7
import logging
7e08aa0c   hitier   Allow force loggi...
8
9
10
from logging.handlers import RotatingFileHandler
from flask.logging import default_handler

a5a365e8   hitier   Move User model t...
11
12
from app.models import db
from app.auth.models import User
f186bcd6   hitier   first logging try
13

db7f51f7   hitier   Load dotenv file ...
14
15
16
17
18
19
from dotenv import load_dotenv

app_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
env_file = os.path.join(app_dir, ".flaskenv")
load_dotenv(env_file, verbose=True)

0d4479a4   hitier   Add login mechanism
20
21
22
23
24
25
26
27
28
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...
29

716b68ce   hitier   Add db migrate fa...
30

f186bcd6   hitier   first logging try
31
32
# TODO: move the following into create_app()
#  and use app.logger system
7b83b0b5   hitier   Fix #19 Add confi...
33
# Please, set a config file on top project dir
d8a6b942   hitier   More install doc
34
# see in ../pdc_config.py
7b83b0b5   hitier   Fix #19 Add confi...
35
try:
919d8186   hitier   Change config fil...
36
    from pdc_config import Config, ProdConfig, DevConfig, TestConfig
7b83b0b5   hitier   Fix #19 Add confi...
37
except ImportError:
d8a6b942   hitier   More install doc
38
    print("Please set a pdc_config.py file in you PYTHON_PATH")
919d8186   hitier   Change config fil...
39
    print("See INSTALL.md for more info")
a40ff516   hitier   Typo: update name...
40
    sys.exit(-1)
7b83b0b5   hitier   Fix #19 Add confi...
41

3122f2f8   hitier   Running mode 1: f...
42
43
44
45
46

def create_app(config_class=None):
    """ App Factory.
    can be called with configuration class as argument.
    """
7b83b0b5   hitier   Fix #19 Add confi...
47
48
49
50
51
52

    # 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...
53
54
55
        flask_env = os.environ.get('FLASK_ENV')
        if not flask_env:
            flask_env = 'testing'
7b83b0b5   hitier   Fix #19 Add confi...
56

a40ff516   hitier   Typo: update name...
57
        if flask_env == 'production':
7b83b0b5   hitier   Fix #19 Add confi...
58
            config_class = ProdConfig
a40ff516   hitier   Typo: update name...
59
        elif flask_env == 'development':
7b83b0b5   hitier   Fix #19 Add confi...
60
            config_class = DevConfig
a40ff516   hitier   Typo: update name...
61
        elif flask_env == 'testing':
7b83b0b5   hitier   Fix #19 Add confi...
62
63
64
65
66
67
            config_class = TestConfig
        else:
            config_class = Config

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

5e4e39bb   hitier   Tiny Code refactor
71
72
    # Log to stdout or to file
    #
f186bcd6   hitier   first logging try
73
    app.logger.removeHandler(default_handler)
7e08aa0c   hitier   Allow force loggi...
74
    if app.debug or app.testing or app.config['LOG_TO_STDOUT']:
5e4e39bb   hitier   Tiny Code refactor
75
        log_handler = logging.StreamHandler()
f186bcd6   hitier   first logging try
76
    else:
5918e302   hitier   Set loglevel from...
77
78
        logs_file = app.config['PDC_LOGS_FILE']
        logs_dir = os.path.dirname(logs_file)
ac058f3d   hitier   Absolute paths fo...
79
80
        if not os.path.exists(logs_dir):
            os.mkdir(logs_dir)
5e4e39bb   hitier   Tiny Code refactor
81
82
83
84
85
        log_handler = RotatingFileHandler(logs_file, maxBytes=10240, backupCount=10)

    log_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    app.logger.addHandler(log_handler)
f186bcd6   hitier   first logging try
86

5918e302   hitier   Set loglevel from...
87
    app.logger.setLevel(app.config['PDC_LOGS_LEVEL'])
f186bcd6   hitier   first logging try
88
89
    app.logger.info("Starting PDC-WEB")

76ef4fbe   hitier   Click, Sqlalchemy...
90
    db.init_app(app)
0d4479a4   hitier   Add login mechanism
91
    login_manager.init_app(app)
76ef4fbe   hitier   Click, Sqlalchemy...
92

7b83b0b5   hitier   Fix #19 Add confi...
93
94
    # Get and activate blueprints
    #
8bd0f3cb   hitier   Fix #25: Flask Bl...
95
96
    from .main import bp as main_bp
    app.register_blueprint(main_bp)
5e4e39bb   hitier   Tiny Code refactor
97

fcac7c5d   hitier   New auth blueprin...
98
99
    from .auth import bp as auth_bp
    app.register_blueprint(auth_bp)
8bd0f3cb   hitier   Fix #25: Flask Bl...
100

775de511   hitier   Error handlers fo...
101
102
103
    from .errors import bp as errors_bp
    app.register_blueprint(errors_bp)

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