forked from j2logo/tutorial-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
148 lines (107 loc) · 4.03 KB
/
__init__.py
File metadata and controls
148 lines (107 loc) · 4.03 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
AUTOR: Juanjo
FECHA DE CREACIÓN: 24/05/2019
"""
import logging
from logging.handlers import SMTPHandler
from flask import Flask, render_template
from flask_login import LoginManager
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from app.common.filters import format_datetime
login_manager = LoginManager()
db = SQLAlchemy()
migrate = Migrate()
mail = Mail()
def create_app(settings_module):
app = Flask(__name__, instance_relative_config=True)
# Load the config file specified by the APP environment variable
app.config.from_object(settings_module)
# Load the configuration from the instance folder
if app.config.get('TESTING', False):
app.config.from_pyfile('config-testing.py', silent=True)
else:
app.config.from_pyfile('config.py', silent=True)
configure_logging(app)
login_manager.init_app(app)
login_manager.login_view = "auth.login"
db.init_app(app)
migrate.init_app(app, db)
mail.init_app(app)
# Registro de los filtros
register_filters(app)
# Registro de los Blueprints
from .auth import auth_bp
app.register_blueprint(auth_bp)
from .admin import admin_bp
app.register_blueprint(admin_bp)
from .public import public_bp
app.register_blueprint(public_bp)
# Custom error handlers
register_error_handlers(app)
return app
def register_filters(app):
app.jinja_env.filters['datetime'] = format_datetime
def register_error_handlers(app):
@app.errorhandler(500)
def base_error_handler(e):
return render_template('500.html'), 500
@app.errorhandler(404)
def error_404_handler(e):
return render_template('404.html'), 404
@app.errorhandler(401)
def error_404_handler(e):
return render_template('401.html'), 401
def configure_logging(app):
"""
Configura el módulo de logs. Establece los manejadores para cada logger.
:param app: Instancia de la aplicación Flask
"""
# Elimina los manejadores por defecto de la app
del app.logger.handlers[:]
loggers = [app.logger, ]
handlers = []
console_handler = logging.StreamHandler()
console_handler.setFormatter(verbose_formatter())
if (app.config['APP_ENV'] == app.config['APP_ENV_LOCAL']) or (
app.config['APP_ENV'] == app.config['APP_ENV_TESTING']) or (
app.config['APP_ENV'] == app.config['APP_ENV_DEVELOPMENT']):
console_handler.setLevel(logging.DEBUG)
handlers.append(console_handler)
elif app.config['APP_ENV'] == app.config['APP_ENV_PRODUCTION']:
console_handler.setLevel(logging.INFO)
handlers.append(console_handler)
mail_handler = SMTPHandler((app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
app.config['DONT_REPLY_FROM_EMAIL'],
app.config['ADMINS'],
'[Error][{}] La aplicación falló'.format(app.config['APP_ENV']),
(app.config['MAIL_USERNAME'],
app.config['MAIL_PASSWORD']),
())
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(mail_handler_formatter())
handlers.append(mail_handler)
for l in loggers:
for handler in handlers:
l.addHandler(handler)
l.propagate = False
l.setLevel(logging.DEBUG)
def mail_handler_formatter():
return logging.Formatter(
'''
Message type: %(levelname)s
Location: %(pathname)s:%(lineno)d
Module: %(module)s
Function: %(funcName)s
Time: %(asctime)s.%(msecs)d
Message:
%(message)s
''',
datefmt='%d/%m/%Y %H:%M:%S'
)
def verbose_formatter():
return logging.Formatter(
'[%(asctime)s.%(msecs)d]\t %(levelname)s \t[%(name)s.%(funcName)s:%(lineno)d]\t %(message)s',
datefmt='%d/%m/%Y %H:%M:%S'
)