Fix assiduite migration

This commit is contained in:
Emmanuel Viennet 2023-07-18 08:35:18 +02:00
parent aee58edab1
commit d86681b268
2 changed files with 33 additions and 27 deletions

View File

@ -672,6 +672,13 @@ def migrate_abs_to_assiduites(
): # migrate-abs-to-assiduites
"""Permet de migrer les absences vers le nouveau module d'assiduités"""
tools.migrate_abs_to_assiduites(dept, morning, noon, evening)
# import cProfile
# cProfile.runctx(
# f"tools.migrate_abs_to_assiduites({dept})",
# {"tools": tools},
# {},
# "migration-nimes",
# )
@app.cli.command()

View File

@ -32,21 +32,17 @@ from app.scodoc.sco_utils import (
)
class _Merger:
"""pour typage"""
class _glob:
"""variables globales du script"""
DEBUG: bool = False
PROBLEMS: dict[int, list[str]] = {}
CURRENT_ETU: list = []
MODULES: list[tuple[int, int]] = []
DEPT_ETUDIDS: dict[int, Identite] = {}
MODULES: dict[tuple[int, int]] = {}
COMPTE: list[int, int] = []
ERR_ETU: list[int] = []
MERGER_ASSI: _Merger = None
MERGER_JUST: _Merger = None
MERGER_ASSI: "_Merger" = None
MERGER_JUST: "_Merger" = None
MORNING: time = None
NOON: time = None
@ -64,7 +60,9 @@ class _Merger:
self.entry_date = abs_.entry_date
def merge(self, abs_: Absence) -> bool:
"""Fusionne les absences"""
"""Fusionne les absences.
Return False si pas de fusion.
"""
if self.etudid != abs_.etudid:
return False
@ -218,7 +216,7 @@ def migrate_abs_to_assiduites(
.matin: bool (0:00 -> time_pref | time_pref->23:59:59)
.jour : date (jour de l'absence/justificatif)
.moduleimpl_id: relation -> moduleimpl_id
description:str -> motif abs / raision justif
description:str -> motif abs / raison justif
.entry_date: datetime -> timestamp d'entrée de l'abs
.etudid: relation -> Identite
@ -272,7 +270,7 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
dept: Departement = Departement.query.filter_by(acronym=dept_name).first()
if dept is None:
return
raise ValueError(f"Département inexistant: {dept_name}")
etuds_id: list[int] = [etud.id for etud in dept.etudiants]
absences_query = absences_query.filter(Absence.etudid.in_(etuds_id))
@ -288,8 +286,8 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
)
return
_glob.CURRENT_ETU = []
_glob.MODULES = []
_glob.DEPT_ETUDIDS = {e.id for e in Identite.query.filter_by(dept_id=dept.id)}
_glob.MODULES = {}
_glob.COMPTE = [0, 0]
_glob.ERR_ETU = []
_glob.MERGER_ASSI = None
@ -326,8 +324,10 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
)
db.session.commit()
_glob.MERGER_ASSI.export()
_glob.MERGER_JUST.export()
if _glob.MERGER_ASSI is not None:
_glob.MERGER_ASSI.export()
if _glob.MERGER_JUST is not None:
_glob.MERGER_JUST.export()
db.session.commit()
@ -359,14 +359,15 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
f"{TerminalColor.GREEN}La migration a pris {time_elapsed.elapsed():.2f} secondes {TerminalColor.RESET}"
)
print(
f"{TerminalColor.RED}{statistiques['total']} absences qui n'ont pas pu être migrées."
)
filename = f"/opt/scodoc-data/log/{datetime.now().strftime('%Y-%m-%dT%H:%M:%S')}scodoc_migration_abs_{dept_name}.json"
print(
f"Vous retrouverez un fichier json {TerminalColor.GREEN}{filename}{TerminalColor.RED} contenant les problèmes de migrations"
)
if statistiques["total"] > 0:
print(
f"{TerminalColor.RED}{statistiques['total']} absences qui n'ont pas pu être migrées."
)
print(
f"Vous retrouverez un fichier json {TerminalColor.GREEN}{filename}{TerminalColor.RED} contenant les problèmes de migrations"
)
with open(
filename,
"w",
@ -383,11 +384,8 @@ def migrate_dept(dept_name: str, stats: _Statistics, time_elapsed: Profiler):
def _from_abs_to_assiduite_justificatif(_abs: Absence):
if _abs.etudid not in _glob.CURRENT_ETU:
etud: Identite = Identite.query.filter_by(id=_abs.etudid).first()
if etud is None:
raise ValueError("Etudiant inexistant")
_glob.CURRENT_ETU.append(_abs.etudid)
if _abs.etudid not in _glob.DEPT_ETUDIDS:
raise ValueError("Etudiant inexistant")
if _abs.estabs:
moduleimpl_id: int = _abs.moduleimpl_id
@ -402,6 +400,7 @@ def _from_abs_to_assiduite_justificatif(_abs: Absence):
)
if moduleimpl_inscription is None:
raise ValueError("Moduleimpl_id incorrect ou étudiant non inscrit")
_glob.MODULES[(_abs.etudid, _abs.moduleimpl_id)] = True
if _glob.MERGER_ASSI is None:
_glob.MERGER_ASSI = _Merger(_abs, True)