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:
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):

View File

@ -553,28 +553,28 @@ class ScoDocJSONEncoder(json.JSONEncoder):
return json.JSONEncoder.default(self, o)
def sendJSON(data):
def sendJSON(data, attached=False):
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:
data = [data] # always list-of-dicts
if force_outer_xml_tag:
data = [{tagname: data}]
tagname += "_list"
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"):
return data
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":
return sendJSON(data)
return sendJSON(data, attached=attached)
else:
raise ValueError("invalid format: %s" % format)