prettier xml exports

This commit is contained in:
Emmanuel Viennet 2021-08-01 16:17:33 +03:00
parent f5dbbaa207
commit 704bb2c170
3 changed files with 225 additions and 206 deletions

View File

@ -31,6 +31,7 @@
from xml.etree import ElementTree from xml.etree import ElementTree
import xml.sax.saxutils import xml.sax.saxutils
from xml.dom import minidom
from app.scodoc import sco_utils as scu from app.scodoc import sco_utils as scu
from app.scodoc.sco_vdi import ApoEtapeVDI from app.scodoc.sco_vdi import ApoEtapeVDI
@ -44,7 +45,7 @@ def quote_xml_attr(data):
# ScoDoc7 legacy function: # 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. """Represent a dict as XML data.
All keys with string or numeric values are attributes (numbers converted to strings). All keys with string or numeric values are attributes (numbers converted to strings).
All list values converted to list of childs (recursively). 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: if not tagname:
raise ValueError("invalid empty tagname !") raise ValueError("invalid empty tagname !")
elements = _dictlist2xml(dictlist, root=[], tagname=tagname, quote=quote) 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 scu.SCO_ENCODING
) )
if pretty:
# solution peu satisfaisante car on doit reparser le XML
# de plus, on encode/decode pour avoir le tag <?xml version="1.0" encoding="utf-8"?>
ans = (
minidom.parseString(ans)
.toprettyxml(indent="\t", encoding="utf-8")
.decode("utf-8")
)
return ans
def _dictlist2xml(dictlist, root=None, tagname=None, quote=False): def _dictlist2xml(dictlist, root=None, tagname=None, quote=False):

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<formation acronyme="DUT Info" code_specialite="" type_parcours="100" titre_officiel="DUT Informatique" formation_code="FCOD2" version="1" titre="DUT Informatique" formation_id="FORM234"> <formation acronyme="DUT Info" code_specialite="" type_parcours="100" titre_officiel="DUT Informatique" formation_code="FCOD2" version="1" titre="DUT Informatique" formation_id="FORM234">
<ue acronyme="UE11" coefficient="0.0" is_external="0" code_apogee="" ue_code="UE11" numero="1" titre="Base de l'informatique" type="0"> <ue acronyme="UE11" coefficient="0.0" is_external="0" code_apogee="" ue_code="UE11" numero="1" titre="Base de l'informatique" type="0">
<matiere titre="Architecture matérielle - Systèmes dexploitation - Réseaux" numero="1"> <matiere titre="Architecture matérielle - Systèmes dexploitation - Réseaux" numero="1">

View File

@ -42,6 +42,8 @@
# - do_formation_delete # - do_formation_delete
import json import json
import xml.dom.minidom
import flask import flask
from flask import g from flask import g
@ -376,6 +378,12 @@ def test_import_formation(test_client):
for mod in modules: for mod in modules:
mi = G.create_moduleimpl( mi = G.create_moduleimpl(
module_id=mod["module_id"], module_id=mod["module_id"],
formsemestre_id=sems[mod["semestre_id"] - 1], formsemestre_id=sems[mod["semestre_id"] - 1]["formsemestre_id"],
responsable_id="bach", 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)