From 704bb2c1703e3130d395aee83793984a9c4860bc Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sun, 1 Aug 2021 16:17:33 +0300 Subject: [PATCH] prettier xml exports --- app/scodoc/sco_xml.py | 14 +- tests/unit/formation-exemple-1.xml | 407 +++++++++++++++-------------- tests/unit/test_formations.py | 10 +- 3 files changed, 225 insertions(+), 206 deletions(-) diff --git a/app/scodoc/sco_xml.py b/app/scodoc/sco_xml.py index d7ec39b2..5677e00d 100644 --- a/app/scodoc/sco_xml.py +++ b/app/scodoc/sco_xml.py @@ -31,6 +31,7 @@ from xml.etree import ElementTree import xml.sax.saxutils +from xml.dom import minidom from app.scodoc import sco_utils as scu from app.scodoc.sco_vdi import ApoEtapeVDI @@ -44,7 +45,7 @@ def quote_xml_attr(data): # ScoDoc7 legacy function: -def simple_dictlist2xml(dictlist, doc=None, tagname=None, quote=False): +def simple_dictlist2xml(dictlist, tagname=None, quote=False, pretty=True): """Represent a dict as XML data. All keys with string or numeric values are attributes (numbers converted to strings). All list values converted to list of childs (recursively). @@ -64,9 +65,18 @@ def simple_dictlist2xml(dictlist, doc=None, tagname=None, quote=False): if not tagname: raise ValueError("invalid empty tagname !") elements = _dictlist2xml(dictlist, root=[], tagname=tagname, quote=quote) - return XML_HEADER + b"\n".join([ElementTree.tostring(x) for x in elements]).decode( + ans = XML_HEADER + b"\n".join([ElementTree.tostring(x) for x in elements]).decode( scu.SCO_ENCODING ) + if pretty: + # solution peu satisfaisante car on doit reparser le XML + # de plus, on encode/decode pour avoir le tag + ans = ( + minidom.parseString(ans) + .toprettyxml(indent="\t", encoding="utf-8") + .decode("utf-8") + ) + return ans def _dictlist2xml(dictlist, root=None, tagname=None, quote=False): diff --git a/tests/unit/formation-exemple-1.xml b/tests/unit/formation-exemple-1.xml index 8baf7a6b..bf1d5251 100755 --- a/tests/unit/formation-exemple-1.xml +++ b/tests/unit/formation-exemple-1.xml @@ -1,205 +1,206 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/unit/test_formations.py b/tests/unit/test_formations.py index a3af8c9b..274a75cf 100644 --- a/tests/unit/test_formations.py +++ b/tests/unit/test_formations.py @@ -42,6 +42,8 @@ # - do_formation_delete import json +import xml.dom.minidom + import flask from flask import g @@ -376,6 +378,12 @@ def test_import_formation(test_client): for mod in modules: mi = G.create_moduleimpl( module_id=mod["module_id"], - formsemestre_id=sems[mod["semestre_id"] - 1], + formsemestre_id=sems[mod["semestre_id"] - 1]["formsemestre_id"], responsable_id="bach", ) + assert mi["ens"] == [] + assert mi["module_id"] == mod["module_id"] + + # --- Export formation en XML + doc1 = sco_formations.formation_export(context, formation_id, format="xml") + assert isinstance(doc1, str)