From abd8bf484dc8a13494b033fe983a1b66041b9768 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 17 Oct 2024 17:07:24 +0200 Subject: [PATCH] Export formation: fix regression and sort xml attributes to get stable results --- app/formations/formation_io.py | 11 +++++------ app/scodoc/sco_xml.py | 30 +++++++++++++----------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/formations/formation_io.py b/app/formations/formation_io.py index 680db80fe..f966fdb9e 100644 --- a/app/formations/formation_io.py +++ b/app/formations/formation_io.py @@ -125,7 +125,7 @@ def formation_export_dict( mat = db.session.get(Matiere, matiere_id) mods = mat.modules.all() 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"]): if export_tags: tags = [t.title for t in module.tags] @@ -228,11 +228,10 @@ def _formation_retreive_refcomp(f_dict: dict) -> int: ).first() if refcomp: return refcomp.id - else: - flash( - f"""Impossible de trouver le référentiel de compétence pour { - refcomp_specialite} : est-il chargé ?""" - ) + flash( + f"""Impossible de trouver le référentiel de compétence pour { + refcomp_specialite} : est-il chargé ?""" + ) return None diff --git a/app/scodoc/sco_xml.py b/app/scodoc/sco_xml.py index 910629a4f..4f705302c 100644 --- a/app/scodoc/sco_xml.py +++ b/app/scodoc/sco_xml.py @@ -107,24 +107,20 @@ def _dictlist2xml(dictlist, root=None, tagname=None, quote=False): elem.set("code", _repr_as_xml(d)) else: if quote: - d_scalar = dict( - [ - (k, quote_xml_attr(_repr_as_xml(v))) - for (k, v) in d.items() - if isinstance(v, scalar_types) - ] - ) + d_scalar = { + k: quote_xml_attr(_repr_as_xml(v)) + for (k, v) in d.items() + if isinstance(v, scalar_types) + } else: - d_scalar = dict( - [ - (k, _repr_as_xml(v)) - for (k, v) in d.items() - if isinstance(v, scalar_types) - ] - ) - for k in d_scalar: - elem.set(k, d_scalar[k]) - d_list = dict([(k, v) for (k, v) in d.items() if isinstance(v, list)]) + d_scalar = { + k: _repr_as_xml(v) + for (k, v) in d.items() + if isinstance(v, scalar_types) + } + for k, v in sorted(d_scalar.items()): + elem.set(k, v) + d_list = {k: v for (k, v) in d.items() if isinstance(v, list)} if d_list: for k, v in d_list.items(): _dictlist2xml(v, tagname=k, root=elem, quote=quote)