1
0
forked from ScoDoc/ScoDoc

enable xml/json result as file

This commit is contained in:
Jean-Marie Place 2021-09-25 09:53:31 +02:00
parent 1741e75f72
commit 00dbd25b42
2 changed files with 8 additions and 8 deletions

View File

@ -130,7 +130,7 @@ def formation_export(formation_id, export_ids=False, export_tags=True, format=No
if mod["ects"] is None: if mod["ects"] is None:
del mod["ects"] del mod["ects"]
return scu.sendResult(F, name="formation", format=format, force_outer_xml_tag=False) return scu.sendResult(F, name="formation", format=format, force_outer_xml_tag=False, attached=True)
def formation_import_xml(doc: str, import_tags=True): def formation_import_xml(doc: str, import_tags=True):

View File

@ -553,28 +553,28 @@ class ScoDocJSONEncoder(json.JSONEncoder):
return json.JSONEncoder.default(self, o) return json.JSONEncoder.default(self, o)
def sendJSON(data): def sendJSON(data, attached=False):
js = json.dumps(data, indent=1, cls=ScoDocJSONEncoder) js = json.dumps(data, indent=1, cls=ScoDocJSONEncoder)
return send_file(js, filename="sco_data.json", mime=JSON_MIMETYPE, attached=False) return send_file(js, filename="sco_data.json", mime=JSON_MIMETYPE, attached=attached)
def sendXML(data, tagname=None, force_outer_xml_tag=True): def sendXML(data, tagname=None, force_outer_xml_tag=True, attached=False):
if type(data) != list: if type(data) != list:
data = [data] # always list-of-dicts data = [data] # always list-of-dicts
if force_outer_xml_tag: if force_outer_xml_tag:
data = [{tagname: data}] data = [{tagname: data}]
tagname += "_list" tagname += "_list"
doc = sco_xml.simple_dictlist2xml(data, tagname=tagname) doc = sco_xml.simple_dictlist2xml(data, tagname=tagname)
return send_file(doc, filename="sco_data.xml", mime=XML_MIMETYPE, attached=False) return send_file(doc, filename="sco_data.xml", mime=XML_MIMETYPE, attached=attached)
def sendResult(data, name=None, format=None, force_outer_xml_tag=True): def sendResult(data, name=None, format=None, force_outer_xml_tag=True, attached=False):
if (format is None) or (format == "html"): if (format is None) or (format == "html"):
return data return data
elif format == "xml": # name is outer tagname elif format == "xml": # name is outer tagname
return sendXML(data, tagname=name, force_outer_xml_tag=force_outer_xml_tag) return sendXML(data, tagname=name, force_outer_xml_tag=force_outer_xml_tag, attached=attached)
elif format == "json": elif format == "json":
return sendJSON(data) return sendJSON(data, attached=attached)
else: else:
raise ValueError("invalid format: %s" % format) raise ValueError("invalid format: %s" % format)