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

View File

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

View File

@ -790,7 +790,7 @@ def formsemestre_bulletinetud(
)[0] )[0]
if format not in {"html", "pdfmail"}: if format not in {"html", "pdfmail"}:
filename = scu.bul_filename(sem, etud, format) 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) sem = sco_formsemestre.get_formsemestre(formsemestre_id)
H = [ H = [

View File

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

View File

@ -339,7 +339,7 @@ class PlacementRunner:
def _production_xls(self): def _production_xls(self):
filename = "placement_%s_%s" % (self.evalname, self.gr_title_filename) filename = "placement_%s_%s" % (self.evalname, self.gr_title_filename)
xls = self._excel_feuille_placement() 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): def _production_pdf(self):
pdf_title = "<br/>".join(self.desceval) pdf_title = "<br/>".join(self.desceval)

View File

@ -330,7 +330,5 @@ def feuille_preparation_jury(formsemestre_id, REQUEST):
xls, xls,
f"PrepaJury{sn}{scu.XLSX_SUFFIX}", f"PrepaJury{sn}{scu.XLSX_SUFFIX}",
scu.XLSX_SUFFIX, scu.XLSX_SUFFIX,
scu.XLSX_MIMETYPE, mime=scu.XLSX_MIMETYPE,
attached=True,
) )
# 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) filename = "notes_%s_%s.xlsx" % (evalname, gr_title_filename)
xls = sco_excel.excel_feuille_saisie(E, sem["titreannee"], description, lines=L) xls = sco_excel.excel_feuille_saisie(E, sem["titreannee"], description, lines=L)
return scu.send_file( return scu.send_file(xls, filename, scu.XLSX_SUFFIX, mime=scu.XLSX_MIMETYPE)
xls, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
)
# return sco_excel.send_excel_file(REQUEST, xls, filename) # 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): def sendPDFFile(data, filename):
filename = make_filename(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): class ScoDocJSONEncoder(json.JSONEncoder):
def default(self, o): # pylint: disable=E0202 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) raise ValueError("invalid format: %s" % format)
def send_file(data, filename, suffix="", mime=None, attached=True): def send_file(data, filename, suffix="", mime=None, attached=None):
"Build Flask Response for file download of given type" """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: if suffix:
filename += suffix filename += suffix
filename = make_filename(filename) 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"] format, with_codesemestre, exclude_cols=["photo_filename"]
) )
return scu.send_file( 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) # 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"]], group_ids=[group["group_id"]],
) )
return scu.send_file( 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) # 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): def import_users_generate_excel_sample(REQUEST):
"une feuille excel pour importation utilisateurs" "une feuille excel pour importation utilisateurs"
data = sco_import_users.generate_excel_sample() data = sco_import_users.generate_excel_sample()
return scu.send_file( return scu.send_file(data, "ImportUtilisateurs", scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE)
data, "ImportUtilisateurs", scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE, attached=True
)
# return sco_excel.send_excel_file(REQUEST, data, "ImportUtilisateurs" + scu.XLSX_SUFFIX)
@bp.route("/import_users_form", methods=["GET", "POST"]) @bp.route("/import_users_form", methods=["GET", "POST"])