From fbdcf63c70f0992696e4aac6baffb5d070e8826e Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 12 Aug 2021 14:49:53 +0200 Subject: [PATCH] Fix: API sco_excel + suffix xslx --- app/scodoc/gen_tables.py | 4 +- app/scodoc/pe_jurype.py | 2 +- app/scodoc/pe_view.py | 4 +- app/scodoc/sco_apogee_csv.py | 4 +- app/scodoc/sco_archives.py | 12 ++++-- app/scodoc/sco_archives_etud.py | 4 +- app/scodoc/sco_excel.py | 10 +++-- app/scodoc/sco_groups_view.py | 17 +++++--- app/scodoc/sco_import_users.py | 4 +- app/scodoc/sco_placement.py | 74 ++++++++++++++++----------------- app/scodoc/sco_prepajury.py | 14 +++---- app/scodoc/sco_recapcomplet.py | 10 ++--- app/scodoc/sco_synchro_etuds.py | 2 +- app/scodoc/sco_trombino.py | 2 +- app/scodoc/sco_utils.py | 1 + app/views/scolar.py | 6 ++- 16 files changed, 95 insertions(+), 75 deletions(-) diff --git a/app/scodoc/gen_tables.py b/app/scodoc/gen_tables.py index 19d7f6de..50d76a3d 100644 --- a/app/scodoc/gen_tables.py +++ b/app/scodoc/gen_tables.py @@ -653,7 +653,9 @@ class GenTable(object): elif format == "xls" or format == "xlsx": xls = self.excel() if publish: - return sco_excel.send_excel_file(REQUEST, xls, filename + ".xls") + return sco_excel.send_excel_file( + REQUEST, xls, filename + scu.XLSX_SUFFIX + ) else: return xls elif format == "text": diff --git a/app/scodoc/pe_jurype.py b/app/scodoc/pe_jurype.py index 73440bb8..5733dce2 100644 --- a/app/scodoc/pe_jurype.py +++ b/app/scodoc/pe_jurype.py @@ -194,7 +194,7 @@ class JuryPE(object): # self.add_file_to_zip(filename, self.xls.excel()) # Fabrique 1 fichier excel résultat avec 1 seule feuille => trop gros - filename = self.NOM_EXPORT_ZIP + "_jurySyntheseDict" + ".xls" + filename = self.NOM_EXPORT_ZIP + "_jurySyntheseDict" + scu.XLSX_SUFFIX self.xlsV2 = self.table_syntheseJury(mode="multiplesheet") if self.xlsV2: self.add_file_to_zip(filename, self.xlsV2.excel()) diff --git a/app/scodoc/pe_view.py b/app/scodoc/pe_view.py index e98faf50..788de9b1 100644 --- a/app/scodoc/pe_view.py +++ b/app/scodoc/pe_view.py @@ -142,7 +142,9 @@ def pe_view_sem_recap( context, jury.syntheseJury, tag_annotation_pe ) if sT: - jury.add_file_to_zip(jury.NOM_EXPORT_ZIP + "_annotationsPE.xls", sT.excel()) + jury.add_file_to_zip( + jury.NOM_EXPORT_ZIP + "_annotationsPE" + scu.XLSX_SUFFIX, sT.excel() + ) latex_pages = {} # Dictionnaire de la forme nom_fichier => contenu_latex for etudid in etudids: diff --git a/app/scodoc/sco_apogee_csv.py b/app/scodoc/sco_apogee_csv.py index 7c1277e8..3b905502 100644 --- a/app/scodoc/sco_apogee_csv.py +++ b/app/scodoc/sco_apogee_csv.py @@ -1266,8 +1266,8 @@ def export_csv_to_apogee( nf += 1 log_filename = "scodoc-" + basename + ".log.txt" - nar_filename = basename + "-nar.xls" - cr_filename = basename + "-decisions.xls" + nar_filename = basename + "-nar" + scu.XLSX_SUFFIX + cr_filename = basename + "-decisions" + scu.XLSX_SUFFIX logf = StringIO() logf.write("export_to_apogee du %s\n\n" % time.ctime()) diff --git a/app/scodoc/sco_archives.py b/app/scodoc/sco_archives.py index 469bf060..4eca9d43 100644 --- a/app/scodoc/sco_archives.py +++ b/app/scodoc/sco_archives.py @@ -237,7 +237,13 @@ class BaseArchiver(object): REQUEST.RESPONSE.setHeader("content-type", scu.XML_MIMETYPE) return data elif ext == ".xls": - return sco_excel.sendExcelFile(REQUEST, data, filename) + 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": @@ -297,7 +303,7 @@ def do_formsemestre_archive( context, REQUEST, formsemestre_id, format="xls" ) if data: - PVArchive.store(archive_id, "Tableau_moyennes.xls", data) + PVArchive.store(archive_id, "Tableau_moyennes" + scu.XLSX_SUFFIX, data) # Tableau recap notes en HTML (pour tous les etudiants, n'utilise pas les groupes) data, _, _ = make_formsemestre_recapcomplet( context, REQUEST, formsemestre_id, format="html", disable_etudlink=True @@ -329,7 +335,7 @@ def do_formsemestre_archive( context, formsemestre_id, format="xls", REQUEST=REQUEST, publish=False ) if data: - PVArchive.store(archive_id, "Decisions_Jury.xls", data) + PVArchive.store(archive_id, "Decisions_Jury" + scu.XLSX_SUFFIX, data) # Classeur bulletins (PDF) data, _ = sco_bulletins_pdf.get_formsemestre_bulletins_pdf( context, formsemestre_id, REQUEST, version=bulVersion diff --git a/app/scodoc/sco_archives_etud.py b/app/scodoc/sco_archives_etud.py index 94aad219..f08c2d18 100644 --- a/app/scodoc/sco_archives_etud.py +++ b/app/scodoc/sco_archives_etud.py @@ -256,7 +256,9 @@ def etudarchive_generate_excel_sample(context, group_id=None, REQUEST=None): extra_cols=["fichier_a_charger"], REQUEST=REQUEST, ) - return sco_excel.sendExcelFile(REQUEST, data, "ImportFichiersEtudiants.xls") + return sco_excel.send_excel_file( + REQUEST, data, "ImportFichiersEtudiants" + scu.XLSX_SUFFIX + ) def etudarchive_import_files_form(context, group_id, REQUEST=None): diff --git a/app/scodoc/sco_excel.py b/app/scodoc/sco_excel.py index b7111a32..19439b06 100644 --- a/app/scodoc/sco_excel.py +++ b/app/scodoc/sco_excel.py @@ -56,7 +56,7 @@ class COLORS(Enum): LIGHT_YELLOW = "FFFFFF99" -def send_excel_file(request, data, filename): +def send_excel_file(request, data, filename, mime=scu.XLSX_MIMETYPE): """publication fichier. (on ne doit rien avoir émis avant, car ici sont générés les entetes) """ @@ -65,7 +65,7 @@ def send_excel_file(request, data, filename): .replace("&", "") .replace(" ", "_") ) - request.RESPONSE.setHeader("content-type", scu.XLSX_MIMETYPE) + request.RESPONSE.setHeader("content-type", mime) request.RESPONSE.setHeader( "content-disposition", 'attachment; filename="%s"' % filename ) @@ -131,6 +131,7 @@ class ScoExcelBook: ... steam = wb.generate() """ + def __init__(self): self.sheets = [] # list of sheets @@ -139,7 +140,7 @@ class ScoExcelBook: self.sheets.append(sheet) def generate(self): - """ génération d'un stream binaire représentant la totalité du classeur. + """génération d'un stream binaire représentant la totalité du classeur. retourne le flux """ wb = Workbook(write_only=True) @@ -206,6 +207,7 @@ class ScoExcelSheet: ligne de haut en bas et cellules de gauche à droite (i.e. A1, A2, .. B1, B2, ..) * pour finit appel de la méthode de génération """ + def __init__(self, sheet_name="feuille", default_style=None, wb=None): """Création de la feuille. sheet_name -- le nom de la feuille @@ -557,7 +559,7 @@ def _excel_to_list(filelike): # we may need 'encoding' argument ? wb = load_workbook(filename=filelike, read_only=True, data_only=True) except: log("Excel_to_list: failure to import document") - open("/tmp/last_scodoc_import_failure.xls", "w").write(filelike) + open("/tmp/last_scodoc_import_failure" + scu.XLSX_SUFFIX, "w").write(filelike) raise ScoValueError( "Fichier illisible: assurez-vous qu'il s'agit bien d'un document Excel !" ) diff --git a/app/scodoc/sco_groups_view.py b/app/scodoc/sco_groups_view.py index 708038b4..e90c96dd 100644 --- a/app/scodoc/sco_groups_view.py +++ b/app/scodoc/sco_groups_view.py @@ -724,10 +724,15 @@ def groups_table( return tab.make_page(context, format=format, REQUEST=REQUEST) elif format == "xlsappel": - xls = sco_excel.excel_feuille_listeappel(groups_infos.formsemestre, groups_infos.groups_titles, - groups_infos.members, partitions=groups_infos.partitions, - with_codes=with_codes, with_paiement=with_paiement, - server_name=REQUEST.BASE0) + xls = sco_excel.excel_feuille_listeappel( + groups_infos.formsemestre, + groups_infos.groups_titles, + groups_infos.members, + partitions=groups_infos.partitions, + with_codes=with_codes, + with_paiement=with_paiement, + server_name=REQUEST.BASE0, + ) filename = "liste_%s" % groups_infos.groups_filename + ".xlsx" return sco_excel.send_excel_file(REQUEST, xls, filename) elif format == "allxls": @@ -798,8 +803,8 @@ def groups_table( L = [dicttakestr(m, keys) for m in groups_infos.members] title = "etudiants_%s" % groups_infos.groups_filename - xls = sco_excel.Excel_SimpleTable(titles=titles, lines=L, SheetName=title) - filename = title + ".xls" + xls = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) + filename = title + scu.XLSX_SUFFIX return sco_excel.send_excel_file(REQUEST, xls, filename) else: raise ValueError("unsupported format") diff --git a/app/scodoc/sco_import_users.py b/app/scodoc/sco_import_users.py index 100cdb50..1b36ce1e 100644 --- a/app/scodoc/sco_import_users.py +++ b/app/scodoc/sco_import_users.py @@ -49,8 +49,8 @@ def generate_excel_sample(): style = sco_excel.Excel_MakeStyle(bold=True) titles = TITLES titlesStyles = [style] * len(titles) - return sco_excel.Excel_SimpleTable( - titles=titles, titlesStyles=titlesStyles, SheetName="Utilisateurs ScoDoc" + return sco_excel.excel_simple_table( + titles=titles, titlesStyles=titlesStyles, sheet_name="Utilisateurs ScoDoc" ) diff --git a/app/scodoc/sco_placement.py b/app/scodoc/sco_placement.py index 30705498..4de9b17a 100644 --- a/app/scodoc/sco_placement.py +++ b/app/scodoc/sco_placement.py @@ -322,11 +322,11 @@ def do_placement(context, REQUEST): maxlines = sem_preferences.get("feuille_placement_positions") if placement_method == "xls": - filename = "placement_%s_%s.xls" % (evalname, gr_title_filename) + filename = f"placement_{evalname}_{gr_title_filename}{scu.XLSX_SUFFIX}" xls = Excel_feuille_placement( E, desceval, listetud, columns, space, maxlines, building, room, numbering ) - return sco_excel.sendExcelFile(REQUEST, xls, filename) + return sco_excel.send_excel_file(REQUEST, xls, filename) else: nbcolumns = int(columns) @@ -663,8 +663,8 @@ def Excel_feuille_placement( li = 0 line = 0 dt = time.strftime("%d/%m/%Y a %Hh%M") - ws0.write(li, 0, u"Feuille placement etudiants éditée le %s" % dt, style_titres) - ws1.write(li, 0, u"Feuille placement etudiants éditée le %s" % dt, style_titres) + ws0.write(li, 0, "Feuille placement etudiants éditée le %s" % dt, style_titres) + ws1.write(li, 0, "Feuille placement etudiants éditée le %s" % dt, style_titres) for desceval in description: if line % 2 == 0: li += 2 @@ -677,41 +677,39 @@ def Excel_feuille_placement( ws0.write( li, 0, - u"Date : %s - Horaire : %s à %s" - % (E["jour"], E["heure_debut"], E["heure_fin"]), + "Date : %s - Horaire : %s à %s" % (E["jour"], E["heure_debut"], E["heure_fin"]), style_titres, ) ws1.write( li, 0, - u"Date : %s - Horaire : %s à %s" - % (E["jour"], E["heure_debut"], E["heure_fin"]), + "Date : %s - Horaire : %s à %s" % (E["jour"], E["heure_debut"], E["heure_fin"]), style_titres, ) li += 1 # entetes colonnes - feuille0 for col in range(nbcolumns): - ws0.write(li, col + 1, u"colonne %s" % (col + 1), style2b) + ws0.write(li, col + 1, "colonne %s" % (col + 1), style2b) # entetes colonnes - feuille1 if numbering == "coordinate": - ws1.write(li, 0, u"Nom", style2bi) - ws1.write(li, 1, u"Prénom", style2bi) - ws1.write(li, 2, u"Colonne", style2bi) - ws1.write(li, 3, u"Ligne", style2bi) + ws1.write(li, 0, "Nom", style2bi) + ws1.write(li, 1, "Prénom", style2bi) + ws1.write(li, 2, "Colonne", style2bi) + ws1.write(li, 3, "Ligne", style2bi) - ws1.write(li, 5, u"Nom", style2bi) - ws1.write(li, 6, u"Prénom", style2bi) - ws1.write(li, 7, u"Colonne", style2bi) - ws1.write(li, 8, u"Ligne", style2bi) + ws1.write(li, 5, "Nom", style2bi) + ws1.write(li, 6, "Prénom", style2bi) + ws1.write(li, 7, "Colonne", style2bi) + ws1.write(li, 8, "Ligne", style2bi) else: - ws1.write(li, 0, u"Nom", style2bi) - ws1.write(li, 1, u"Prénom", style2bi) - ws1.write(li, 2, u"Place", style2bi) + ws1.write(li, 0, "Nom", style2bi) + ws1.write(li, 1, "Prénom", style2bi) + ws1.write(li, 2, "Place", style2bi) - ws1.write(li, 4, u"Nom", style2bi) - ws1.write(li, 5, u"Prénom", style2bi) - ws1.write(li, 6, u"Place", style2bi) + ws1.write(li, 4, "Nom", style2bi) + ws1.write(li, 5, "Prénom", style2bi) + ws1.write(li, 6, "Place", style2bi) # etudiants line = 1 @@ -751,7 +749,7 @@ def Excel_feuille_placement( ws0.write(li0 + 2, col, " ", style1bb) else: ws0.write( - li0 + 2, col, u"place %s" % (col + (line - 1) * nbcolumns), style1bb + li0 + 2, col, "place %s" % (col + (line - 1) * nbcolumns), style1bb ) # ws0.write(li+3,col, ' ', style1bm ) # ws0.write(li+4,col, ' ', style1bb ) @@ -793,21 +791,21 @@ def Excel_feuille_placement( nbcol = 0 col = 0 if numbering == "coordinate": - ws1.write(li, 0, u"Nom", style2bi) - ws1.write(li, 1, u"Prénom", style2bi) - ws1.write(li, 2, u"Colonne", style2bi) - ws1.write(li, 3, u"Ligne", style2bi) + ws1.write(li, 0, "Nom", style2bi) + ws1.write(li, 1, "Prénom", style2bi) + ws1.write(li, 2, "Colonne", style2bi) + ws1.write(li, 3, "Ligne", style2bi) - ws1.write(li, 5, u"Nom", style2bi) - ws1.write(li, 6, u"Prénom", style2bi) - ws1.write(li, 7, u"Colonne", style2bi) - ws1.write(li, 8, u"Ligne", style2bi) + ws1.write(li, 5, "Nom", style2bi) + ws1.write(li, 6, "Prénom", style2bi) + ws1.write(li, 7, "Colonne", style2bi) + ws1.write(li, 8, "Ligne", style2bi) else: - ws1.write(li, 0, u"Nom", style2bi) - ws1.write(li, 1, u"Prénom", style2bi) - ws1.write(li, 2, u"Place", style2bi) + ws1.write(li, 0, "Nom", style2bi) + ws1.write(li, 1, "Prénom", style2bi) + ws1.write(li, 2, "Place", style2bi) - ws1.write(li, 4, u"Nom", style2bi) - ws1.write(li, 5, u"Prénom", style2bi) - ws1.write(li, 6, u"Place", style2bi) + ws1.write(li, 4, "Nom", style2bi) + ws1.write(li, 5, "Prénom", style2bi) + ws1.write(li, 6, "Place", style2bi) return wb.savetostr() diff --git a/app/scodoc/sco_prepajury.py b/app/scodoc/sco_prepajury.py index 057f8ea5..3d2398da 100644 --- a/app/scodoc/sco_prepajury.py +++ b/app/scodoc/sco_prepajury.py @@ -190,14 +190,14 @@ def feuille_preparation_jury(context, formsemestre_id, REQUEST): titles.append("Autorisations") # titles.append('Assidu') L.append(titles) - style_bold = sco_excel.Excel_MakeStyle(bold=True) - style_center = sco_excel.Excel_MakeStyle(halign="center") - style_boldcenter = sco_excel.Excel_MakeStyle(bold=True, halign="center") - style_moy = sco_excel.Excel_MakeStyle( + style_bold = sco_excel.excel_make_style(bold=True) + style_center = sco_excel.excel_make_style(halign="center") + style_boldcenter = sco_excel.excel_make_style(bold=True, halign="center") + style_moy = sco_excel.excel_make_style( bold=True, halign="center", bgcolor="lightyellow" ) - style_note = sco_excel.Excel_MakeStyle(halign="right") - style_note_bold = sco_excel.Excel_MakeStyle(halign="right", bold=True) + style_note = sco_excel.excel_make_style(halign="right") + style_note_bold = sco_excel.excel_make_style(halign="right", bold=True) if prev_moy: tit_prev_moy = "Moy " + sp col_prev_moy = titles.index(tit_prev_moy) @@ -315,4 +315,4 @@ def feuille_preparation_jury(context, formsemestre_id, REQUEST): xls = L.gen_workbook() - return sco_excel.sendExcelFile(REQUEST, xls, "PrepaJury%s.xls" % sn) + return sco_excel.send_excel_file(REQUEST, xls, f"PrepaJury{sn}{scu.XLSX_SUFFIX}") diff --git a/app/scodoc/sco_recapcomplet.py b/app/scodoc/sco_recapcomplet.py index a160029e..6f27c6cf 100644 --- a/app/scodoc/sco_recapcomplet.py +++ b/app/scodoc/sco_recapcomplet.py @@ -216,7 +216,7 @@ def do_formsemestre_recapcomplet( elif format == "csv": return scu.sendCSVFile(REQUEST, data, filename) elif format[:3] == "xls": - return sco_excel.sendExcelFile(REQUEST, data, filename) + return sco_excel.send_excel_file(REQUEST, data, filename) elif format == "json": return scu.sendJSON(REQUEST, data) else: @@ -776,15 +776,15 @@ def make_formsemestre_recapcomplet( semname = sem["titre_num"].replace(" ", "_") date = time.strftime("%d-%m-%Y") if format == "xls": - filename = "notes_modules-%s-%s.xls" % (semname, date) + filename = "notes_modules-%s-%s%s" % (semname, date, scu.XLSX_SUFFIX) else: - filename = "notes_modules_evals-%s-%s.xls" % (semname, date) - xls = sco_excel.Excel_SimpleTable( + filename = "notes_modules_evals-%s-%s%s" % (semname, date, scu.XLSX_SUFFIX) + xls = sco_excel.excel_simple_table( titles=["etudid", "code_nip"] + F[0][:-2], lines=[ [x[-1], x[-2]] + x[:-2] for x in F[1:] ], # reordonne cols (etudid et nip en 1er) - SheetName="notes %s %s" % (semname, date), + sheet_name="notes %s %s" % (semname, date), ) return xls, filename, "xls" else: diff --git a/app/scodoc/sco_synchro_etuds.py b/app/scodoc/sco_synchro_etuds.py index a6c230a2..9776f995 100644 --- a/app/scodoc/sco_synchro_etuds.py +++ b/app/scodoc/sco_synchro_etuds.py @@ -143,7 +143,7 @@ def formsemestre_synchro_etuds( base_url=base_url, read_only=read_only, ) - return sco_excel.sendExcelFile(REQUEST, xls, filename + ".xls") + return sco_excel.send_excel_file(REQUEST, xls, filename + scu.XLSX_SUFFIX) H = [header] if not submitted: diff --git a/app/scodoc/sco_trombino.py b/app/scodoc/sco_trombino.py index d72dce23..2b59fab1 100644 --- a/app/scodoc/sco_trombino.py +++ b/app/scodoc/sco_trombino.py @@ -491,7 +491,7 @@ def photos_generate_excel_sample(context, group_ids=[], REQUEST=None): extra_cols=["fichier_photo"], REQUEST=REQUEST, ) - return sco_excel.sendExcelFile(REQUEST, data, "ImportPhotos.xls") + return sco_excel.send_excel_file(REQUEST, data, "ImportPhotos" + scu.XLSX_SUFFIX) def photos_import_files_form(context, group_ids=[], REQUEST=None): diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index 2f6c828a..56cc8354 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -302,6 +302,7 @@ CSV_LINESEP = "\n" CSV_MIMETYPE = "text/comma-separated-values" XLS_MIMETYPE = "application/vnd.ms-excel" XLSX_MIMETYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" +XLSX_SUFFIX = ".xlsx" PDF_MIMETYPE = "application/pdf" XML_MIMETYPE = "text/xml" JSON_MIMETYPE = "application/json" diff --git a/app/views/scolar.py b/app/views/scolar.py index c59b99c5..d541efd0 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -1852,7 +1852,7 @@ def import_generate_excel_sample(context, REQUEST, with_codesemestre="1"): data = sco_import_etuds.sco_import_generate_excel_sample( format, with_codesemestre, exclude_cols=["photo_filename"], REQUEST=REQUEST ) - return sco_excel.sendExcelFile(REQUEST, data, "ImportEtudiants.xls") + return sco_excel.send_excel_file(REQUEST, data, "ImportEtudiants" + scu.XLSX_SUFFIX) # --- Données admission @@ -1871,7 +1871,9 @@ def import_generate_admission_sample(context, REQUEST, formsemestre_id): context=context, REQUEST=REQUEST, ) - return sco_excel.sendExcelFile(REQUEST, data, "AdmissionEtudiants.xls") + return sco_excel.send_excel_file( + REQUEST, data, "AdmissionEtudiants" + scu.XLSX_SUFFIX + ) # --- Données admission depuis fichier excel (version nov 2016)