__init__.py
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
import os
from flask import Flask
from flask_login import LoginManager
import logging
from logging.handlers import RotatingFileHandler
from flask.logging import default_handler
from app.models import db
from app.auth.models import User
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)
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))
# TODO: move the following into create_app()
# and use app.logger system
# Please, set a config file on top project dir
# see in ../pdc_config.py
try:
from pdc_config import Config, ProdConfig, DevConfig, TestConfig
except ImportError:
sys.stderr.write("ERROR: Please set a pdc_config.py file in you PYTHON_PATH\n")
sys.stderr.write("ERROR: See INSTALL.md for more info\n")
sys.exit(-1)
def create_app(config_class=None):
""" App Factory.
can be called with configuration class as argument.
"""
# Configuration Switcher
# try to set configuration depending on environment
# if no configuration class was given
#
if config_class is None:
flask_env = os.environ.get('FLASK_ENV')
if not flask_env:
flask_env = 'testing'
if flask_env == 'production':
config_class = ProdConfig
elif flask_env == 'development':
config_class = DevConfig
elif flask_env == 'testing':
config_class = TestConfig
else:
config_class = Config
# Main flask app
#
app = Flask(__name__)
app.config.from_object(config_class)
# Log to stdout or to file
#
app.logger.removeHandler(default_handler)
if app.debug or app.testing or app.config['LOG_TO_STDOUT']:
log_handler = logging.StreamHandler()
else:
logs_file = app.config['PDC_LOGS_FILE']
logs_dir = os.path.dirname(logs_file)
if not os.path.exists(logs_dir):
os.mkdir(logs_dir)
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)
app.logger.setLevel(app.config['PDC_LOGS_LEVEL'])
app.logger.info("Starting PDC-WEB")
db.init_app(app)
login_manager.init_app(app)
# Get and activate blueprints
#
from .main import bp as main_bp
app.register_blueprint(main_bp)
from .auth import bp as auth_bp
app.register_blueprint(auth_bp)
from .errors import bp as errors_bp
app.register_blueprint(errors_bp)
return app