From 478688fe49420c7031062975c42963d35070a94e Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 9 Dec 2021 11:52:46 +0100 Subject: [PATCH] Ordre des modules et UE (edition PN et poids) --- app/comp/moy_ue.py | 8 ++-- app/models/modules.py | 1 - app/scodoc/sco_edit_apc.py | 6 +-- app/scodoc/sco_edit_formation.py | 68 ++++++++++++++++++++------------ 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/app/comp/moy_ue.py b/app/comp/moy_ue.py index 06d0b2c5..7d7ea338 100644 --- a/app/comp/moy_ue.py +++ b/app/comp/moy_ue.py @@ -51,11 +51,13 @@ def df_load_module_coefs(formation_id: int, semestre_idx: int = None) -> pd.Data Si semestre_idx None, prend toutes les UE de la formation. """ - ues = UniteEns.query.filter_by(formation_id=formation_id).filter( - UniteEns.type != sco_codes_parcours.UE_SPORT + ues = ( + UniteEns.query.filter_by(formation_id=formation_id) + .filter(UniteEns.type != sco_codes_parcours.UE_SPORT) + .order_by(UniteEns.semestre_idx, UniteEns.numero, UniteEns.acronyme) ) modules = Module.query.filter_by(formation_id=formation_id).order_by( - Module.semestre_id, Module.numero + Module.semestre_id, Module.module_type.desc(), Module.numero, Module.code ) if semestre_idx is not None: ues = ues.filter_by(semestre_idx=semestre_idx) diff --git a/app/models/modules.py b/app/models/modules.py index 726dd5f2..e19c7774 100644 --- a/app/models/modules.py +++ b/app/models/modules.py @@ -1,6 +1,5 @@ """ScoDoc 9 models : Modules """ -from typing import Any from app import db from app.models import APO_CODE_STR_LEN diff --git a/app/scodoc/sco_edit_apc.py b/app/scodoc/sco_edit_apc.py index 200d26d8..a0f65cbe 100644 --- a/app/scodoc/sco_edit_apc.py +++ b/app/scodoc/sco_edit_apc.py @@ -50,10 +50,10 @@ def html_edit_formation_apc( parcours = formation.get_parcours() assert parcours.APC_SAE ressources = formation.modules.filter_by(module_type=ModuleType.RESSOURCE).order_by( - Module.semestre_id, Module.numero + Module.semestre_id, Module.numero, Module.code ) saes = formation.modules.filter_by(module_type=ModuleType.SAE).order_by( - Module.semestre_id, Module.numero + Module.semestre_id, Module.numero, Module.code ) if semestre_idx is None: semestre_ids = range(1, parcours.NB_SEM + 1) @@ -61,7 +61,7 @@ def html_edit_formation_apc( semestre_ids = [semestre_idx] other_modules = formation.modules.filter( Module.module_type != ModuleType.SAE, Module.module_type != ModuleType.RESSOURCE - ) + ).order_by(Module.semestre_id, Module.module_type, Module.numero, Module.code) arrow_up, arrow_down, arrow_none = sco_groups.get_arrow_icons_tags() icons = { diff --git a/app/scodoc/sco_edit_formation.py b/app/scodoc/sco_edit_formation.py index 7d2e7e72..6eb8628c 100644 --- a/app/scodoc/sco_edit_formation.py +++ b/app/scodoc/sco_edit_formation.py @@ -31,9 +31,12 @@ import flask from flask import g, url_for, request +from app import db +from app import log +from app.models.modules import Module + import app.scodoc.notesdb as ndb import app.scodoc.sco_utils as scu -from app import log from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message from app.scodoc.sco_exceptions import ScoValueError, ScoLockedFormError @@ -311,39 +314,54 @@ def invalidate_sems_in_formation(formation_id): ) # > formation modif. -def module_move(module_id, after=0, redirect=1): +def objects_renumber(obj_list) -> None: + "fixe les numeros des objets de la liste pour ne pas changer son ordre" + log(f"objects_renumber {obj_list}") + for i, obj in enumerate(obj_list): + obj.numero = i + db.session.add(obj) + db.session.commit() + + +def module_move(module_id, after=0, redirect=True): """Move before/after previous one (decrement/increment numero)""" - module = sco_edit_module.module_list({"module_id": module_id})[0] - redirect = int(redirect) + redirect = bool(redirect) + module = Module.query.get_or_404(module_id) after = int(after) # 0: deplace avant, 1 deplace apres if after not in (0, 1): - raise ValueError('invalid value for "after"') - formation_id = module["formation_id"] - others = sco_edit_module.module_list({"matiere_id": module["matiere_id"]}) - # log('others=%s' % others) - if len(others) > 1: - idx = [p["module_id"] for p in others].index(module_id) - # log('module_move: after=%s idx=%s' % (after, idx)) + raise ValueError(f'invalid value for "after" ({after})') + if module.formation.is_apc(): + # pas de matières, mais on prend tous les modules de même type de la formation + query = Module.query.filter_by( + semestre_id=module.semestre_id, + formation=module.formation, + module_type=module.module_type, + ) + else: + query = Module.query.filter_by(matiere=module.matiere) + modules = query.order_by(Module.numero, Module.code).all() + if len({o.numero for o in modules}) != len(modules): + # il y a des numeros identiques ! + objects_renumber(modules) + if len(modules) > 1: + idx = [m.id for m in modules].index(module.id) neigh = None # object to swap with if after == 0 and idx > 0: - neigh = others[idx - 1] - elif after == 1 and idx < len(others) - 1: - neigh = others[idx + 1] - if neigh: # - # swap numero between partition and its neighbor - # log('moving module %s' % module_id) - cnx = ndb.GetDBConnexion() - module["numero"], neigh["numero"] = neigh["numero"], module["numero"] - if module["numero"] == neigh["numero"]: - neigh["numero"] -= 2 * after - 1 - sco_edit_module._moduleEditor.edit(cnx, module) - sco_edit_module._moduleEditor.edit(cnx, neigh) - + neigh = modules[idx - 1] + elif after == 1 and idx < len(modules) - 1: + neigh = modules[idx + 1] + if neigh: # échange les numéros + module.numero, neigh.numero = neigh.numero, module.numero + db.session.add(module) + db.session.add(neigh) + db.session.commit() # redirect to ue_list page: if redirect: return flask.redirect( url_for( - "notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation_id + "notes.ue_table", + scodoc_dept=g.scodoc_dept, + formation_id=module.formation.id, ) )