diff --git a/app/comp/res_common.py b/app/comp/res_common.py index d43f597ec..2298842f8 100644 --- a/app/comp/res_common.py +++ b/app/comp/res_common.py @@ -926,7 +926,7 @@ class ResultatsSemestre(ResultatsCache): group = None # group (dict) de l'étudiant dans cette partition # dans NotesTableCompat, à revoir etud_etat = self.get_etud_etat(row["etudid"]) - if etud_etat == "D": + if etud_etat == scu.DEMISSION: gr_name = "Dém." row["_tr_class"] = "dem" elif etud_etat == DEF: diff --git a/app/models/validations.py b/app/models/validations.py index b24685a87..fb470b974 100644 --- a/app/models/validations.py +++ b/app/models/validations.py @@ -156,8 +156,16 @@ class ScolarEvent(db.Model): db.Integer, db.ForeignKey("notes_formsemestre.id"), ) + etud = db.relationship("Identite", lazy="select", backref="events", uselist=False) + formsemestre = db.relationship( + "FormSemestre", lazy="select", uselist=False, foreign_keys=[formsemestre_id] + ) def to_dict(self) -> dict: + "as a dict" d = dict(self.__dict__) d.pop("_sa_instance_state", None) return d + + def __repr__(self) -> str: + return f"{self.__class__.__name__}({self.event_type}, {self.event_date.isoformat()}, {self.formsemestre})" diff --git a/app/pe/pe_semestretag.py b/app/pe/pe_semestretag.py index 0a7adcaba..bed334656 100644 --- a/app/pe/pe_semestretag.py +++ b/app/pe/pe_semestretag.py @@ -41,10 +41,11 @@ from app.comp import res_sem from app.comp.res_compat import NotesTableCompat from app.models import FormSemestre from app.models.moduleimpls import ModuleImpl +from app.pe import pe_tagtable from app.scodoc import sco_codes_parcours from app.scodoc import sco_tag_module -from app.pe import pe_tagtable +from app.scodoc import sco_utils as scu class SemestreTag(pe_tagtable.TableTag): @@ -103,7 +104,7 @@ class SemestreTag(pe_tagtable.TableTag): self.inscrlist = [ etud for etud in self.nt.inscrlist - if self.nt.get_etud_etat(etud["etudid"]) == "I" + if self.nt.get_etud_etat(etud["etudid"]) == scu.INSCRIT ] self.identdict = { etudid: ident diff --git a/app/scodoc/notes_table.py b/app/scodoc/notes_table.py index 56f2b93e3..a8322f166 100644 --- a/app/scodoc/notes_table.py +++ b/app/scodoc/notes_table.py @@ -411,9 +411,9 @@ class NotesTable: def get_etud_etat_html(self, etudid): etat = self.inscrdict[etudid]["etat"] - if etat == "I": + if etat == scu.INSCRIT: return "" - elif etat == "D": + elif etat == scu.DEMISSION: return ' (DEMISSIONNAIRE) ' elif etat == DEF: return ' (DEFAILLANT) ' @@ -465,7 +465,7 @@ class NotesTable: vals = [] for etudid in self.get_etudids(): # saute les demissionnaires et les défaillants: - if self.inscrdict[etudid]["etat"] != "I": + if self.inscrdict[etudid]["etat"] != scu.INSCRIT: continue val = moys.get(etudid, None) # None si non inscrit try: @@ -507,8 +507,8 @@ class NotesTable: for t in T: etudid = t[-1] # saute les demissionnaires et les défaillants: - if self.inscrdict[etudid]["etat"] != "I": - if self.inscrdict[etudid]["etat"] == "D": + if self.inscrdict[etudid]["etat"] != scu.INSCRIT: + if self.inscrdict[etudid]["etat"] == scu.DEMISSION: nb_dem += 1 if self.inscrdict[etudid]["etat"] == DEF: nb_def += 1 diff --git a/app/scodoc/sco_bulletins.py b/app/scodoc/sco_bulletins.py index 227349328..ae2cc60dd 100644 --- a/app/scodoc/sco_bulletins.py +++ b/app/scodoc/sco_bulletins.py @@ -435,11 +435,11 @@ def get_appreciations_list(formsemestre_id: int, etudid: int) -> dict: def _get_etud_etat_html(etat: str) -> str: """chaine html représentant l'état (backward compat sco7)""" - if etat == scu.INSCRIT: # "I" + if etat == scu.INSCRIT: return "" - elif etat == scu.DEMISSION: # "D" + elif etat == scu.DEMISSION: return ' (DEMISSIONNAIRE) ' - elif etat == scu.DEF: # "DEF" + elif etat == scu.DEF: return ' (DEFAILLANT) ' else: return f' ({etat}) ' diff --git a/app/scodoc/sco_cursus_dut.py b/app/scodoc/sco_cursus_dut.py index f4354c6f1..32d4796b9 100644 --- a/app/scodoc/sco_cursus_dut.py +++ b/app/scodoc/sco_cursus_dut.py @@ -360,7 +360,7 @@ class SituationEtudCursusClassic(SituationEtudCursus): cur_begin_date = self.sem["dateord"] p = [] for s in self.sems: - if s["ins"]["etat"] == "D": + if s["ins"]["etat"] == scu.DEMISSION: dem = " (dem.)" else: dem = "" diff --git a/app/scodoc/sco_etud.py b/app/scodoc/sco_etud.py index 096cc8490..bb869d5a9 100644 --- a/app/scodoc/sco_etud.py +++ b/app/scodoc/sco_etud.py @@ -1010,7 +1010,7 @@ def descr_situation_etud(etudid: int, ne="") -> str: situation = "non inscrit" + ne else: sem = sco_formsemestre.get_formsemestre(r["formsemestre_id"]) - if r["etat"] == "I": + if r["etat"] == scu.INSCRIT: situation = "inscrit%s en %s" % (ne, sem["titremois"]) # Cherche la date d'inscription dans scolar_events: events = scolar_events_list( diff --git a/app/scodoc/sco_formsemestre_exterieurs.py b/app/scodoc/sco_formsemestre_exterieurs.py index baec914d8..c33b7309f 100644 --- a/app/scodoc/sco_formsemestre_exterieurs.py +++ b/app/scodoc/sco_formsemestre_exterieurs.py @@ -117,7 +117,7 @@ def formsemestre_ext_create_form(etudid, formsemestre_id): # Les autres situations (eg redoublements en changeant d'établissement) # doivent être gérées par les validations de semestres "antérieurs" insem = sco_formsemestre_inscriptions.do_formsemestre_inscription_list( - args={"etudid": etudid, "etat": "I"} + args={"etudid": etudid, "etat": scu.INSCRIT} ) semlist = [sco_formsemestre.get_formsemestre(i["formsemestre_id"]) for i in insem] existing_semestre_ids = {s["semestre_id"] for s in semlist} diff --git a/app/scodoc/sco_formsemestre_inscriptions.py b/app/scodoc/sco_formsemestre_inscriptions.py index 8236f2ab7..540282e9c 100644 --- a/app/scodoc/sco_formsemestre_inscriptions.py +++ b/app/scodoc/sco_formsemestre_inscriptions.py @@ -32,11 +32,13 @@ import time import flask from flask import flash, url_for, g, request +from app import db from app.comp import res_sem from app.comp.res_compat import NotesTableCompat -from app.models import Formation, FormSemestre, FormSemestreInscription +from app.models import Formation, FormSemestre, FormSemestreInscription, Scolog from app.models.etudiants import Identite from app.models.groups import GroupDescr +from app.models.validations import ScolarEvent import app.scodoc.sco_utils as scu from app import log from app.scodoc.scolog import logdb @@ -77,7 +79,7 @@ def do_formsemestre_inscription_listinscrits(formsemestre_id): if r is None: # retreive list r = do_formsemestre_inscription_list( - args={"formsemestre_id": formsemestre_id, "etat": "I"} + args={"formsemestre_id": formsemestre_id, "etat": scu.INSCRIT} ) sco_cache.SemInscriptionsCache.set(formsemestre_id, r) return r @@ -133,36 +135,51 @@ def do_formsemestre_demission( etudid, formsemestre_id, event_date=None, - etat_new="D", # 'D' or DEF - operation_method="demEtudiant", + etat_new=scu.DEMISSION, # DEMISSION or DEF + operation_method="dem_etudiant", event_type="DEMISSION", ): "Démission ou défaillance d'un étudiant" # marque 'D' ou DEF dans l'inscription au semestre et ajoute # un "evenement" scolarite - cnx = ndb.GetDBConnexion() + if etat_new not in (scu.DEF, scu.DEMISSION): + raise ScoValueError("nouveau code d'état invalide") + try: + event_date_iso = ndb.DateDMYtoISO(event_date) + except ValueError as exc: + raise ScoValueError("format de date invalide") from exc + etud: Identite = Identite.query.filter_by( + id=etudid, dept_id=g.scodoc_dept_id + ).first_or_404() # check lock - sem = sco_formsemestre.get_formsemestre(formsemestre_id) - if not sem["etat"]: + formsemestre: FormSemestre = FormSemestre.query.filter_by( + id=formsemestre_id, dept_id=g.scodoc_dept_id + ).first_or_404() + if not formsemestre.etat: raise ScoValueError("Modification impossible: semestre verrouille") # - ins = do_formsemestre_inscription_list( - {"etudid": etudid, "formsemestre_id": formsemestre_id} - )[0] - if not ins: - raise ScoException("etudiant non inscrit ?!") - ins["etat"] = etat_new - do_formsemestre_inscription_edit(args=ins, formsemestre_id=formsemestre_id) - logdb(cnx, method=operation_method, etudid=etudid) - sco_etud.scolar_events_create( - cnx, - args={ - "etudid": etudid, - "event_date": event_date, - "formsemestre_id": formsemestre_id, - "event_type": event_type, - }, + if formsemestre_id not in (inscr.formsemestre_id for inscr in etud.inscriptions()): + raise ScoValueError("étudiant non inscrit dans ce semestre !") + inscr = next( + inscr + for inscr in etud.inscriptions() + if inscr.formsemestre_id == formsemestre_id ) + inscr.etat = etat_new + db.session.add(inscr) + Scolog.logdb(method=operation_method, etudid=etudid) + event = ScolarEvent( + etudid=etudid, + event_date=event_date_iso, + formsemestre_id=formsemestre_id, + event_type=event_type, + ) + db.session.add(event) + db.session.commit() + if etat_new == scu.DEMISSION: + flash("Démission enregistrée") + elif etat_new == scu.DEF: + flash("Défaillance enregistrée") def do_formsemestre_inscription_edit(args=None, formsemestre_id=None): @@ -250,7 +267,7 @@ def do_formsemestre_inscription_with_modules( formsemestre_id, etudid, group_ids=[], - etat="I", + etat=scu.INSCRIT, etape=None, method="inscription_with_modules", ): @@ -467,7 +484,7 @@ def formsemestre_inscription_with_modules( formsemestre_id, etudid, group_ids=group_ids, - etat="I", + etat=scu.INSCRIT, method="formsemestre_inscription_with_modules", ) return flask.redirect( diff --git a/app/scodoc/sco_formsemestre_validation.py b/app/scodoc/sco_formsemestre_validation.py index 177b97581..f129a36a2 100644 --- a/app/scodoc/sco_formsemestre_validation.py +++ b/app/scodoc/sco_formsemestre_validation.py @@ -191,11 +191,11 @@ def formsemestre_validation_etud_form( ) etud_etat = nt.get_etud_etat(etudid) - if etud_etat == "D": + if etud_etat == scu.DEMISSION: H.append('
Confirmer la suppression de la photo de {etud.nom_disp()} ?
", @@ -1057,99 +1058,91 @@ def formSuppressPhoto(etudid=None, dialog_confirmed=False): # -@bp.route("/formDem") +@bp.route("/form_dem") @scodoc @permission_required(Permission.ScoEtudInscrit) @scodoc7func -def formDem(etudid, formsemestre_id): +def form_dem(etudid, formsemestre_id): "Formulaire Démission Etudiant" - return _formDem_of_Def( + return _form_dem_of_def( etudid, formsemestre_id, operation_name="Démission", - operation_method="doDemEtudiant", + operation_method="do_dem_etudiant", ) -@bp.route("/formDef") +@bp.route("/form_def") @scodoc @permission_required(Permission.ScoEtudInscrit) @scodoc7func -def formDef(etudid, formsemestre_id): +def form_def(etudid, formsemestre_id): "Formulaire Défaillance Etudiant" - return _formDem_of_Def( + return _form_dem_of_def( etudid, formsemestre_id, operation_name="Défaillance", - operation_method="doDefEtudiant", + operation_method="do_def_etudiant", ) -def _formDem_of_Def( - etudid, - formsemestre_id, - operation_name="", - operation_method="", +def _form_dem_of_def( + etudid: int, + formsemestre_id: int, + operation_name: str = "", + operation_method: str = "", ): "Formulaire démission ou défaillance Etudiant" - etud = sco_etud.get_etud_info(filled=True, etudid=etudid)[0] - sem = sco_formsemestre.get_formsemestre(formsemestre_id) - if not sem["etat"]: + etud: Identite = Identite.query.filter_by( + id=etudid, dept_id=g.scodoc_dept_id + ).first_or_404() + formsemestre: FormSemestre = FormSemestre.query.filter_by( + id=formsemestre_id, dept_id=g.scodoc_dept_id + ).first_or_404() + if not formsemestre.etat: raise ScoValueError("Modification impossible: semestre verrouille") - - etud["formsemestre_id"] = formsemestre_id - etud["semtitre"] = sem["titremois"] - etud["nowdmy"] = time.strftime("%d/%m/%Y") - etud["operation_name"] = operation_name + nowdmy = time.strftime("%d/%m/%Y") # header = html_sco_header.sco_header( - page_title="%(operation_name)s de %(nomprenom)s (du semestre %(semtitre)s)" - % etud, + page_title=f"""{operation_name} de {etud.nomprenom} (du semestre {formsemestre.titre_mois()})""" ) - H = [ - '' - % etud - ] - H.append( - """
+ {html_sco_header.sco_footer()} """ - % (operation_method, operation_name.lower()) - ) - H.append( - """ - - - -- -""" - % etud - ) - return header + "\n".join(H) + html_sco_header.sco_footer() -@bp.route("/doDemEtudiant") +@bp.route("/do_dem_etudiant") @scodoc @permission_required(Permission.ScoEtudInscrit) @scodoc7func -def doDemEtudiant(etudid, formsemestre_id, event_date=None): +def do_dem_etudiant(etudid, formsemestre_id, event_date=None): "Déclare la démission d'un etudiant dans le semestre" return _do_dem_or_def_etud( etudid, formsemestre_id, event_date=event_date, - etat_new="D", - operation_method="demEtudiant", + etat_new=scu.DEMISSION, + operation_method="dem_etudiant", event_type="DEMISSION", ) -@bp.route("/doDefEtudiant") +@bp.route("/do_def_etudiant") @scodoc @permission_required(Permission.ScoEtudInscrit) @scodoc7func -def doDefEtudiant(etudid, formsemestre_id, event_date=None): +def do_def_etudiant(etudid, formsemestre_id, event_date=None): "Déclare la défaillance d'un etudiant dans le semestre" return _do_dem_or_def_etud( etudid, @@ -1165,7 +1158,7 @@ def _do_dem_or_def_etud( etudid, formsemestre_id, event_date=None, - etat_new="D", # 'D' or DEF + etat_new=scu.DEMISSION, # DEMISSION or DEF operation_method="demEtudiant", event_type="DEMISSION", redirect=True, @@ -1175,7 +1168,7 @@ def _do_dem_or_def_etud( etudid, formsemestre_id, event_date=event_date, - etat_new=etat_new, # 'D' or DEF + etat_new=etat_new, # DEMISSION or DEF operation_method=operation_method, event_type=event_type, ) @@ -1185,11 +1178,11 @@ def _do_dem_or_def_etud( ) -@bp.route("/doCancelDem", methods=["GET", "POST"]) +@bp.route("/do_cancel_dem", methods=["GET", "POST"]) @scodoc @permission_required(Permission.ScoEtudInscrit) @scodoc7func -def doCancelDem(etudid, formsemestre_id, dialog_confirmed=False, args=None): +def do_cancel_dem(etudid, formsemestre_id, dialog_confirmed=False, args=None): "Annule une démission" return _do_cancel_dem_or_def( etudid, @@ -1197,18 +1190,18 @@ def doCancelDem(etudid, formsemestre_id, dialog_confirmed=False, args=None): dialog_confirmed=dialog_confirmed, args=args, operation_name="démission", - etat_current="D", - etat_new="I", + etat_current=scu.DEMISSION, + etat_new=scu.INSCRIT, operation_method="cancelDem", event_type="DEMISSION", ) -@bp.route("/doCancelDef", methods=["GET", "POST"]) +@bp.route("/do_cancel_def", methods=["GET", "POST"]) @scodoc @permission_required(Permission.ScoEtudInscrit) @scodoc7func -def doCancelDef(etudid, formsemestre_id, dialog_confirmed=False, args=None): +def do_cancel_def(etudid, formsemestre_id, dialog_confirmed=False, args=None): "Annule la défaillance de l'étudiant" return _do_cancel_dem_or_def( etudid, @@ -1217,8 +1210,8 @@ def doCancelDef(etudid, formsemestre_id, dialog_confirmed=False, args=None): args=args, operation_name="défaillance", etat_current=sco_codes_parcours.DEF, - etat_new="I", - operation_method="cancelDef", + etat_new=scu.INSCRIT, + operation_method="cancel_def", event_type="DEFAILLANCE", ) @@ -1229,30 +1222,30 @@ def _do_cancel_dem_or_def( dialog_confirmed=False, args=None, operation_name="", # "démission" ou "défaillance" - etat_current="D", - etat_new="I", - operation_method="cancelDem", + etat_current=scu.DEMISSION, + etat_new=scu.INSCRIT, + operation_method="cancel_dem", event_type="DEMISSION", ): - "Annule une demission ou une défaillance" + "Annule une démission ou une défaillance" + etud: Identite = Identite.query.filter_by( + id=etudid, dept_id=g.scodoc_dept_id + ).first_or_404() + formsemestre: FormSemestre = FormSemestre.query.filter_by( + id=formsemestre_id, dept_id=g.scodoc_dept_id + ).first_or_404() # check lock - sem = sco_formsemestre.get_formsemestre(formsemestre_id) - if not sem["etat"]: + if not formsemestre.etat: raise ScoValueError("Modification impossible: semestre verrouille") # verif - info = sco_etud.get_etud_info(etudid, filled=True)[0] - ok = False - for i in info["ins"]: - if i["formsemestre_id"] == formsemestre_id: - if i["etat"] != etat_current: - raise ScoValueError("etudiant non %s !" % operation_name) - ok = True - break - if not ok: - raise ScoValueError("etudiant non inscrit ???") + if formsemestre_id not in (inscr.formsemestre_id for inscr in etud.inscriptions()): + raise ScoValueError("étudiant non inscrit dans ce semestre !") + if etud.inscription_etat(formsemestre_id) != etat_current: + raise ScoValueError(f"etudiant non {operation_name} !") + if not dialog_confirmed: return scu.confirm_dialog( - "
Confirmer l'annulation de la %s ?
" % operation_name, + f"Confirmer l'annulation de la {operation_name} ?
", dest_url="", cancel_url=url_for( "scolar.ficheEtud", scodoc_dept=g.scodoc_dept, etudid=etudid @@ -1260,25 +1253,22 @@ def _do_cancel_dem_or_def( parameters={"etudid": etudid, "formsemestre_id": formsemestre_id}, ) # - ins = sco_formsemestre_inscriptions.do_formsemestre_inscription_list( - {"etudid": etudid, "formsemestre_id": formsemestre_id} - )[0] - if ins["etat"] != etat_current: - raise ScoException("etudiant non %s !!!" % etat_current) # obviously a bug - ins["etat"] = etat_new - cnx = ndb.GetDBConnexion() - sco_formsemestre_inscriptions.do_formsemestre_inscription_edit( - args=ins, formsemestre_id=formsemestre_id + inscr = next( + inscr + for inscr in etud.inscriptions() + if inscr.formsemestre_id == formsemestre_id ) - logdb(cnx, method=operation_method, etudid=etudid) - cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor) - cursor.execute( - "delete from scolar_events where etudid=%(etudid)s and formsemestre_id=%(formsemestre_id)s and event_type='" - + event_type - + "'", - {"etudid": etudid, "formsemestre_id": formsemestre_id}, - ) - cnx.commit() + inscr.etat = etat_new + db.session.add(inscr) + Scolog.logdb(method=operation_method, etudid=etudid) + # Efface les évènements + for event in ScolarEvent.query.filter_by( + etudid=etudid, formsemestre_id=formsemestre_id, event_type=event_type + ): + db.session.delete(event) + + db.session.commit() + flash(f"{operation_name} annulée.") return flask.redirect( url_for("scolar.ficheEtud", scodoc_dept=g.scodoc_dept, etudid=etudid) )