From 00dbd25b42d303a36187f6ebb5bb1b92b3fc6999 Mon Sep 17 00:00:00 2001 From: Place Jean-Marie Date: Sat, 25 Sep 2021 09:53:31 +0200 Subject: [PATCH] enable xml/json result as file --- app/scodoc/sco_formations.py | 2 +- app/scodoc/sco_utils.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/scodoc/sco_formations.py b/app/scodoc/sco_formations.py index b9cb6f2c1..019d7833d 100644 --- a/app/scodoc/sco_formations.py +++ b/app/scodoc/sco_formations.py @@ -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): diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index 6efdd7d49..af91fc4b5 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -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)