From c5c0b510ec599a2486d4d39d9bc4519c7c51d41d Mon Sep 17 00:00:00 2001
From: Emmanuel Viennet <emmanuel.viennet@gmail.com>
Date: Tue, 1 Mar 2022 09:48:37 +0100
Subject: [PATCH] filename export formations

---
 app/scodoc/sco_formations.py |  8 +++++++-
 app/scodoc/sco_utils.py      | 21 ++++++++++++++++-----
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/app/scodoc/sco_formations.py b/app/scodoc/sco_formations.py
index caeeb707b..573c95dfb 100644
--- a/app/scodoc/sco_formations.py
+++ b/app/scodoc/sco_formations.py
@@ -151,8 +151,14 @@ def formation_export(
                 if mod["ects"] is None:
                     del mod["ects"]
 
+    filename = f"scodoc_formation_{formation.departement.acronym}_{formation.acronyme or ''}_v{formation.version}"
     return scu.sendResult(
-        F, name="formation", format=format, force_outer_xml_tag=False, attached=True
+        F,
+        name="formation",
+        format=format,
+        force_outer_xml_tag=False,
+        attached=True,
+        filename=filename,
     )
 
 
diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py
index b3d99ac3e..7af19bc19 100644
--- a/app/scodoc/sco_utils.py
+++ b/app/scodoc/sco_utils.py
@@ -645,21 +645,30 @@ class ScoDocJSONEncoder(json.JSONEncoder):
             return json.JSONEncoder.default(self, o)
 
 
-def sendJSON(data, attached=False):
+def sendJSON(data, attached=False, filename=None):
     js = json.dumps(data, indent=1, cls=ScoDocJSONEncoder)
     return send_file(
-        js, filename="sco_data.json", mime=JSON_MIMETYPE, attached=attached
+        js, filename=filename or "sco_data.json", mime=JSON_MIMETYPE, attached=attached
     )
 
 
-def sendXML(data, tagname=None, force_outer_xml_tag=True, attached=False, quote=True):
+def sendXML(
+    data,
+    tagname=None,
+    force_outer_xml_tag=True,
+    attached=False,
+    quote=True,
+    filename=None,
+):
     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, quote=quote)
-    return send_file(doc, filename="sco_data.xml", mime=XML_MIMETYPE, attached=attached)
+    return send_file(
+        doc, filename=filename or "sco_data.xml", mime=XML_MIMETYPE, attached=attached
+    )
 
 
 def sendResult(
@@ -669,6 +678,7 @@ def sendResult(
     force_outer_xml_tag=True,
     attached=False,
     quote_xml=True,
+    filename=None,
 ):
     if (format is None) or (format == "html"):
         return data
@@ -679,9 +689,10 @@ def sendResult(
             force_outer_xml_tag=force_outer_xml_tag,
             attached=attached,
             quote=quote_xml,
+            filename=filename,
         )
     elif format == "json":
-        return sendJSON(data, attached=attached)
+        return sendJSON(data, attached=attached, filename=filename)
     else:
         raise ValueError("invalid format: %s" % format)