diff --git a/app/models/modules.py b/app/models/modules.py index f5a978c2..854a8a8b 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)"""