ScoDoc/app/views/__init__.py

104 lines
3.7 KiB
Python
Raw Normal View History

2021-05-29 18:22:51 +02:00
# -*- coding: UTF-8 -*
"""ScoDoc Flask views
"""
import datetime
2021-05-29 18:22:51 +02:00
from flask import Blueprint
2021-12-04 21:06:32 +01:00
from flask import g, current_app, request
from flask_login import current_user
from app import db
2021-12-04 21:06:32 +01:00
from app.models import Identite
from app.models.formsemestre import FormSemestre
from app.scodoc import notesdb as ndb
2021-12-04 21:06:32 +01:00
from app.scodoc import sco_abs
from app.scodoc import sco_formsemestre_status
from app.scodoc import sco_preferences
from app.scodoc.sco_permissions import Permission
from app.scodoc import sco_utils as scu
import sco_version
2021-05-29 18:22:51 +02:00
2021-07-04 12:32:13 +02:00
scodoc_bp = Blueprint("scodoc", __name__)
scolar_bp = Blueprint("scolar", __name__)
2021-05-29 18:22:51 +02:00
notes_bp = Blueprint("notes", __name__)
users_bp = Blueprint("users", __name__)
absences_bp = Blueprint("absences", __name__)
2021-05-29 18:22:51 +02:00
2021-08-13 00:34:58 +02:00
# Cette fonction est bien appelée avant toutes les requêtes
# de tous les blueprints
# mais apparemment elle n'a pas acces aux arguments
@scodoc_bp.before_app_request
2021-08-13 00:34:58 +02:00
def start_scodoc_request():
"""Affecte toutes les requêtes, de tous les blueprints"""
# current_app.logger.info(f"start_scodoc_request")
2021-08-13 00:34:58 +02:00
ndb.open_db_connection()
2021-10-16 19:20:36 +02:00
if current_user and current_user.is_authenticated:
current_user.last_seen = datetime.datetime.utcnow()
db.session.commit()
# caches locaux (durée de vie=la requête en cours)
g.stored_get_formsemestre = {}
# g.stored_etud_info = {} optim en cours, voir si utile
@scodoc_bp.teardown_app_request
def close_dept_db_connection(arg):
2021-08-13 00:34:58 +02:00
# current_app.logger.info("close_db_connection")
ndb.close_db_connection()
2021-12-04 21:06:32 +01:00
class ScoData:
"""Classe utilisée pour passer des valeurs aux vues (templates)"""
2022-03-07 21:49:11 +01:00
def __init__(self, etud=None, formsemestre=None):
2021-12-04 21:06:32 +01:00
# Champs utilisés par toutes les pages ScoDoc (sidebar, en-tête)
self.Permission = Permission
self.scu = scu
self.SCOVERSION = sco_version.SCOVERSION
2021-12-04 21:06:32 +01:00
# -- Informations étudiant courant, si sélectionné:
2022-03-07 21:49:11 +01:00
if etud is None:
etudid = g.get("etudid", None)
if etudid is None:
if request.method == "GET":
etudid = request.args.get("etudid", None)
elif request.method == "POST":
etudid = request.form.get("etudid", None)
if etudid is not None:
etud = Identite.query.get_or_404(etudid)
self.etud = etud
if etud is not None:
2021-12-04 21:06:32 +01:00
# Infos sur l'étudiant courant
ins = self.etud.inscription_courante()
if ins:
self.etud_cur_sem = ins.formsemestre
self.nbabs, self.nbabsjust = sco_abs.get_abs_count_in_interval(
2022-03-07 21:49:11 +01:00
etud.id,
2021-12-04 21:06:32 +01:00
self.etud_cur_sem.date_debut.isoformat(),
self.etud_cur_sem.date_fin.isoformat(),
)
self.nbabsnj = self.nbabs - self.nbabsjust
else:
self.etud_cur_sem = None
else:
self.etud = None
# --- Informations sur semestre courant, si sélectionné
2022-03-07 21:49:11 +01:00
if formsemestre is None:
formsemestre_id = (
sco_formsemestre_status.retreive_formsemestre_from_request()
)
if formsemestre_id is not None:
formsemestre = FormSemestre.query.get_or_404(formsemestre_id)
if formsemestre is None:
2021-12-04 21:06:32 +01:00
self.sem = None
self.sem_menu_bar = None
else:
2022-03-07 21:49:11 +01:00
self.sem = formsemestre
2021-12-04 21:06:32 +01:00
self.sem_menu_bar = sco_formsemestre_status.formsemestre_status_menubar(
self.sem.to_dict()
)
# --- Préférences
2022-03-07 21:49:11 +01:00
self.prefs = sco_preferences.SemPreferences(formsemestre.id)
2021-12-04 21:06:32 +01:00
from app.views import scodoc, notes, scolar, absences, users, pn_modules, refcomp