diff --git a/app/scodoc/gen_tables.py b/app/scodoc/gen_tables.py index a318f93e..67a52261 100644 --- a/app/scodoc/gen_tables.py +++ b/app/scodoc/gen_tables.py @@ -610,7 +610,6 @@ class GenTable(object): format="html", page_title="", filename=None, - REQUEST=None, javascripts=[], with_html_headers=True, publish=True, @@ -643,35 +642,55 @@ class GenTable(object): H.append(html_sco_header.sco_footer()) return "\n".join(H) elif format == "pdf": - objects = self.pdf() - doc = sco_pdf.pdf_basic_page( - objects, title=title, preferences=self.preferences + pdf_objs = self.pdf() + pdf_doc = sco_pdf.pdf_basic_page( + pdf_objs, title=title, preferences=self.preferences ) if publish: - return scu.sendPDFFile(REQUEST, doc, filename + ".pdf") + return scu.send_file( + pdf_doc, + filename, + suffix=".pdf", + mime=scu.PDF_MIMETYPE, + attached=True, + ) else: - return doc - elif format == "xls" or format == "xlsx": + return pdf_doc + elif format == "xls" or format == "xlsx": # dans les 2 cas retourne du xlsx xls = self.excel() if publish: - return sco_excel.send_excel_file( - REQUEST, xls, filename + scu.XLSX_SUFFIX + return scu.send_file( + xls, + filename, + suffix=scu.XLSX_SUFFIX, + mime=scu.XLSX_MIMETYPE, + attached=True, ) else: return xls elif format == "text": return self.text() elif format == "csv": - return scu.sendCSVFile(REQUEST, self.text(), filename + ".csv") + return scu.send_file( + self.text(), + filename, + suffix=".csv", + mime=scu.CSV_MIMETYPE, + attached=True, + ) elif format == "xml": xml = self.xml() - if REQUEST and publish: - REQUEST.RESPONSE.setHeader("content-type", scu.XML_MIMETYPE) + if publish: + return scu.send_file( + xml, filename, suffix=".xml", mime=scu.XML_MIMETYPE, attached=True + ) return xml elif format == "json": js = self.json() - if REQUEST and publish: - REQUEST.RESPONSE.setHeader("content-type", scu.JSON_MIMETYPE) + if publish: + return scu.send_file( + js, filename, suffix=".json", mime=scu.JSON_MIMETYPE, attached=True + ) return js else: log("make_page: format=%s" % format) @@ -732,5 +751,5 @@ if __name__ == "__main__": document.build(objects) data = doc.getvalue() open("/tmp/gen_table.pdf", "wb").write(data) - p = T.make_page(format="pdf", REQUEST=None) + p = T.make_page(format="pdf") open("toto.pdf", "wb").write(p) diff --git a/app/scodoc/sco_abs_views.py b/app/scodoc/sco_abs_views.py index 8ab6a9a0..a05d42c5 100644 --- a/app/scodoc/sco_abs_views.py +++ b/app/scodoc/sco_abs_views.py @@ -844,9 +844,9 @@ def ListeAbsEtud( # Formats non HTML et demande d'une seule table: if format != "html" and format != "text": if absjust_only == 1: - return tab_absjust.make_page(format=format, REQUEST=REQUEST) + return tab_absjust.make_page(format=format) else: - return tab_absnonjust.make_page(format=format, REQUEST=REQUEST) + return tab_absnonjust.make_page(format=format) if format == "html": # Mise en forme HTML: diff --git a/app/scodoc/sco_archives.py b/app/scodoc/sco_archives.py index 9266724f..ccdbba67 100644 --- a/app/scodoc/sco_archives.py +++ b/app/scodoc/sco_archives.py @@ -47,12 +47,13 @@ qui est une description (humaine, format libre) de l'archive. """ -import os -import time import datetime +import glob +import mimetypes +import os import re import shutil -import glob +import time import flask from flask import g @@ -246,29 +247,13 @@ class BaseArchiver(object): def get_archived_file(self, REQUEST, oid, archive_name, filename): """Recupere donnees du fichier indiqué et envoie au client""" - # XXX très incomplet: devrait inférer et assigner un type MIME archive_id = self.get_id_from_name(oid, archive_name) data = self.get(archive_id, filename) - ext = os.path.splitext(filename.lower())[1] - if ext == ".html" or ext == ".htm": - return data - elif ext == ".xml": - REQUEST.RESPONSE.setHeader("content-type", scu.XML_MIMETYPE) - return data - elif ext == ".xls": - return sco_excel.send_excel_file( - REQUEST, data, filename, mime=scu.XLS_MIMETYPE - ) - elif ext == ".xlsx": - return sco_excel.send_excel_file( - REQUEST, data, filename, mime=scu.XLSX_MIMETYPE - ) - elif ext == ".csv": - return scu.sendCSVFile(REQUEST, data, filename) - elif ext == ".pdf": - return scu.sendPDFFile(REQUEST, data, filename) - REQUEST.RESPONSE.setHeader("content-type", "application/octet-stream") - return data # should set mimetype for known files like images + mime = mimetypes.guess_type(filename)[0] + if mime is None: + mime = "application/octet-stream" + + return scu.send_file(data, filename, mime=mime) class SemsArchiver(BaseArchiver): diff --git a/app/scodoc/sco_cost_formation.py b/app/scodoc/sco_cost_formation.py index f262a653..a702eaed 100644 --- a/app/scodoc/sco_cost_formation.py +++ b/app/scodoc/sco_cost_formation.py @@ -197,4 +197,4 @@ def formsemestre_estim_cost( coef_tp, ) - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) diff --git a/app/scodoc/sco_debouche.py b/app/scodoc/sco_debouche.py index 0a301ebf..df93897c 100644 --- a/app/scodoc/sco_debouche.py +++ b/app/scodoc/sco_debouche.py @@ -70,7 +70,6 @@ def report_debouche_date(start_year=None, format="html", REQUEST=None): init_qtip=True, javascripts=["js/etud_info.js"], format=format, - REQUEST=REQUEST, with_html_headers=True, ) diff --git a/app/scodoc/sco_edit_ue.py b/app/scodoc/sco_edit_ue.py index e2a40ed4..db19d56b 100644 --- a/app/scodoc/sco_edit_ue.py +++ b/app/scodoc/sco_edit_ue.py @@ -1039,7 +1039,7 @@ def formation_table_recap(formation_id, format="html", REQUEST=None): pdf_title=title, preferences=sco_preferences.SemPreferences(), ) - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) def ue_list_semestre_ids(ue): diff --git a/app/scodoc/sco_etape_apogee_view.py b/app/scodoc/sco_etape_apogee_view.py index efa9c007..1b625bcd 100644 --- a/app/scodoc/sco_etape_apogee_view.py +++ b/app/scodoc/sco_etape_apogee_view.py @@ -578,7 +578,7 @@ def _view_etuds_page( preferences=sco_preferences.SemPreferences(), ) if format != "html": - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) H.append(tab.html()) @@ -678,7 +678,8 @@ def view_apo_csv(etape_apo="", semset_id="", format="html", REQUEST=None): sem_id = semset["sem_id"] csv_data = sco_etape_apogee.apo_csv_get(etape_apo, annee_scolaire, sem_id) if format == "raw": - return scu.sendCSVFile(REQUEST, csv_data, etape_apo + ".txt") + scu.send_file(csv_data, etape_apo, suffix=".txt", mime=scu.CSV_MIMETYPE) + apo_data = sco_apogee_csv.ApoData(csv_data, periode=semset["sem_id"]) ( @@ -753,7 +754,7 @@ def view_apo_csv(etape_apo="", semset_id="", format="html", REQUEST=None): ) if format != "html": - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) H += [ tab.html(), diff --git a/app/scodoc/sco_evaluations.py b/app/scodoc/sco_evaluations.py index b9ba65c6..257d1c65 100644 --- a/app/scodoc/sco_evaluations.py +++ b/app/scodoc/sco_evaluations.py @@ -921,7 +921,7 @@ def formsemestre_evaluations_delai_correction( + "", filename=scu.make_filename("evaluations_delais_" + sem["titreannee"]), ) - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) def module_evaluation_insert_before(ModEvals, next_eval): diff --git a/app/scodoc/sco_export_results.py b/app/scodoc/sco_export_results.py index 73f3c27e..7f67e7a0 100644 --- a/app/scodoc/sco_export_results.py +++ b/app/scodoc/sco_export_results.py @@ -246,9 +246,7 @@ def scodoc_table_results( "&types_parcours=".join([str(x) for x in types_parcours]), ) if format != "html": - return tab.make_page( - format=format, with_html_headers=False, REQUEST=REQUEST - ) + return tab.make_page(format=format, with_html_headers=False) tab_html = tab.html() nb_rows = tab.get_nb_rows() else: diff --git a/app/scodoc/sco_find_etud.py b/app/scodoc/sco_find_etud.py index 77109da1..99691c13 100644 --- a/app/scodoc/sco_find_etud.py +++ b/app/scodoc/sco_find_etud.py @@ -404,6 +404,4 @@ def search_inscr_etud_by_nip(code_nip, REQUEST=None, format="json"): ) tab = GenTable(columns_ids=columns_ids, rows=T) - return tab.make_page( - format=format, with_html_headers=False, REQUEST=REQUEST, publish=True - ) + return tab.make_page(format=format, with_html_headers=False, publish=True) diff --git a/app/scodoc/sco_formsemestre.py b/app/scodoc/sco_formsemestre.py index 3bda684a..fe4e8e53 100644 --- a/app/scodoc/sco_formsemestre.py +++ b/app/scodoc/sco_formsemestre.py @@ -583,7 +583,7 @@ def view_formsemestre_by_etape(etape_apo=None, format="html", REQUEST=None): """, ) tab.base_url = "%s?etape_apo=%s" % (REQUEST.URL0, etape_apo or "") - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) def sem_has_etape(sem, code_etape): diff --git a/app/scodoc/sco_formsemestre_status.py b/app/scodoc/sco_formsemestre_status.py index d1b5e8a8..ab422e53 100644 --- a/app/scodoc/sco_formsemestre_status.py +++ b/app/scodoc/sco_formsemestre_status.py @@ -728,7 +728,7 @@ def formsemestre_description( tab.html_before_table += "checked" tab.html_before_table += ">indiquer les évaluations" - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) # genere liste html pour accès aux groupes de ce semestre diff --git a/app/scodoc/sco_groups_view.py b/app/scodoc/sco_groups_view.py index 7ca1788f..1c73b6bc 100644 --- a/app/scodoc/sco_groups_view.py +++ b/app/scodoc/sco_groups_view.py @@ -706,7 +706,7 @@ def groups_table( ): if format == "moodlecsv": format = "csv" - return tab.make_page(format=format, REQUEST=REQUEST) + return tab.make_page(format=format) elif format == "xlsappel": xls = sco_excel.excel_feuille_listeappel( @@ -977,4 +977,4 @@ def export_groups_as_moodle_csv(formsemestre_id=None, REQUEST=None): text_with_titles=prefs["moodle_csv_with_headerline"], preferences=prefs, ) - return tab.make_page(format="csv", REQUEST=REQUEST) + return tab.make_page(format="csv") diff --git a/app/scodoc/sco_liste_notes.py b/app/scodoc/sco_liste_notes.py index 3478433a..bb822aa9 100644 --- a/app/scodoc/sco_liste_notes.py +++ b/app/scodoc/sco_liste_notes.py @@ -482,7 +482,7 @@ def _make_table_notes( # html_generate_cells=False # la derniere ligne (moyennes) est incomplete ) - t = tab.make_page(format=format, with_html_headers=False, REQUEST=REQUEST) + t = tab.make_page(format=format, with_html_headers=False) if format != "html": return t diff --git a/app/scodoc/sco_lycee.py b/app/scodoc/sco_lycee.py index 677738f7..99f939b4 100644 --- a/app/scodoc/sco_lycee.py +++ b/app/scodoc/sco_lycee.py @@ -85,7 +85,7 @@ def scodoc_table_etuds_lycees(format="html", REQUEST=None): no_links=True, ) tab.base_url = REQUEST.URL0 - t = tab.make_page(format=format, with_html_headers=False, REQUEST=REQUEST) + t = tab.make_page(format=format, with_html_headers=False) if format != "html": return t H = [ @@ -192,7 +192,7 @@ def formsemestre_etuds_lycees( tab.base_url += "&only_primo=1" if no_grouping: tab.base_url += "&no_grouping=1" - t = tab.make_page(format=format, with_html_headers=False, REQUEST=REQUEST) + t = tab.make_page(format=format, with_html_headers=False) if format != "html": return t F = [ diff --git a/app/scodoc/sco_placement.py b/app/scodoc/sco_placement.py index bf474883..d4a68e81 100644 --- a/app/scodoc/sco_placement.py +++ b/app/scodoc/sco_placement.py @@ -393,7 +393,7 @@ def do_placement(REQUEST): preferences=sco_preferences.SemPreferences(M["formsemestre_id"]), # html_generate_cells=False # la derniere ligne (moyennes) est incomplete ) - t = tab.make_page(format="pdf", with_html_headers=False, REQUEST=REQUEST) + t = tab.make_page(format="pdf", with_html_headers=False) return t diff --git a/app/scodoc/sco_poursuite_dut.py b/app/scodoc/sco_poursuite_dut.py index bbc39e1e..b2fb7d11 100644 --- a/app/scodoc/sco_poursuite_dut.py +++ b/app/scodoc/sco_poursuite_dut.py @@ -217,6 +217,5 @@ def formsemestre_poursuite_report(formsemestre_id, format="html", REQUEST=None): init_qtip=True, javascripts=["js/etud_info.js"], format=format, - REQUEST=REQUEST, with_html_headers=True, ) diff --git a/app/scodoc/sco_pvjury.py b/app/scodoc/sco_pvjury.py index f06180f4..affe43f4 100644 --- a/app/scodoc/sco_pvjury.py +++ b/app/scodoc/sco_pvjury.py @@ -535,7 +535,6 @@ def formsemestre_pvjury(formsemestre_id, format="html", publish=True, REQUEST=No return tab.make_page( format=format, with_html_headers=False, - REQUEST=REQUEST, publish=publish, ) tab.base_url = "%s?formsemestre_id=%s" % (REQUEST.URL0, formsemestre_id) diff --git a/app/scodoc/sco_recapcomplet.py b/app/scodoc/sco_recapcomplet.py index 86579f8c..a01a6059 100644 --- a/app/scodoc/sco_recapcomplet.py +++ b/app/scodoc/sco_recapcomplet.py @@ -27,8 +27,9 @@ """Tableau recapitulatif des notes d'un semestre """ -import time import datetime +import json +import time from xml.etree import ElementTree import app.scodoc.sco_utils as scu @@ -227,11 +228,14 @@ def do_formsemestre_recapcomplet( if format == "xml" or format == "html": return data elif format == "csv": - return scu.sendCSVFile(REQUEST, data, filename) - elif format[:3] == "xls": - return sco_excel.send_excel_file(REQUEST, data, filename) + return scu.send_file(data, filename=filename, mime=scu.CSV_MIMETYPE) + elif format[:3] == "xls" or format[:3] == "xlsx": + return scu.send_file(data, filename=filename, mime=scu.XLSX_MIMETYPE) elif format == "json": - return scu.sendJSON(REQUEST, data) + js = json.dumps(data, indent=1, cls=scu.ScoDocJSONEncoder) + return scu.send_file( + js, filename=filename, suffix=scu.JSON_SUFFIX, mime=scu.JSON_MIMETYPE + ) else: raise ValueError("unknown format %s" % format) diff --git a/app/scodoc/sco_report.py b/app/scodoc/sco_report.py index 2bf2fa1e..193b0eb6 100644 --- a/app/scodoc/sco_report.py +++ b/app/scodoc/sco_report.py @@ -355,7 +355,6 @@ def formsemestre_report_counts( t = tab.make_page( title="""