Export formation: fix regression and sort xml attributes to get stable results

This commit is contained in:
Emmanuel Viennet 2024-10-17 17:07:24 +02:00
parent 8feff2b182
commit abd8bf484d
2 changed files with 18 additions and 23 deletions

View File

@ -125,7 +125,7 @@ def formation_export_dict(
mat = db.session.get(Matiere, matiere_id) mat = db.session.get(Matiere, matiere_id)
mods = mat.modules.all() mods = mat.modules.all()
mods.sort(key=lambda m: (m.numero, m.code)) mods.sort(key=lambda m: (m.numero, m.code))
mat_d["module"] = [mod.to_dict() for mod in mods] mat_d["module"] = [mod.to_dict(convert_objects=True) for mod in mods]
for module, mod_d in zip(mods, mat_d["module"]): for module, mod_d in zip(mods, mat_d["module"]):
if export_tags: if export_tags:
tags = [t.title for t in module.tags] tags = [t.title for t in module.tags]
@ -228,7 +228,6 @@ def _formation_retreive_refcomp(f_dict: dict) -> int:
).first() ).first()
if refcomp: if refcomp:
return refcomp.id return refcomp.id
else:
flash( flash(
f"""Impossible de trouver le référentiel de compétence pour { f"""Impossible de trouver le référentiel de compétence pour {
refcomp_specialite} : est-il chargé ?""" refcomp_specialite} : est-il chargé ?"""

View File

@ -107,24 +107,20 @@ def _dictlist2xml(dictlist, root=None, tagname=None, quote=False):
elem.set("code", _repr_as_xml(d)) elem.set("code", _repr_as_xml(d))
else: else:
if quote: if quote:
d_scalar = dict( d_scalar = {
[ k: quote_xml_attr(_repr_as_xml(v))
(k, quote_xml_attr(_repr_as_xml(v)))
for (k, v) in d.items() for (k, v) in d.items()
if isinstance(v, scalar_types) if isinstance(v, scalar_types)
] }
)
else: else:
d_scalar = dict( d_scalar = {
[ k: _repr_as_xml(v)
(k, _repr_as_xml(v))
for (k, v) in d.items() for (k, v) in d.items()
if isinstance(v, scalar_types) if isinstance(v, scalar_types)
] }
) for k, v in sorted(d_scalar.items()):
for k in d_scalar: elem.set(k, v)
elem.set(k, d_scalar[k]) d_list = {k: v for (k, v) in d.items() if isinstance(v, list)}
d_list = dict([(k, v) for (k, v) in d.items() if isinstance(v, list)])
if d_list: if d_list:
for k, v in d_list.items(): for k, v in d_list.items():
_dictlist2xml(v, tagname=k, root=elem, quote=quote) _dictlist2xml(v, tagname=k, root=elem, quote=quote)