fix Module.set_ue_coef_dict

This commit is contained in:
Emmanuel Viennet 2021-12-10 16:45:36 +01:00
parent 272de740e1
commit 2cca1e9bbd

View File

@ -85,13 +85,27 @@ class Module(db.Model):
Les coefs nuls (zéro) ne sont pas stockés: la relation est supprimée. Les coefs nuls (zéro) ne sont pas stockés: la relation est supprimée.
""" """
ue_coefs = [] ue_coefs = []
changed = False
for ue_id, coef in ue_coef_dict.items(): for ue_id, coef in ue_coef_dict.items():
ue = UniteEns.query.get(ue_id) # Existant ?
if coef == 0.0: coefs = [c for c in self.ue_coefs if c.ue_id == ue_id]
self.delete_ue_coef(ue) 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: else:
ue_coefs.append(ModuleUECoef(module=self, ue=ue, coef=coef)) # crée nouveau coef:
self.ue_coefs = ue_coefs 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() self.formation.invalidate_module_coefs()
def update_ue_coef_dict(self, ue_coef_dict: dict): def update_ue_coef_dict(self, ue_coef_dict: dict):