From f0f9158ac64a38f8e4478a6ffbba17de71cce9f0 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 10 Jul 2024 18:27:13 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9liore=20import/export=20formations=20BU?= =?UTF-8?q?T:=20identification=20des=20niveaux=20si=20libell=C3=A9s=20dupl?= =?UTF-8?q?iqu=C3=A9s.=20+=20Fix=20unicit=C3=A9=20apprentissages=20critiqu?= =?UTF-8?q?es-modules.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/but_refcomp.py | 1 + app/scodoc/sco_edit_formation.py | 1 - app/scodoc/sco_formations.py | 29 +++++++++---- .../versions/f6cb3d4e44ec_fix_refcomp_geii.py | 43 +++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 migrations/versions/f6cb3d4e44ec_fix_refcomp_geii.py diff --git a/app/models/but_refcomp.py b/app/models/but_refcomp.py index 6831f092..f072bfb6 100644 --- a/app/models/but_refcomp.py +++ b/app/models/but_refcomp.py @@ -604,6 +604,7 @@ app_critiques_modules = db.Table( db.ForeignKey("apc_app_critique.id", ondelete="CASCADE"), primary_key=True, ), + db.UniqueConstraint("module_id", "app_crit_id", name="uix_module_id_app_crit_id"), ) diff --git a/app/scodoc/sco_edit_formation.py b/app/scodoc/sco_edit_formation.py index 6def9f0f..addc5847 100644 --- a/app/scodoc/sco_edit_formation.py +++ b/app/scodoc/sco_edit_formation.py @@ -47,7 +47,6 @@ from app.scodoc import html_sco_header from app.scodoc import sco_cache from app.scodoc import codes_cursus from app.scodoc import sco_edit_ue -from app.scodoc import sco_formsemestre def formation_delete(formation_id=None, dialog_confirmed=False): diff --git a/app/scodoc/sco_formations.py b/app/scodoc/sco_formations.py index 86893ebb..2ac67a98 100644 --- a/app/scodoc/sco_formations.py +++ b/app/scodoc/sco_formations.py @@ -125,6 +125,9 @@ def formation_export_dict( if formation.is_apc(): # BUT: indique niveau de compétence associé à l'UE if ue.niveau_competence: + ue_dict["apc_niveau_competence_titre"] = ( + ue.niveau_competence.competence.titre + ) ue_dict["apc_niveau_libelle"] = ue.niveau_competence.libelle ue_dict["apc_niveau_annee"] = ue.niveau_competence.annee ue_dict["apc_niveau_ordre"] = ue.niveau_competence.ordre @@ -219,7 +222,7 @@ def formation_export( """Get a formation, with UE, matieres, modules in desired format """ - formation: Formation = Formation.query.get_or_404(formation_id) + formation = Formation.get_formation(formation_id) f_dict = formation_export_dict( formation, export_ids=export_ids, @@ -258,29 +261,39 @@ def _formation_retreive_refcomp(f_dict: dict) -> int: return refcomp.id else: flash( - f"Impossible de trouver le référentiel de compétence pour {refcomp_specialite} : est-il chargé ?" + f"""Impossible de trouver le référentiel de compétence pour { + refcomp_specialite} : est-il chargé ?""" ) return None def _formation_retreive_apc_niveau( referentiel_competence_id: int, ue_dict: dict -) -> int: +) -> int | None: """Recherche dans le ref. de comp. un niveau pour cette UE. - Utilise (libelle, annee, ordre) comme clé. + Utilise (libelle, annee, ordre) comme clé, ou + (competence_titre, libelle, annee, ordre) si présent. """ libelle = ue_dict.get("apc_niveau_libelle") annee = ue_dict.get("apc_niveau_annee") ordre = ue_dict.get("apc_niveau_ordre") - if all((libelle, annee, ordre)): + competence_titre = ue_dict.get("apc_niveau_competence_titre") + niveau = None + if all((competence_titre, libelle, annee, ordre)): + niveau = ( + ApcNiveau.query.filter_by(libelle=libelle, annee=annee, ordre=ordre) + .join(ApcCompetence) + .filter_by(referentiel_id=referentiel_competence_id, titre=competence_titre) + ).first() + + elif all((libelle, annee, ordre)): niveau = ( ApcNiveau.query.filter_by(libelle=libelle, annee=annee, ordre=ordre) .join(ApcCompetence) .filter_by(referentiel_id=referentiel_competence_id) ).first() - if niveau is not None: - return niveau.id - return None + + return niveau.id if niveau is not None else None def formation_import_xml(doc: str, import_tags=True, use_local_refcomp=False): diff --git a/migrations/versions/f6cb3d4e44ec_fix_refcomp_geii.py b/migrations/versions/f6cb3d4e44ec_fix_refcomp_geii.py new file mode 100644 index 00000000..8a871c84 --- /dev/null +++ b/migrations/versions/f6cb3d4e44ec_fix_refcomp_geii.py @@ -0,0 +1,43 @@ +"""fix_refcomp_geii + +Revision ID: f6cb3d4e44ec +Revises: 9794534db935 +Create Date: 2024-07-10 17:03:58.549168 + +Élimine éventuels doublons dans associations Apprentissages critiques du ref. comp. +Pour le cas de GEII seulement. +""" + +from alembic import op + + +# revision identifiers, used by Alembic. +revision = "f6cb3d4e44ec" +down_revision = "9794534db935" +branch_labels = None +depends_on = None + + +def upgrade(): + op.execute( + """ + CREATE TABLE apc_modules_acs_temp AS + SELECT DISTINCT module_id, app_crit_id + FROM apc_modules_acs; + DELETE FROM apc_modules_acs; + INSERT INTO apc_modules_acs (module_id, app_crit_id) + SELECT module_id, app_crit_id + FROM apc_modules_acs_temp; + """ + ) + with op.batch_alter_table("apc_modules_acs", schema=None) as batch_op: + batch_op.create_unique_constraint( + "uix_module_id_app_crit_id", ["module_id", "app_crit_id"] + ) + + op.drop_table("apc_modules_acs_temp") + + +def downgrade(): + with op.batch_alter_table("apc_modules_acs", schema=None) as batch_op: + batch_op.drop_constraint("uix_module_id_app_crit_id", type_="unique")