2021-05-29 18:22:51 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
2022-09-10 15:23:54 +02:00
|
|
|
import base64
|
2021-10-07 22:26:29 +02:00
|
|
|
import datetime
|
2023-04-04 09:57:54 +02:00
|
|
|
import json
|
2021-05-29 18:22:51 +02:00
|
|
|
import os
|
2021-08-29 19:57:32 +02:00
|
|
|
import socket
|
2021-07-01 18:54:07 +02:00
|
|
|
import sys
|
2021-08-29 19:57:32 +02:00
|
|
|
import time
|
|
|
|
import traceback
|
2021-07-01 18:54:07 +02:00
|
|
|
|
2021-05-29 18:22:51 +02:00
|
|
|
import logging
|
2021-08-29 19:57:32 +02:00
|
|
|
from logging.handlers import SMTPHandler, WatchedFileHandler
|
2022-03-21 11:48:34 +01:00
|
|
|
from threading import Thread
|
2023-04-04 09:57:54 +02:00
|
|
|
import warnings
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2021-08-30 11:03:24 +02:00
|
|
|
from flask import current_app, g, request
|
2021-05-29 18:22:51 +02:00
|
|
|
from flask import Flask
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask import abort, flash, has_request_context
|
2021-07-28 08:12:57 +02:00
|
|
|
from flask import render_template
|
2023-03-19 10:26:03 +01:00
|
|
|
from flask.logging import default_handler
|
2023-02-26 21:24:07 +01:00
|
|
|
from flask_caching import Cache
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask_json import FlaskJSON, json_response
|
2021-09-15 00:33:30 +02:00
|
|
|
from flask_login import LoginManager, current_user
|
2021-05-29 18:22:51 +02:00
|
|
|
from flask_mail import Mail
|
2023-02-26 21:24:07 +01:00
|
|
|
from flask_migrate import Migrate
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
2023-01-30 22:25:17 +01:00
|
|
|
from jinja2 import select_autoescape
|
2023-04-03 17:40:45 +02:00
|
|
|
import sqlalchemy as sa
|
2023-09-01 14:38:41 +02:00
|
|
|
import werkzeug.debug
|
2023-11-15 20:38:51 +01:00
|
|
|
from wtforms.fields import HiddenField
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2023-03-19 10:26:03 +01:00
|
|
|
from flask_cas import CAS
|
|
|
|
|
2021-10-10 21:03:18 +02:00
|
|
|
from app.scodoc.sco_exceptions import (
|
|
|
|
AccessDenied,
|
2022-03-21 11:48:34 +01:00
|
|
|
ScoBugCatcher,
|
2022-12-09 06:06:53 +01:00
|
|
|
ScoException,
|
2021-10-10 21:03:18 +02:00
|
|
|
ScoGenError,
|
2023-03-19 10:26:03 +01:00
|
|
|
ScoInvalidCSRF,
|
2023-09-01 14:38:41 +02:00
|
|
|
ScoPDFFormatError,
|
2021-10-10 21:03:18 +02:00
|
|
|
ScoValueError,
|
|
|
|
APIInvalidParams,
|
|
|
|
)
|
2023-04-04 09:57:54 +02:00
|
|
|
from app.scodoc.sco_vdi import ApoEtapeVDI
|
|
|
|
|
2021-08-09 23:23:11 +02:00
|
|
|
from config import DevConfig
|
2021-08-21 17:07:44 +02:00
|
|
|
import sco_version
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2021-07-20 17:32:04 +02:00
|
|
|
db = SQLAlchemy()
|
2021-08-27 17:03:47 +02:00
|
|
|
migrate = Migrate(compare_type=True)
|
2021-05-29 18:22:51 +02:00
|
|
|
login = LoginManager()
|
|
|
|
login.login_view = "auth.login"
|
2021-09-11 15:59:06 +02:00
|
|
|
login.login_message = "Identifiez-vous pour accéder à cette page."
|
2022-07-27 16:03:14 +02:00
|
|
|
|
2021-05-29 18:22:51 +02:00
|
|
|
mail = Mail()
|
|
|
|
|
2022-03-12 09:39:28 +01:00
|
|
|
CACHE_TYPE = os.environ.get("CACHE_TYPE")
|
|
|
|
cache = Cache(
|
2021-07-27 14:33:11 +02:00
|
|
|
config={
|
2021-08-22 07:48:40 +02:00
|
|
|
# see https://flask-caching.readthedocs.io/en/latest/index.html#configuring-flask-caching
|
2022-03-12 09:39:28 +01:00
|
|
|
"CACHE_TYPE": CACHE_TYPE or "RedisCache",
|
|
|
|
# by default, never expire:
|
|
|
|
"CACHE_DEFAULT_TIMEOUT": os.environ.get("CACHE_DEFAULT_TIMEOUT") or 0,
|
2021-07-27 14:33:11 +02:00
|
|
|
}
|
|
|
|
)
|
2021-07-19 19:53:01 +02:00
|
|
|
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2021-07-28 08:12:57 +02:00
|
|
|
def handle_sco_value_error(exc):
|
2023-09-01 14:38:41 +02:00
|
|
|
"page d'erreur avec message"
|
2023-01-30 22:25:17 +01:00
|
|
|
return render_template("sco_value_error.j2", exc=exc), 404
|
2021-07-28 08:12:57 +02:00
|
|
|
|
|
|
|
|
2021-10-10 21:03:18 +02:00
|
|
|
def handle_access_denied(exc):
|
2023-01-30 22:25:17 +01:00
|
|
|
return render_template("error_access_denied.j2", exc=exc), 403
|
2021-10-10 21:03:18 +02:00
|
|
|
|
|
|
|
|
2023-03-19 10:26:03 +01:00
|
|
|
def handle_invalid_csrf(exc):
|
|
|
|
"""Form submit with invalid CSRF token"""
|
|
|
|
# logout user and go back to login page with an error message
|
|
|
|
from app import auth
|
|
|
|
|
|
|
|
auth.logic.logout()
|
|
|
|
return render_template("error_csrf.j2", exc=exc), 404
|
|
|
|
|
|
|
|
|
2023-12-07 23:07:11 +01:00
|
|
|
# def handle_pdf_format_error(exc):
|
|
|
|
# return "ay ay ay"
|
|
|
|
handle_pdf_format_error = handle_sco_value_error
|
2023-09-01 14:38:41 +02:00
|
|
|
|
|
|
|
|
2022-03-14 14:09:59 +01:00
|
|
|
def internal_server_error(exc):
|
2021-08-30 11:03:24 +02:00
|
|
|
"""Bugs scodoc, erreurs 500"""
|
2021-08-29 22:42:38 +02:00
|
|
|
# note that we set the 500 status explicitly
|
2022-09-10 15:23:54 +02:00
|
|
|
from app.scodoc import sco_utils as scu
|
|
|
|
|
2023-03-22 10:15:27 +01:00
|
|
|
# Invalide tous les caches
|
|
|
|
log("internal_server_error: clearing caches")
|
|
|
|
clear_scodoc_cache()
|
|
|
|
|
2021-10-07 22:26:29 +02:00
|
|
|
return (
|
|
|
|
render_template(
|
2023-01-30 22:25:17 +01:00
|
|
|
"error_500.j2",
|
2021-10-07 22:26:29 +02:00
|
|
|
SCOVERSION=sco_version.SCOVERSION,
|
|
|
|
date=datetime.datetime.now().isoformat(),
|
2022-03-14 14:09:59 +01:00
|
|
|
exc=exc,
|
2022-09-10 15:23:54 +02:00
|
|
|
traceback_str_base64=base64.urlsafe_b64encode(
|
|
|
|
traceback.format_exc().encode(scu.SCO_ENCODING)
|
|
|
|
).decode(scu.SCO_ENCODING),
|
2022-03-14 14:09:59 +01:00
|
|
|
request_url=request.url,
|
2022-09-10 15:23:54 +02:00
|
|
|
scu=scu,
|
2021-10-07 22:26:29 +02:00
|
|
|
),
|
|
|
|
500,
|
|
|
|
)
|
2021-08-29 22:42:38 +02:00
|
|
|
|
|
|
|
|
2022-03-21 11:48:34 +01:00
|
|
|
def handle_sco_bug(exc):
|
|
|
|
"""Un bug, en général rare, sur lequel les dev cherchent des
|
|
|
|
informations pour le corriger.
|
|
|
|
"""
|
2022-12-09 06:06:53 +01:00
|
|
|
if current_app.config["TESTING"] or current_app.config["DEBUG"]:
|
|
|
|
raise ScoException # for development servers only
|
|
|
|
else:
|
|
|
|
Thread(
|
|
|
|
target=_async_dump, args=(current_app._get_current_object(), request.url)
|
|
|
|
).start()
|
2022-03-21 11:48:34 +01:00
|
|
|
|
|
|
|
return internal_server_error(exc)
|
|
|
|
|
|
|
|
|
|
|
|
def _async_dump(app, request_url: str):
|
|
|
|
from app.scodoc.sco_dump_db import sco_dump_and_send_db
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
ndb.open_db_connection()
|
|
|
|
try:
|
|
|
|
sco_dump_and_send_db("ScoBugCatcher", request_url=request_url)
|
|
|
|
except ScoValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-09-13 09:54:53 +02:00
|
|
|
def handle_invalid_usage(error):
|
2023-04-06 16:10:32 +02:00
|
|
|
response = json_response(data_=error.to_dict())
|
2021-09-13 09:54:53 +02:00
|
|
|
response.status_code = error.status_code
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2022-08-01 17:50:51 +02:00
|
|
|
# JSON ENCODING
|
2023-04-06 16:10:32 +02:00
|
|
|
# used by some internal finctions
|
2023-08-01 22:07:23 +02:00
|
|
|
# the API is now using flask_json, NOT THIS ENCODER
|
2023-04-04 12:14:05 +02:00
|
|
|
class ScoDocJSONEncoder(json.JSONEncoder):
|
2023-04-04 09:57:54 +02:00
|
|
|
def default(self, o): # pylint: disable=E0202
|
|
|
|
if isinstance(o, (datetime.date, datetime.datetime)):
|
2022-08-01 17:50:51 +02:00
|
|
|
return o.isoformat()
|
2023-04-04 09:57:54 +02:00
|
|
|
elif isinstance(o, ApoEtapeVDI):
|
|
|
|
return str(o)
|
|
|
|
else:
|
|
|
|
return json.JSONEncoder.default(self, o)
|
2022-08-01 17:50:51 +02:00
|
|
|
|
|
|
|
|
2021-08-30 11:03:24 +02:00
|
|
|
def render_raw_html(template_filename: str, **args) -> str:
|
|
|
|
"""Load and render an HTML file _without_ using Flask
|
2023-02-26 21:24:07 +01:00
|
|
|
Necessary for 503 error message, when DB is down and Flask may be broken.
|
2021-08-30 11:03:24 +02:00
|
|
|
"""
|
|
|
|
template_path = os.path.join(
|
|
|
|
current_app.config["SCODOC_DIR"],
|
|
|
|
"app",
|
|
|
|
current_app.template_folder,
|
|
|
|
template_filename,
|
|
|
|
)
|
|
|
|
with open(template_path) as f:
|
|
|
|
txt = f.read().format(**args)
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
|
|
def postgresql_server_error(e):
|
|
|
|
"""Erreur de connection au serveur postgresql (voir notesdb.open_db_connection)"""
|
2023-01-30 22:25:17 +01:00
|
|
|
return render_raw_html("error_503.j2", SCOVERSION=sco_version.SCOVERSION), 503
|
2021-08-30 11:03:24 +02:00
|
|
|
|
|
|
|
|
2021-09-15 00:33:30 +02:00
|
|
|
class LogRequestFormatter(logging.Formatter):
|
2021-08-29 22:42:38 +02:00
|
|
|
"""Ajoute URL et remote_addr for logging"""
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
if has_request_context():
|
|
|
|
record.url = request.url
|
|
|
|
record.remote_addr = request.remote_addr
|
|
|
|
else:
|
|
|
|
record.url = None
|
|
|
|
record.remote_addr = None
|
2021-09-15 00:33:30 +02:00
|
|
|
record.sco_user = current_user
|
2021-10-07 23:33:24 +02:00
|
|
|
if has_request_context():
|
|
|
|
record.sco_admin_mail = current_app.config["SCODOC_ADMIN_MAIL"]
|
|
|
|
else:
|
|
|
|
record.sco_admin_mail = "(pas de requête)"
|
2021-09-15 00:33:30 +02:00
|
|
|
|
|
|
|
return super().format(record)
|
|
|
|
|
|
|
|
|
|
|
|
class LogExceptionFormatter(logging.Formatter):
|
|
|
|
"""Formatteur pour les exceptions: ajoute détails"""
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
if has_request_context():
|
|
|
|
record.url = request.url
|
2021-09-15 15:19:08 +02:00
|
|
|
record.remote_addr = request.environ.get(
|
|
|
|
"HTTP_X_FORWARDED_FOR", request.remote_addr
|
|
|
|
)
|
2021-09-15 00:33:30 +02:00
|
|
|
record.http_referrer = request.referrer
|
|
|
|
record.http_method = request.method
|
|
|
|
if request.method == "GET":
|
|
|
|
record.http_params = str(request.args)
|
|
|
|
else:
|
2021-09-27 22:58:05 +02:00
|
|
|
# rep = reprlib.Repr() # abbrège
|
|
|
|
record.http_params = str(request.form)[:2048]
|
2021-09-15 00:33:30 +02:00
|
|
|
else:
|
|
|
|
record.url = None
|
|
|
|
record.remote_addr = None
|
|
|
|
record.http_referrer = None
|
|
|
|
record.http_method = None
|
|
|
|
record.http_params = None
|
|
|
|
record.sco_user = current_user
|
2021-10-07 23:33:24 +02:00
|
|
|
|
|
|
|
if has_request_context():
|
|
|
|
record.sco_admin_mail = current_app.config["SCODOC_ADMIN_MAIL"]
|
|
|
|
else:
|
|
|
|
record.sco_admin_mail = "(pas de requête)"
|
2021-08-29 22:42:38 +02:00
|
|
|
return super().format(record)
|
|
|
|
|
|
|
|
|
2021-09-12 23:06:23 +02:00
|
|
|
class ScoSMTPHandler(SMTPHandler):
|
|
|
|
def getSubject(self, record: logging.LogRecord) -> str:
|
|
|
|
stack_summary = traceback.extract_tb(record.exc_info[2])
|
|
|
|
frame_summary = stack_summary[-1]
|
2021-09-13 09:54:53 +02:00
|
|
|
subject = f"ScoExc({sco_version.SCOVERSION}): {record.exc_info[0].__name__} in {frame_summary.name} {frame_summary.filename}"
|
|
|
|
|
2021-09-12 23:06:23 +02:00
|
|
|
return subject
|
|
|
|
|
|
|
|
|
2021-09-16 16:05:37 +02:00
|
|
|
class ReverseProxied(object):
|
|
|
|
"""Adaptateur wsgi qui nous permet d'avoir toutes les URL calculées en https
|
|
|
|
sauf quand on est en dev.
|
|
|
|
La variable HTTP_X_FORWARDED_PROTO est positionnée par notre config nginx"""
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
scheme = environ.get("HTTP_X_FORWARDED_PROTO")
|
|
|
|
if scheme:
|
|
|
|
environ["wsgi.url_scheme"] = scheme # ou forcer à https ici ?
|
|
|
|
return self.app(environ, start_response)
|
|
|
|
|
|
|
|
|
2021-08-09 23:23:11 +02:00
|
|
|
def create_app(config_class=DevConfig):
|
2021-06-16 16:59:31 +02:00
|
|
|
app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static")
|
2023-04-06 16:10:32 +02:00
|
|
|
app.config.from_object(config_class)
|
2023-02-28 21:40:50 +01:00
|
|
|
from app.auth import cas
|
|
|
|
|
|
|
|
CAS(app, url_prefix="/cas", configuration_function=cas.set_cas_configuration)
|
2021-09-16 16:05:37 +02:00
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
2023-08-01 22:07:23 +02:00
|
|
|
app_json = FlaskJSON(app)
|
|
|
|
|
|
|
|
@app_json.encoder
|
|
|
|
def scodoc_json_encoder(o):
|
|
|
|
"Overide default date encoding (RFC 822) and use ISO"
|
|
|
|
if isinstance(o, (datetime.date, datetime.datetime)):
|
|
|
|
return o.isoformat()
|
2022-06-24 03:32:48 +02:00
|
|
|
|
2023-04-03 17:40:45 +02:00
|
|
|
# Pour conserver l'ordre des objets dans les JSON:
|
|
|
|
# e.g. l'ordre des UE dans les bulletins
|
|
|
|
app.json.sort_keys = False
|
|
|
|
|
2022-06-24 03:32:48 +02:00
|
|
|
# Evite de logguer toutes les requetes dans notre log
|
|
|
|
logging.getLogger("werkzeug").disabled = True
|
2023-02-27 09:46:15 +01:00
|
|
|
app.logger.setLevel(app.config["LOG_LEVEL"])
|
2023-04-04 09:57:54 +02:00
|
|
|
if app.config["TESTING"] or app.config["DEBUG"]:
|
|
|
|
# S'arrête sur tous les warnings, sauf
|
|
|
|
# flask_sqlalchemy/query (pb deprecation du model.get())
|
|
|
|
warnings.filterwarnings("error", module="flask_sqlalchemy/query")
|
2023-04-10 11:25:46 +02:00
|
|
|
# warnings.filterwarnings("ignore", module="json/provider.py") xxx sans effet en test
|
2023-04-11 13:48:57 +02:00
|
|
|
if app.config["DEBUG"]:
|
|
|
|
# comme on a désactivé ci-dessus les logs de werkzeug,
|
|
|
|
# on affiche nous même le PIN en mode debug:
|
|
|
|
print(
|
|
|
|
f""" * Debugger is active!
|
|
|
|
* Debugger PIN: {werkzeug.debug.get_pin_and_cookie_name(app)[0]}
|
|
|
|
"""
|
|
|
|
)
|
2022-06-26 17:54:44 +02:00
|
|
|
# Vérifie/crée lien sym pour les URL statiques
|
|
|
|
link_filename = f"{app.root_path}/static/links/{sco_version.SCOVERSION}"
|
|
|
|
if not os.path.exists(link_filename):
|
|
|
|
app.logger.info(f"creating symlink {link_filename}")
|
|
|
|
os.symlink("..", link_filename)
|
|
|
|
|
2021-05-29 18:22:51 +02:00
|
|
|
db.init_app(app)
|
|
|
|
migrate.init_app(app, db)
|
|
|
|
login.init_app(app)
|
|
|
|
mail.init_app(app)
|
2023-02-28 19:43:48 +01:00
|
|
|
app.extensions["mail"].debug = 0 # disable copy of mails to stderr
|
2021-07-19 19:53:01 +02:00
|
|
|
cache.init_app(app)
|
|
|
|
sco_cache.CACHE = cache
|
2022-03-12 09:39:28 +01:00
|
|
|
if CACHE_TYPE: # non default
|
|
|
|
app.logger.info(f"CACHE_TYPE={CACHE_TYPE}")
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2021-09-21 14:01:46 +02:00
|
|
|
app.register_error_handler(ScoGenError, handle_sco_value_error)
|
2021-07-28 08:12:57 +02:00
|
|
|
app.register_error_handler(ScoValueError, handle_sco_value_error)
|
2022-03-21 11:48:34 +01:00
|
|
|
app.register_error_handler(ScoBugCatcher, handle_sco_bug)
|
2023-03-19 10:26:03 +01:00
|
|
|
app.register_error_handler(ScoInvalidCSRF, handle_invalid_csrf)
|
2023-09-01 14:38:41 +02:00
|
|
|
app.register_error_handler(ScoPDFFormatError, handle_pdf_format_error)
|
2021-10-10 21:03:18 +02:00
|
|
|
app.register_error_handler(AccessDenied, handle_access_denied)
|
2021-08-29 22:42:38 +02:00
|
|
|
app.register_error_handler(500, internal_server_error)
|
2021-08-30 11:03:24 +02:00
|
|
|
app.register_error_handler(503, postgresql_server_error)
|
2021-09-13 09:54:53 +02:00
|
|
|
app.register_error_handler(APIInvalidParams, handle_invalid_usage)
|
2021-07-28 08:12:57 +02:00
|
|
|
|
2021-05-29 18:22:51 +02:00
|
|
|
from app.auth import bp as auth_bp
|
|
|
|
|
|
|
|
app.register_blueprint(auth_bp, url_prefix="/auth")
|
|
|
|
|
2021-12-23 19:28:25 +01:00
|
|
|
from app.entreprises import bp as entreprises_bp
|
2022-02-21 19:25:38 +01:00
|
|
|
|
2021-12-23 19:28:25 +01:00
|
|
|
app.register_blueprint(entreprises_bp, url_prefix="/ScoDoc/entreprises")
|
|
|
|
|
2021-07-04 12:32:13 +02:00
|
|
|
from app.views import scodoc_bp
|
2021-05-31 00:14:15 +02:00
|
|
|
from app.views import scolar_bp
|
2021-05-29 18:22:51 +02:00
|
|
|
from app.views import notes_bp
|
2021-06-26 21:57:54 +02:00
|
|
|
from app.views import users_bp
|
2021-05-31 00:14:15 +02:00
|
|
|
from app.views import absences_bp
|
2023-04-17 15:44:55 +02:00
|
|
|
from app.views import assiduites_bp
|
2022-07-27 16:03:14 +02:00
|
|
|
from app.api import api_bp
|
|
|
|
from app.api import api_web_bp
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2024-04-06 12:16:53 +02:00
|
|
|
# Jinja2 configuration
|
2023-01-30 22:25:17 +01:00
|
|
|
# Enable autoescaping of all templates, including .j2
|
|
|
|
app.jinja_env.autoescape = select_autoescape(default_for_string=True, default=True)
|
2024-04-06 12:16:53 +02:00
|
|
|
app.jinja_env.trim_blocks = True
|
|
|
|
app.jinja_env.lstrip_blocks = True
|
|
|
|
# previously in Flask-Bootstrap:
|
|
|
|
app.jinja_env.globals["bootstrap_is_hidden_field"] = lambda field: isinstance(
|
|
|
|
field, HiddenField
|
|
|
|
)
|
2023-01-30 22:25:17 +01:00
|
|
|
|
2021-07-04 12:32:13 +02:00
|
|
|
# https://scodoc.fr/ScoDoc
|
|
|
|
app.register_blueprint(scodoc_bp)
|
2021-05-31 00:14:15 +02:00
|
|
|
# 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")
|
2021-06-26 21:57:54 +02:00
|
|
|
# https://scodoc.fr/ScoDoc/RT/Scolarite/Users/...
|
|
|
|
app.register_blueprint(users_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Users")
|
2021-05-31 00:14:15 +02:00
|
|
|
# https://scodoc.fr/ScoDoc/RT/Scolarite/Absences/...
|
|
|
|
app.register_blueprint(
|
|
|
|
absences_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Absences"
|
|
|
|
)
|
2023-04-17 15:44:55 +02:00
|
|
|
app.register_blueprint(
|
|
|
|
assiduites_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Assiduites"
|
|
|
|
)
|
2021-09-09 12:49:23 +02:00
|
|
|
app.register_blueprint(api_bp, url_prefix="/ScoDoc/api")
|
2022-07-27 16:03:14 +02:00
|
|
|
app.register_blueprint(api_web_bp, url_prefix="/ScoDoc/<scodoc_dept>/api")
|
2022-07-26 09:00:48 +02:00
|
|
|
|
2021-09-15 00:33:30 +02:00
|
|
|
scodoc_log_formatter = LogRequestFormatter(
|
|
|
|
"[%(asctime)s] %(sco_user)s@%(remote_addr)s requested %(url)s\n"
|
|
|
|
"%(levelname)s: %(message)s"
|
|
|
|
)
|
2021-10-07 22:26:29 +02:00
|
|
|
# les champs additionnels sont définis dans LogRequestFormatter
|
2021-09-15 00:33:30 +02:00
|
|
|
scodoc_exc_formatter = LogExceptionFormatter(
|
|
|
|
"[%(asctime)s] %(sco_user)s@%(remote_addr)s requested %(url)s\n"
|
|
|
|
"%(levelname)s: %(message)s\n"
|
|
|
|
"Referrer: %(http_referrer)s\n"
|
|
|
|
"Method: %(http_method)s\n"
|
|
|
|
"Params: %(http_params)s\n"
|
2021-10-07 22:26:29 +02:00
|
|
|
"Admin mail: %(sco_admin_mail)s\n"
|
2021-08-29 22:42:38 +02:00
|
|
|
)
|
2021-08-21 19:51:33 +02:00
|
|
|
if not app.testing:
|
2021-08-29 22:42:38 +02:00
|
|
|
if not app.debug:
|
|
|
|
# --- Config logs pour PRODUCTION
|
|
|
|
# On supprime le logguer par défaut qui va vers stderr et pollue les logs systemes
|
|
|
|
app.logger.removeHandler(default_handler)
|
|
|
|
# --- Mail des messages ERROR et CRITICAL
|
|
|
|
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 = ()
|
|
|
|
host_name = socket.gethostname()
|
2021-09-12 23:06:23 +02:00
|
|
|
mail_handler = ScoSMTPHandler(
|
2021-08-29 22:42:38 +02:00
|
|
|
mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
|
2022-01-19 22:40:41 +01:00
|
|
|
fromaddr=app.config["SCODOC_MAIL_FROM"],
|
2021-08-29 22:42:38 +02:00
|
|
|
toaddrs=["exception@scodoc.org"],
|
2021-09-13 10:06:25 +02:00
|
|
|
subject="ScoDoc Exception", # unused see ScoSMTPHandler
|
2021-08-29 22:42:38 +02:00
|
|
|
credentials=auth,
|
|
|
|
secure=secure,
|
|
|
|
)
|
|
|
|
mail_handler.setFormatter(scodoc_exc_formatter)
|
|
|
|
mail_handler.setLevel(logging.ERROR)
|
|
|
|
app.logger.addHandler(mail_handler)
|
|
|
|
else:
|
|
|
|
# Pour logs en DEV uniquement:
|
2021-09-15 00:33:30 +02:00
|
|
|
default_handler.setFormatter(scodoc_log_formatter)
|
2021-08-29 22:42:38 +02:00
|
|
|
|
|
|
|
# Config logs pour DEV et PRODUCTION
|
2021-08-21 19:51:33 +02:00
|
|
|
# Configuration des logs (actifs aussi en mode development)
|
2021-08-29 19:57:32 +02:00
|
|
|
# usually /opt/scodoc-data/log/scodoc.log:
|
|
|
|
# rotated by logrotate
|
|
|
|
file_handler = WatchedFileHandler(
|
|
|
|
app.config["SCODOC_LOG_FILE"], encoding="utf-8"
|
2021-05-29 18:22:51 +02:00
|
|
|
)
|
2021-09-15 00:33:30 +02:00
|
|
|
file_handler.setFormatter(scodoc_log_formatter)
|
2021-05-29 18:22:51 +02:00
|
|
|
file_handler.setLevel(logging.INFO)
|
|
|
|
app.logger.addHandler(file_handler)
|
2021-09-15 00:33:30 +02:00
|
|
|
# Log pour les erreurs (exceptions) uniquement:
|
|
|
|
# usually /opt/scodoc-data/log/scodoc_exc.log
|
|
|
|
file_handler = WatchedFileHandler(
|
|
|
|
app.config["SCODOC_ERR_FILE"], encoding="utf-8"
|
|
|
|
)
|
|
|
|
file_handler.setFormatter(scodoc_exc_formatter)
|
|
|
|
file_handler.setLevel(logging.ERROR)
|
|
|
|
app.logger.addHandler(file_handler)
|
2021-05-29 18:22:51 +02:00
|
|
|
|
2021-08-29 19:57:32 +02:00
|
|
|
# app.logger.setLevel(logging.INFO)
|
2021-08-21 17:07:44 +02:00
|
|
|
app.logger.info(f"{sco_version.SCONAME} {sco_version.SCOVERSION} startup")
|
2021-08-29 19:57:32 +02:00
|
|
|
app.logger.info(
|
|
|
|
f"create_app({config_class.__name__}, {config_class.SQLALCHEMY_DATABASE_URI})"
|
|
|
|
)
|
|
|
|
# ---- INITIALISATION SPECIFIQUES A SCODOC
|
|
|
|
from app.scodoc import sco_bulletins_generator
|
2021-09-29 14:47:43 +02:00
|
|
|
|
2021-08-29 19:57:32 +02:00
|
|
|
from app.scodoc.sco_bulletins_legacy import BulletinGeneratorLegacy
|
|
|
|
from app.scodoc.sco_bulletins_standard import BulletinGeneratorStandard
|
2022-02-21 19:25:38 +01:00
|
|
|
from app.but.bulletin_but_pdf import BulletinGeneratorStandardBUT
|
2021-08-29 19:57:32 +02:00
|
|
|
from app.scodoc.sco_bulletins_ucac import BulletinGeneratorUCAC
|
|
|
|
|
2022-02-21 19:25:38 +01:00
|
|
|
# l'ordre est important, le premier sera le "défaut" pour les nouveaux départements.
|
2021-08-29 19:57:32 +02:00
|
|
|
sco_bulletins_generator.register_bulletin_class(BulletinGeneratorStandard)
|
2022-02-21 19:25:38 +01:00
|
|
|
sco_bulletins_generator.register_bulletin_class(BulletinGeneratorStandardBUT)
|
2021-09-29 14:47:43 +02:00
|
|
|
sco_bulletins_generator.register_bulletin_class(BulletinGeneratorLegacy)
|
2021-08-29 19:57:32 +02:00
|
|
|
sco_bulletins_generator.register_bulletin_class(BulletinGeneratorUCAC)
|
2021-09-29 14:47:43 +02:00
|
|
|
if app.testing or app.debug:
|
|
|
|
from app.scodoc.sco_bulletins_example import BulletinGeneratorExample
|
|
|
|
|
|
|
|
sco_bulletins_generator.register_bulletin_class(BulletinGeneratorExample)
|
2021-09-09 08:03:43 +02:00
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
from app.auth.cas import set_cas_configuration
|
|
|
|
|
|
|
|
with app.app_context():
|
2023-02-27 10:43:50 +01:00
|
|
|
try:
|
|
|
|
set_cas_configuration(app)
|
2023-04-03 17:40:45 +02:00
|
|
|
except sa.exc.ProgrammingError:
|
2023-02-27 10:43:50 +01:00
|
|
|
# Si la base n'a pas été upgradée (arrive durrant l'install)
|
|
|
|
# il se peut que la table scodoc_site_config n'existe pas encore.
|
|
|
|
pass
|
2021-05-29 18:22:51 +02:00
|
|
|
return app
|
2021-08-09 23:23:11 +02:00
|
|
|
|
|
|
|
|
2022-07-26 09:00:48 +02:00
|
|
|
def set_sco_dept(scodoc_dept: str, open_cnx=True):
|
2021-08-13 00:34:58 +02:00
|
|
|
"""Set global g object to given dept and open db connection if needed"""
|
|
|
|
# Check that dept exists
|
2021-08-30 11:03:24 +02:00
|
|
|
try:
|
|
|
|
dept = Departement.query.filter_by(acronym=scodoc_dept).first()
|
2023-04-03 17:40:45 +02:00
|
|
|
except sa.exc.OperationalError:
|
2021-08-30 11:03:24 +02:00
|
|
|
abort(503)
|
2021-08-13 00:34:58 +02:00
|
|
|
if not dept:
|
|
|
|
raise ScoValueError(f"Invalid dept: {scodoc_dept}")
|
|
|
|
g.scodoc_dept = scodoc_dept # l'acronyme
|
|
|
|
g.scodoc_dept_id = dept.id # l'id
|
2022-07-26 09:00:48 +02:00
|
|
|
if open_cnx and not hasattr(g, "db_conn"):
|
2021-08-13 00:34:58 +02:00
|
|
|
ndb.open_db_connection()
|
2021-10-16 19:20:36 +02:00
|
|
|
if not hasattr(g, "stored_get_formsemestre"):
|
|
|
|
g.stored_get_formsemestre = {}
|
2021-08-13 00:34:58 +02:00
|
|
|
|
|
|
|
|
2021-08-09 23:23:11 +02:00
|
|
|
def user_db_init():
|
2021-08-27 17:03:47 +02:00
|
|
|
"""Initialize the users database.
|
|
|
|
Check that basic roles and admin user exist.
|
|
|
|
"""
|
2021-08-09 23:23:11 +02:00
|
|
|
from app.auth.models import User, Role
|
|
|
|
|
|
|
|
current_app.logger.info("Init User's db")
|
|
|
|
# Create roles:
|
2022-03-21 22:07:34 +01:00
|
|
|
Role.reset_standard_roles_permissions()
|
2021-08-09 23:23:11 +02:00
|
|
|
current_app.logger.info("created initial roles")
|
|
|
|
# Ensure that admin exists
|
|
|
|
admin_mail = current_app.config.get("SCODOC_ADMIN_MAIL")
|
|
|
|
if admin_mail:
|
|
|
|
admin_user_name = current_app.config["SCODOC_ADMIN_LOGIN"]
|
|
|
|
user = User.query.filter_by(user_name=admin_user_name).first()
|
|
|
|
if not user:
|
|
|
|
user = User(user_name=admin_user_name, email=admin_mail)
|
|
|
|
try:
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
except:
|
|
|
|
db.session.rollback()
|
|
|
|
raise
|
|
|
|
current_app.logger.info(
|
|
|
|
"created initial admin user, login: {u.user_name}, email: {u.email}".format(
|
|
|
|
u=user
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-27 17:03:47 +02:00
|
|
|
def sco_db_insert_constants():
|
|
|
|
"""Initialize Sco database: insert some constants (modalités, ...)."""
|
2021-08-09 23:23:11 +02:00
|
|
|
from app import models
|
|
|
|
|
|
|
|
current_app.logger.info("Init Sco db")
|
|
|
|
# Modalités:
|
2021-11-12 22:17:46 +01:00
|
|
|
models.FormationModalite.insert_modalites()
|
2021-08-09 23:23:11 +02:00
|
|
|
|
|
|
|
|
2021-08-27 17:03:47 +02:00
|
|
|
def initialize_scodoc_database(erase=False, create_all=False):
|
|
|
|
"""Initialize the database for unit tests
|
2021-08-09 23:23:11 +02:00
|
|
|
Starts from an existing database and create all necessary
|
|
|
|
SQL tables and functions.
|
|
|
|
If erase is True, _erase_ all database content.
|
|
|
|
"""
|
|
|
|
# - ERASE (the truncation sql function has been defined above)
|
|
|
|
if erase:
|
|
|
|
truncate_database()
|
|
|
|
# - Create all tables
|
2021-08-27 17:03:47 +02:00
|
|
|
if create_all:
|
|
|
|
# managed by migrations, except for TESTS
|
|
|
|
db.create_all()
|
2021-08-09 23:23:11 +02:00
|
|
|
# - Insert initial roles and create super-admin user
|
|
|
|
user_db_init()
|
|
|
|
# - Insert some constant values (modalites, ...)
|
2021-08-27 17:03:47 +02:00
|
|
|
sco_db_insert_constants()
|
2021-08-10 12:57:38 +02:00
|
|
|
# - Flush cache
|
|
|
|
clear_scodoc_cache()
|
2021-08-09 23:23:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def truncate_database():
|
|
|
|
"""Erase content of all tables (including users !) from
|
|
|
|
the current database.
|
|
|
|
"""
|
|
|
|
# use a stored SQL function, see createtables.sql
|
|
|
|
try:
|
2023-04-03 17:40:45 +02:00
|
|
|
db.session.execute(sa.text("SELECT truncate_tables('scodoc');"))
|
2021-08-09 23:23:11 +02:00
|
|
|
db.session.commit()
|
|
|
|
except:
|
|
|
|
db.session.rollback()
|
|
|
|
raise
|
2022-12-17 03:26:22 +01:00
|
|
|
# Remet les compteurs (séquences sql) à zéro
|
|
|
|
db.session.execute(
|
2023-04-03 17:40:45 +02:00
|
|
|
sa.text(
|
|
|
|
"""
|
2022-12-17 03:26:22 +01:00
|
|
|
CREATE OR REPLACE FUNCTION reset_sequences(username IN VARCHAR) RETURNS void AS $$
|
|
|
|
DECLARE
|
|
|
|
statements CURSOR FOR
|
2023-11-15 20:38:51 +01:00
|
|
|
SELECT sequence_name
|
|
|
|
FROM information_schema.sequences
|
2022-12-17 03:26:22 +01:00
|
|
|
ORDER BY sequence_name ;
|
|
|
|
BEGIN
|
|
|
|
FOR stmt IN statements LOOP
|
|
|
|
EXECUTE 'ALTER SEQUENCE ' || quote_ident(stmt.sequence_name) || ' RESTART;';
|
|
|
|
END LOOP;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
SELECT reset_sequences('scodoc');
|
|
|
|
"""
|
2023-04-03 17:40:45 +02:00
|
|
|
)
|
2022-12-17 03:26:22 +01:00
|
|
|
)
|
|
|
|
db.session.commit()
|
2021-08-09 23:23:11 +02:00
|
|
|
|
|
|
|
|
2021-08-10 12:57:38 +02:00
|
|
|
def clear_scodoc_cache():
|
|
|
|
"""Clear ScoDoc cache
|
|
|
|
This cache (currently Redis) is persistent between invocation
|
|
|
|
and it may be necessary to clear it during developement or tests.
|
|
|
|
"""
|
|
|
|
# attaque directement redis, court-circuite ScoDoc:
|
|
|
|
import redis
|
|
|
|
|
2024-06-03 15:14:27 +02:00
|
|
|
r = redis.Redis(host=(os.environ.get("CACHE_REDIS_HOST") or "localhost"))
|
2021-08-10 12:57:38 +02:00
|
|
|
r.flushall()
|
2021-08-13 00:34:58 +02:00
|
|
|
# Also clear local caches:
|
|
|
|
sco_preferences.clear_base_preferences()
|
|
|
|
|
2021-08-10 12:57:38 +02:00
|
|
|
|
2021-08-29 19:57:32 +02:00
|
|
|
# --------- Logging
|
2023-01-11 20:39:58 +01:00
|
|
|
def log(msg: str):
|
2021-08-29 19:57:32 +02:00
|
|
|
"""log a message.
|
2021-09-05 12:30:11 +02:00
|
|
|
If Flask app, use configured logger, else stderr.
|
|
|
|
"""
|
2021-08-29 19:57:32 +02:00
|
|
|
try:
|
|
|
|
dept = getattr(g, "scodoc_dept", "")
|
|
|
|
msg = f" ({dept}) {msg}"
|
|
|
|
except RuntimeError:
|
|
|
|
# Flask Working outside of application context.
|
|
|
|
pass
|
|
|
|
|
2022-09-07 13:06:26 +02:00
|
|
|
if current_app and not current_app.config["DEBUG"]:
|
2021-08-29 19:57:32 +02:00
|
|
|
current_app.logger.info(msg)
|
|
|
|
else:
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.write(
|
2022-09-07 13:06:26 +02:00
|
|
|
f"""[{time.strftime("%a %b %d %H:%M:%S %Y")}] scodoc: {msg}\n"""
|
2021-08-29 19:57:32 +02:00
|
|
|
)
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
|
|
|
|
# Debug: log call stack
|
|
|
|
def log_call_stack():
|
|
|
|
log("Call stack:\n" + "\n".join(x.strip() for x in traceback.format_stack()[:-1]))
|
|
|
|
|
|
|
|
|
|
|
|
# Alarms by email:
|
|
|
|
def send_scodoc_alarm(subject, txt):
|
|
|
|
from app import email
|
|
|
|
|
2023-02-28 19:43:48 +01:00
|
|
|
sender = email.get_from_addr()
|
2021-08-29 19:57:32 +02:00
|
|
|
email.send_email(subject, sender, ["exception@scodoc.org"], txt)
|
|
|
|
|
|
|
|
|
2021-08-13 00:34:58 +02:00
|
|
|
from app.models import Departement
|
|
|
|
from app.scodoc import notesdb as ndb, sco_preferences
|
|
|
|
from app.scodoc import sco_cache
|
2021-08-10 12:57:38 +02:00
|
|
|
|
2022-03-04 20:02:50 +01:00
|
|
|
|
|
|
|
def scodoc_flash_status_messages():
|
|
|
|
"""Should be called on each page: flash messages indicating specific ScoDoc status"""
|
|
|
|
email_test_mode_address = sco_preferences.get_preference("email_test_mode_address")
|
|
|
|
if email_test_mode_address:
|
|
|
|
flash(
|
|
|
|
f"Mode test: mails redirigés vers {email_test_mode_address}",
|
|
|
|
category="warning",
|
|
|
|
)
|
2023-01-23 23:03:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def critical_error(msg):
|
|
|
|
"""Handle a critical error: flush all caches, display message to the user"""
|
|
|
|
import app.scodoc.sco_utils as scu
|
|
|
|
|
|
|
|
log(f"\n*** CRITICAL ERROR: {msg}")
|
2024-05-19 22:53:54 +02:00
|
|
|
subject = f"CRITICAL ERROR: {msg}".strip()[:68]
|
|
|
|
send_scodoc_alarm(subject, msg)
|
2023-01-23 23:03:20 +01:00
|
|
|
clear_scodoc_cache()
|
|
|
|
raise ScoValueError(
|
|
|
|
f"""
|
2024-05-19 22:53:54 +02:00
|
|
|
Une erreur est survenue, veuillez ré-essayer.
|
2023-01-24 00:35:15 +01:00
|
|
|
|
2023-01-23 23:03:20 +01:00
|
|
|
{msg}
|
|
|
|
"""
|
|
|
|
)
|