enhance scu.send_file, fix some API bugs.

This commit is contained in:
Emmanuel Viennet 2021-09-21 22:19:08 +02:00
parent 13f1539282
commit eded2fffe9
10 changed files with 24 additions and 30 deletions

View File

@ -652,7 +652,6 @@ class GenTable(object):
filename,
suffix=".pdf",
mime=scu.PDF_MIMETYPE,
attached=True,
)
else:
return pdf_doc
@ -664,7 +663,6 @@ class GenTable(object):
filename,
suffix=scu.XLSX_SUFFIX,
mime=scu.XLSX_MIMETYPE,
attached=True,
)
else:
return xls
@ -682,14 +680,14 @@ class GenTable(object):
xml = self.xml()
if publish:
return scu.send_file(
xml, filename, suffix=".xml", mime=scu.XML_MIMETYPE, attached=True
xml, filename, suffix=".xml", mime=scu.XML_MIMETYPE
)
return xml
elif format == "json":
js = self.json()
if publish:
return scu.send_file(
js, filename, suffix=".json", mime=scu.JSON_MIMETYPE, attached=True
js, filename, suffix=".json", mime=scu.JSON_MIMETYPE
)
return js
else:

View File

@ -269,9 +269,8 @@ def etudarchive_generate_excel_sample(group_id=None, REQUEST=None):
return scu.send_file(
data,
"ImportFichiersEtudiants",
scu.XLSX_SUFFIX,
scu.XLSX_MIMETYPE,
attached=True,
suffix=scu.XLSX_SUFFIX,
mime=scu.XLSX_MIMETYPE,
)
# return sco_excel.send_excel_file(REQUEST, data, "ImportFichiersEtudiants" + scu.XLSX_SUFFIX)

View File

@ -790,7 +790,7 @@ def formsemestre_bulletinetud(
)[0]
if format not in {"html", "pdfmail"}:
filename = scu.bul_filename(sem, etud, format)
return scu.send_file(bulletin, filename, mime=scu.get_mime_suffix(format))
return scu.send_file(bulletin, filename, mime=scu.get_mime_suffix(format)[0])
sem = sco_formsemestre.get_formsemestre(formsemestre_id)
H = [

View File

@ -723,9 +723,7 @@ def groups_table(
server_name=request.url_root,
)
filename = "liste_%s" % groups_infos.groups_filename
return scu.send_file(
xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
)
return scu.send_file(xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE)
elif format == "allxls":
# feuille Excel avec toutes les infos etudiants
if not groups_infos.members:
@ -794,9 +792,7 @@ def groups_table(
title = "etudiants_%s" % groups_infos.groups_filename
xls = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title)
filename = title + scu.XLSX_SUFFIX
return scu.send_file(
xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
)
return scu.send_file(xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE)
else:
raise ValueError("unsupported format")

View File

@ -339,7 +339,7 @@ class PlacementRunner:
def _production_xls(self):
filename = "placement_%s_%s" % (self.evalname, self.gr_title_filename)
xls = self._excel_feuille_placement()
return scu.send_file(xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE)
return scu.send_file(xls, filename, scu.XLSX_SUFFIX, mime=scu.XLSX_MIMETYPE)
def _production_pdf(self):
pdf_title = "<br/>".join(self.desceval)

View File

@ -330,7 +330,5 @@ def feuille_preparation_jury(formsemestre_id, REQUEST):
xls,
f"PrepaJury{sn}{scu.XLSX_SUFFIX}",
scu.XLSX_SUFFIX,
scu.XLSX_MIMETYPE,
attached=True,
mime=scu.XLSX_MIMETYPE,
)
# return sco_excel.send_excel_file(REQUEST, xls, f"PrepaJury{sn}{scu.XLSX_SUFFIX}")

View File

@ -829,9 +829,7 @@ def feuille_saisie_notes(evaluation_id, group_ids=[], REQUEST=None):
filename = "notes_%s_%s.xlsx" % (evalname, gr_title_filename)
xls = sco_excel.excel_feuille_saisie(E, sem["titreannee"], description, lines=L)
return scu.send_file(
xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
)
return scu.send_file(xls, filename, scu.XLSX_SUFFIX, mime=scu.XLSX_MIMETYPE)
# return sco_excel.send_excel_file(REQUEST, xls, filename)

View File

@ -540,7 +540,8 @@ def sendCSVFile(REQUEST, data, filename): # DEPRECATED ne plus utiliser
def sendPDFFile(data, filename):
filename = make_filename(filename)
return send_file(data, filename=filename, mime=JSON_MIMETYPE, attached=True)
return send_file(data, filename=filename, mime=PDF_MIMETYPE, attached=True)
class ScoDocJSONEncoder(json.JSONEncoder):
def default(self, o): # pylint: disable=E0202
@ -578,8 +579,15 @@ def sendResult(data, name=None, format=None, force_outer_xml_tag=True):
raise ValueError("invalid format: %s" % format)
def send_file(data, filename, suffix="", mime=None, attached=True):
"Build Flask Response for file download of given type"
def send_file(data, filename, suffix="", mime=None, attached=None):
"""Build Flask Response for file download of given type
By default (attached is None), json and xml are inlined and otrher types are atteched.
"""
if attached is None:
if mime == XML_MIMETYPE or mime == JSON_MIMETYPE:
attached = True
else:
attached = False
if suffix:
filename += suffix
filename = make_filename(filename)

View File

@ -1931,7 +1931,7 @@ def import_generate_excel_sample(REQUEST, with_codesemestre="1"):
format, with_codesemestre, exclude_cols=["photo_filename"]
)
return scu.send_file(
data, "ImportEtudiants", scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
data, "ImportEtudiants", scu.XLSX_SUFFIX, mime=scu.XLSX_MIMETYPE
)
# return sco_excel.send_excel_file(REQUEST, data, "ImportEtudiants" + scu.XLSX_SUFFIX)
@ -1952,7 +1952,7 @@ def import_generate_admission_sample(REQUEST, formsemestre_id):
group_ids=[group["group_id"]],
)
return scu.send_file(
data, "AdmissionEtudiants", scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
data, "AdmissionEtudiants", scu.XLSX_SUFFIX, mime=scu.XLSX_MIMETYPE
)
# return sco_excel.send_excel_file(REQUEST, data, "AdmissionEtudiants" + scu.XLSX_SUFFIX)

View File

@ -488,10 +488,7 @@ def create_user_form(REQUEST, user_name=None, edit=0, all_roles=1):
def import_users_generate_excel_sample(REQUEST):
"une feuille excel pour importation utilisateurs"
data = sco_import_users.generate_excel_sample()
return scu.send_file(
data, "ImportUtilisateurs", scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
)
# return sco_excel.send_excel_file(REQUEST, data, "ImportUtilisateurs" + scu.XLSX_SUFFIX)
return scu.send_file(data, "ImportUtilisateurs", scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE)
@bp.route("/import_users_form", methods=["GET", "POST"])