From 61d35ddac0f89525ffc0746a4d205071fb2dfb94 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 22 Feb 2024 17:22:56 +0100 Subject: [PATCH] =?UTF-8?q?Fix:=20cr=C3=A9ation=20modules=20(parcours)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/modules.py | 34 +++++++++++++++++++++++++++++++++- app/scodoc/sco_edit_module.py | 7 +++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/models/modules.py b/app/models/modules.py index b0db0e68a..3da839a7a 100644 --- a/app/models/modules.py +++ b/app/models/modules.py @@ -6,7 +6,12 @@ from flask import current_app, g from app import db from app import models from app.models import APO_CODE_STR_LEN -from app.models.but_refcomp import ApcParcours, app_critiques_modules, parcours_modules +from app.models.but_refcomp import ( + ApcParcours, + ApcReferentielCompetences, + app_critiques_modules, + parcours_modules, +) from app.scodoc import sco_utils as scu from app.scodoc.codes_cursus import UE_SPORT from app.scodoc.sco_exceptions import ScoValueError @@ -100,6 +105,33 @@ class Module(models.ScoDocModel): return args_dict + @classmethod + def filter_model_attributes(cls, data: dict, excluded: set[str] = None) -> dict: + """Returns a copy of dict with only the keys belonging to the Model and not in excluded. + Add 'id' to excluded.""" + # on ne peut pas affecter directement parcours + return super().filter_model_attributes(data, (excluded or set()) | {"parcours"}) + + @classmethod + def create_from_dict(cls, data: dict) -> "Module": + """Create from given dict, add parcours""" + mod = super().create_from_dict(data) + for p in data["parcours"]: + if isinstance(p, ApcParcours): + parcour: ApcParcours = p + else: + pid = int(p) + query = ApcParcours.query.filter_by(id=pid) + if g.scodoc_dept: + query = query.join(ApcReferentielCompetences).filter_by( + dept_id=g.scodoc_dept_id + ) + parcour: ApcParcours = query.first() + if parcour is None: + raise ScoValueError("Parcours invalide") + mod.parcours.append(parcour) + return mod + def clone(self): """Create a new copy of this module.""" mod = Module( diff --git a/app/scodoc/sco_edit_module.py b/app/scodoc/sco_edit_module.py index 5cf9797ee..e8f28787e 100644 --- a/app/scodoc/sco_edit_module.py +++ b/app/scodoc/sco_edit_module.py @@ -682,8 +682,11 @@ def module_edit( "input_type": "checkbox", "vertical": True, "dom_id": "tf_module_parcours", - "labels": [parcour.libelle for parcour in ref_comp.parcours] - + ["Tous (tronc commun)"], + "labels": [ + f"  {parcour.libelle} ({parcour.code})" + for parcour in ref_comp.parcours + ] + + ["  Tous (tronc commun)"], "allowed_values": [ str(parcour.id) for parcour in ref_comp.parcours ]