From 2cca1e9bbdade1c9a25b6460855cecdf4c832570 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 10 Dec 2021 16:45:36 +0100 Subject: [PATCH] fix Module.set_ue_coef_dict --- app/models/modules.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/models/modules.py b/app/models/modules.py index f5a978c2d..854a8a8be 100644 --- a/app/models/modules.py +++ b/app/models/modules.py @@ -85,14 +85,28 @@ class Module(db.Model): Les coefs nuls (zéro) ne sont pas stockés: la relation est supprimée. """ ue_coefs = [] + changed = False for ue_id, coef in ue_coef_dict.items(): - ue = UniteEns.query.get(ue_id) - if coef == 0.0: - self.delete_ue_coef(ue) + # Existant ? + coefs = [c for c in self.ue_coefs if c.ue_id == ue_id] + if coefs: + ue_coef = coefs[0] + if coef == 0.0: # supprime ce coef + db.session.delete(ue_coef) + changed = True + elif coef != ue_coef.coef: + ue_coef.coef = coef + db.session.add(ue_coef) + changed = True else: - ue_coefs.append(ModuleUECoef(module=self, ue=ue, coef=coef)) - self.ue_coefs = ue_coefs - self.formation.invalidate_module_coefs() + # crée nouveau coef: + if coef != 0.0: + ue = UniteEns.query.get(ue_id) + ue_coef = ModuleUECoef(module=self, ue=ue, coef=coef) + self.ue_coefs.append(ue_coef) + changed = True + if changed: + self.formation.invalidate_module_coefs() def update_ue_coef_dict(self, ue_coef_dict: dict): """update coefs vers UE (ajoute aux existants)"""