From eded2fffe995d415713a082d75d1212241dba4ce Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 21 Sep 2021 22:19:08 +0200 Subject: [PATCH] enhance scu.send_file, fix some API bugs. --- app/scodoc/gen_tables.py | 6 ++---- app/scodoc/sco_archives_etud.py | 5 ++--- app/scodoc/sco_bulletins.py | 2 +- app/scodoc/sco_groups_view.py | 8 ++------ app/scodoc/sco_placement.py | 2 +- app/scodoc/sco_prepajury.py | 4 +--- app/scodoc/sco_saisie_notes.py | 4 +--- app/scodoc/sco_utils.py | 14 +++++++++++--- app/views/scolar.py | 4 ++-- app/views/users.py | 5 +---- 10 files changed, 24 insertions(+), 30 deletions(-) diff --git a/app/scodoc/gen_tables.py b/app/scodoc/gen_tables.py index c199d0b3e..223feddaf 100644 --- a/app/scodoc/gen_tables.py +++ b/app/scodoc/gen_tables.py @@ -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: diff --git a/app/scodoc/sco_archives_etud.py b/app/scodoc/sco_archives_etud.py index 04b043c52..0ea44268c 100644 --- a/app/scodoc/sco_archives_etud.py +++ b/app/scodoc/sco_archives_etud.py @@ -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) diff --git a/app/scodoc/sco_bulletins.py b/app/scodoc/sco_bulletins.py index 27fe7c05f..cb2225e86 100644 --- a/app/scodoc/sco_bulletins.py +++ b/app/scodoc/sco_bulletins.py @@ -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 = [ diff --git a/app/scodoc/sco_groups_view.py b/app/scodoc/sco_groups_view.py index 76039bea0..f14bc5586 100644 --- a/app/scodoc/sco_groups_view.py +++ b/app/scodoc/sco_groups_view.py @@ -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") diff --git a/app/scodoc/sco_placement.py b/app/scodoc/sco_placement.py index a950c2152..ac39c2bba 100644 --- a/app/scodoc/sco_placement.py +++ b/app/scodoc/sco_placement.py @@ -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 = "
".join(self.desceval) diff --git a/app/scodoc/sco_prepajury.py b/app/scodoc/sco_prepajury.py index 183b4551c..4cee94092 100644 --- a/app/scodoc/sco_prepajury.py +++ b/app/scodoc/sco_prepajury.py @@ -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}") diff --git a/app/scodoc/sco_saisie_notes.py b/app/scodoc/sco_saisie_notes.py index 213a487a6..9aca6f1f8 100644 --- a/app/scodoc/sco_saisie_notes.py +++ b/app/scodoc/sco_saisie_notes.py @@ -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) diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index 3d19ba92e..27c9ea709 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -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) diff --git a/app/views/scolar.py b/app/views/scolar.py index 055cf7566..f6af76871 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -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) diff --git a/app/views/users.py b/app/views/users.py index 73b877c7a..7f18420f7 100644 --- a/app/views/users.py +++ b/app/views/users.py @@ -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"])