From 371b01724555ddcd4300c952b6f3046ad325d0ca Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 4 Aug 2022 16:54:59 +0200 Subject: [PATCH] Base test API: charge ref. comp. --- app/but/import_refcomp.py | 2 +- app/models/but_refcomp.py | 15 +- app/models/preferences.py | 3 + app/views/refcomp.py | 2 +- .../fakedatabase/create_test_api_database.py | 212 +++--------------- 5 files changed, 40 insertions(+), 194 deletions(-) diff --git a/app/but/import_refcomp.py b/app/but/import_refcomp.py index 9acd1c9d..e1e62d48 100644 --- a/app/but/import_refcomp.py +++ b/app/but/import_refcomp.py @@ -35,7 +35,7 @@ def orebut_import_refcomp(xml_data: str, dept_id: int, orig_filename=None): raise ScoValueError( f"""Un référentiel a déjà été chargé d'un fichier de même nom. ({orig_filename}) - Supprimez-le ou changer le nom du fichier.""" + Supprimez-le ou changez le nom du fichier.""" ) try: diff --git a/app/models/but_refcomp.py b/app/models/but_refcomp.py index 0a4afd07..e30d9988 100644 --- a/app/models/but_refcomp.py +++ b/app/models/but_refcomp.py @@ -128,12 +128,15 @@ class ApcReferentielCompetences(db.Model, XMLModel): for parcour in parcours } # Cherche tronc commun - niveaux_ids_tc = set.intersection( - *[ - {n.id for n in niveaux_by_parcours[parcour_id]} - for parcour_id in niveaux_by_parcours - ] - ) + if niveaux_by_parcours: + niveaux_ids_tc = set.intersection( + *[ + {n.id for n in niveaux_by_parcours[parcour_id]} + for parcour_id in niveaux_by_parcours + ] + ) + else: + niveaux_ids_tc = set() # Enleve les niveaux du tronc commun niveaux_by_parcours_no_tc = { parcour.id: [ diff --git a/app/models/preferences.py b/app/models/preferences.py index 924f6e60..c6b81edf 100644 --- a/app/models/preferences.py +++ b/app/models/preferences.py @@ -18,3 +18,6 @@ class ScoPreference(db.Model): name = db.Column(db.String(128), nullable=False, index=True) value = db.Column(db.Text()) formsemestre_id = db.Column(db.Integer, db.ForeignKey("notes_formsemestre.id")) + + def __repr__(self): + return f"<{self.__class__.__name__} {self.id} {self.departement.acronym} {self.name}={self.value}>" diff --git a/app/views/refcomp.py b/app/views/refcomp.py index 53a2a464..1b2b659d 100644 --- a/app/views/refcomp.py +++ b/app/views/refcomp.py @@ -235,7 +235,7 @@ def refcomp_load(formation_id=None): ] except (ValueError, IndexError): raise ScoValueError("choix invalide") - f = open(filename) + f = open(filename, encoding="utf-8") else: raise ScoValueError("choix invalide") try: diff --git a/tools/fakedatabase/create_test_api_database.py b/tools/fakedatabase/create_test_api_database.py index 36c3dca0..fd836a92 100644 --- a/tools/fakedatabase/create_test_api_database.py +++ b/tools/fakedatabase/create_test_api_database.py @@ -12,28 +12,21 @@ import random import time import sys +from app import db from app.auth.models import Role, User +from app.but.import_refcomp import orebut_import_refcomp from app import models +from app.models import departements from app.models import ( + Absence, + ApcReferentielCompetences, Departement, Formation, FormSemestre, + FormSemestreEtape, Identite, ModuleImpl, NotesNotes, - ApcReferentielCompetences, - ApcCompetence, - Absence, - FormSemestreEtape, -) -from app import db -from app.models.but_refcomp import ( - ApcParcours, - ApcAnneeParcours, - ApcSituationPro, - ApcComposanteEssentielle, - ApcNiveau, - ApcAppCritique, ) from app.scodoc import ( sco_cache, @@ -43,35 +36,46 @@ from app.scodoc import ( sco_groups, ) from app.scodoc.sco_permissions import Permission -from app.scodoc.sco_saisie_notes import notes_add from tools.fakeportal.gen_nomprenoms import nomprenom random.seed(12345678) # tests reproductibles # La formation à utiliser: FORMATION_XML_FILENAME = "tests/ressources/formations/scodoc_formation_RT_BUT_RT_v1.xml" +REFCOMP_FILENAME = ( + "ressources/referentiels/but2022/competences/but-RT-05012022-081735.xml" +) def init_departement(acronym: str) -> Departement: "Create dept, and switch context into it." import app as mapp - dept = Departement(acronym=acronym) - db.session.add(dept) + dept = departements.create_dept(acronym) mapp.set_sco_dept(acronym) - db.session.commit() return dept -def import_formation() -> Formation: +def import_formation(dept_id: int) -> Formation: """Import formation from XML. + Associate ref. comp. Returns formation_id """ - with open(FORMATION_XML_FILENAME) as f: + with open(FORMATION_XML_FILENAME, encoding="utf-8") as f: doc = f.read() - # --- Création de la formation + # --- Création de la formation (import programme) f = sco_formations.formation_import_xml(doc) - return Formation.query.get(f[0]) + formation = Formation.query.get(f[0]) + # --- Association ref. comp. + with open(REFCOMP_FILENAME, encoding="utf-8") as f: + xml_data = f.read() + ref_comp = orebut_import_refcomp( + xml_data, dept_id=dept_id, orig_filename=REFCOMP_FILENAME + ) + formation.referentiel_competence_id = ref_comp.id + db.session.add(formation) + db.session.commit() + return formation def create_users(dept: Departement) -> tuple: @@ -269,169 +273,6 @@ def saisie_notes_evaluations(formsemestre: FormSemestre, user: User): saisir_notes(evaluation.id, condition_saisie_notes) -def create_ref_comp(formation: Formation): - """ - Créer un referentiel de competences - """ - # ### ApcSituationPro ### - # apc_situation_pro_id = 1 - # apc_situation_pro_competence_id = 1 - # apc_situation_pro_libelle = "" - # - # apc_situation_pro = ApcSituationPro( - # apc_situation_pro_id, apc_situation_pro_competence_id, apc_situation_pro_libelle - # ) - # db.session.add(apc_situation_pro) - # db.session.commit() - # - # ### ApcComposanteEssentielle ### - # apc_composante_essentielle_id = 1 - # apc_composante_essentielle_competence_id = 1 - # apc_composante_essentielle_libelle = "" - # - # apc_composante_essentielle = ApcComposanteEssentielle( - # apc_composante_essentielle_id, - # apc_composante_essentielle_competence_id, - # apc_composante_essentielle_libelle, - # ) - # db.session.add(apc_composante_essentielle) - # db.session.commit() - # - # ### ApcAppCritique ### - # apc_app_critique_id = 1 - # apc_app_critique_niveau_id = 1 - # apc_app_critique_code = "" - # apc_app_critique_libelle = "" - # apc_app_critique_modules = formation.modules - # - # apc_app_critique = ApcAppCritique( - # apc_app_critique_id, - # apc_app_critique_niveau_id, - # apc_app_critique_code, - # apc_app_critique_libelle, - # apc_app_critique_modules, - # ) - # db.session.add(apc_app_critique) - # db.session.commit() - # - # ### ApcNiveau ### - # apc_niveau_id = 1 - # apc_niveau_competence_id = 1 - # apc_niveau_libelle = "" - # apc_niveau_annee = "" - # apc_niveau_ordre = 1 - # apc_niveau_app_critiques = apc_app_critique - # - # apc_niveau = ApcNiveau( - # apc_niveau_id, - # apc_niveau_competence_id, - # apc_niveau_libelle, - # apc_niveau_annee, - # apc_niveau_ordre, - # apc_niveau_app_critiques, - # ) - # db.session.add(apc_niveau) - # db.session.commit() - # - # ### ApcCompetence ### - # apc_competence_id = 1 - # apc_competence_referentiel_id = 1 - # apc_competence_id_orebut = "" - # apc_competence_titre = "" - # apc_competence_titre_long = "" - # apc_competence_couleur = "" - # apc_competence_numero = 1 - # apc_competence_xml_attribs = { # xml_attrib : attribute - # "id": "id_orebut", - # "nom_court": "titre", # was name - # "libelle_long": "titre_long", - # } - # apc_competence_situations = apc_situation_pro - # apc_competence_composantes_essentielles = apc_composante_essentielle - # apc_competence_niveaux = apc_niveau - # - # apc_competence = ApcCompetence( - # apc_competence_id, - # apc_competence_referentiel_id, - # apc_competence_id_orebut, - # apc_competence_titre, - # apc_competence_titre_long, - # apc_competence_couleur, - # apc_competence_numero, - # apc_competence_xml_attribs, - # apc_competence_situations, - # apc_competence_composantes_essentielles, - # apc_competence_niveaux, - # ) - # db.session.add(apc_competence) - # db.session.commit() - # - # ### ApcAnneeParcours ### - # apc_annee_parcours_id = 1 - # apc_annee_parcours_parcours_id = 1 - # apc_annee_parcours_ordre = 1 - # - # ap_annee_parcours = ApcAnneeParcours( - # apc_annee_parcours_id, apc_annee_parcours_parcours_id, apc_annee_parcours_ordre - # ) - # - # ### ApcParcours ### - # apc_parcours_id = 1 - # apc_parcours_referentiel_id = 1 - # apc_parcours_numero = 1 - # apc_parcours_code = "" - # apc_parcours_libelle = "" - # apc_parcours_annees = ap_annee_parcours - # - # apc_parcours = ApcParcours( - # apc_parcours_id, - # apc_parcours_referentiel_id, - # apc_parcours_numero, - # apc_parcours_code, - # apc_parcours_libelle, - # apc_parcours_annees, - # ) - # db.session.add(apc_parcours) - # db.session.commit() - - ### ApcReferentielCompetences ### - apc_referentiel_competences_id = 1 - apc_referentiel_competences_dept_id = 1 - apc_referentiel_competences_annexe = "" - apc_referentiel_competences_specialite = "" - apc_referentiel_competences_specialite_long = "" - apc_referentiel_competences_type_titre = "" - apc_referentiel_competences_type_structure = "" - apc_referentiel_competences_type_departement = "" - apc_referentiel_competences_version_orebut = "" - apc_referentiel_competences_xml_attribs = { - "type": "type_titre", - "version": "version_orebut", - } - apc_referentiel_competences_scodoc_date_loaded = "" - apc_referentiel_competences_scodoc_orig_filename = "" - # apc_referentiel_competences_competences = apc_competence - # apc_referentiel_competences_parcours = apc_parcours - # apc_referentiel_competences_formations = formation - - apc_referentiel_competences = ApcReferentielCompetences( - dept_id=apc_referentiel_competences_dept_id, - annexe=apc_referentiel_competences_annexe, - specialite=apc_referentiel_competences_specialite, - specialite_long=apc_referentiel_competences_specialite_long, - type_titre=apc_referentiel_competences_type_titre, - type_structure=apc_referentiel_competences_type_structure, - type_departement=apc_referentiel_competences_type_departement, - version_orebut=apc_referentiel_competences_version_orebut, - scodoc_orig_filename=apc_referentiel_competences_scodoc_orig_filename, - ) - db.session.add(apc_referentiel_competences) - db.session.commit() - - formation.referentiel_competence_id = apc_referentiel_competences.id - db.session.commit() - - def add_absences(formsemestre: FormSemestre): """ Ajoute des absences en base @@ -496,12 +337,11 @@ def init_test_database(): user_lecteur, user_autre = create_users(dept) with sco_cache.DeferredSemCacheManager(): etuds = create_etuds(dept) - formation = import_formation() + formation = import_formation(dept.id) formsemestre = create_formsemestre(formation, user_lecteur) create_evaluations(formsemestre) inscrit_etudiants(etuds, formsemestre) saisie_notes_evaluations(formsemestre, user_lecteur) - create_ref_comp(formation) add_absences(formsemestre) create_etape_apo(formsemestre) # à compléter