1
0
forked from ScoDoc/ScoDoc
ScoDoc-Front/app/__init__.py

136 lines
4.2 KiB
Python
Raw Normal View History

2021-05-29 18:22:51 +02:00
# -*- coding: UTF-8 -*
# pylint: disable=invalid-name
import os
import sys
# Un hack en attendant la migration vers Python3 #sco8
2021-07-10 07:27:06 +02:00
if sys.version_info.major < 3:
reload(sys)
sys.setdefaultencoding("UTF8")
2021-05-29 18:22:51 +02:00
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
from flask import request
from flask import Flask
from flask import current_app
from flask import g
2021-05-29 18:22:51 +02:00
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from config import Config
from app.scodoc import notesdb as ndb
2021-05-29 18:22:51 +02:00
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager()
login.login_view = "auth.login"
login.login_message = "Please log in to access this page."
mail = Mail()
bootstrap = Bootstrap(app)
moment = Moment()
@app.before_request
def open_dept_db_connection():
# current_app.logger.info("open_dept_db_connection")
2021-07-04 12:42:04 +02:00
if hasattr(g, "scodoc_dept") and not hasattr(g, "db_conn") and g.scodoc_dept:
g.db_conn = ndb.open_dept_connection()
@app.teardown_request
def close_dept_db_connection():
# current_app.logger.info("close_dept_db_connection")
if hasattr(g, "db_conn"):
ndb.close_dept_connection()
2021-05-29 18:22:51 +02:00
def create_app(config_class=Config):
app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static")
app.logger.setLevel(logging.DEBUG)
2021-05-29 18:22:51 +02:00
app.config.from_object(config_class)
db.init_app(app)
migrate.init_app(app, db)
login.init_app(app)
mail.init_app(app)
bootstrap.init_app(app)
moment.init_app(app)
from app.auth import bp as auth_bp
app.register_blueprint(auth_bp, url_prefix="/auth")
from app.views import essais_bp
app.register_blueprint(essais_bp, url_prefix="/Essais")
2021-07-04 12:32:13 +02:00
from app.main import bp as main_bp
from app.views import scodoc_bp
from app.views import scolar_bp
2021-05-29 18:22:51 +02:00
from app.views import notes_bp
from app.views import users_bp
from app.views import absences_bp
2021-05-29 18:22:51 +02:00
2021-07-04 12:32:13 +02:00
app.register_blueprint(main_bp) # XXX à enlever en production #sco8
# https://scodoc.fr/ScoDoc
app.register_blueprint(scodoc_bp)
# https://scodoc.fr/ScoDoc/RT/Scolarite/...
app.register_blueprint(scolar_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite")
# https://scodoc.fr/ScoDoc/RT/Scolarite/Notes/...
app.register_blueprint(notes_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Notes")
# https://scodoc.fr/ScoDoc/RT/Scolarite/Users/...
app.register_blueprint(users_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Users")
# https://scodoc.fr/ScoDoc/RT/Scolarite/Absences/...
app.register_blueprint(
absences_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Absences"
)
2021-05-29 18:22:51 +02:00
if not app.debug and not app.testing:
if app.config["MAIL_SERVER"]:
auth = None
if app.config["MAIL_USERNAME"] or app.config["MAIL_PASSWORD"]:
auth = (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"])
secure = None
if app.config["MAIL_USE_TLS"]:
secure = ()
mail_handler = SMTPHandler(
mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
fromaddr="no-reply@" + app.config["MAIL_SERVER"],
toaddrs=[app.config["ADMINS"]],
subject="ScoDoc8 Failure",
credentials=auth,
secure=secure,
)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
if not os.path.exists("logs"):
os.mkdir("logs")
file_handler = RotatingFileHandler(
"logs/scodoc.log", maxBytes=10240, backupCount=10
)
file_handler.setFormatter(
logging.Formatter(
"%(asctime)s %(levelname)s: %(message)s " "[in %(pathname)s:%(lineno)d]"
)
)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.logger.info("ScoDoc8 startup")
return app
# from app import models