Fix: API sco_excel + suffix xslx

This commit is contained in:
Emmanuel Viennet 2021-08-12 14:49:53 +02:00
parent 6287376efb
commit fbdcf63c70
16 changed files with 95 additions and 75 deletions

View File

@ -653,7 +653,9 @@ class GenTable(object):
elif format == "xls" or format == "xlsx": elif format == "xls" or format == "xlsx":
xls = self.excel() xls = self.excel()
if publish: 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: else:
return xls return xls
elif format == "text": elif format == "text":

View File

@ -194,7 +194,7 @@ class JuryPE(object):
# self.add_file_to_zip(filename, self.xls.excel()) # self.add_file_to_zip(filename, self.xls.excel())
# Fabrique 1 fichier excel résultat avec 1 seule feuille => trop gros # 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") self.xlsV2 = self.table_syntheseJury(mode="multiplesheet")
if self.xlsV2: if self.xlsV2:
self.add_file_to_zip(filename, self.xlsV2.excel()) self.add_file_to_zip(filename, self.xlsV2.excel())

View File

@ -142,7 +142,9 @@ def pe_view_sem_recap(
context, jury.syntheseJury, tag_annotation_pe context, jury.syntheseJury, tag_annotation_pe
) )
if sT: 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 latex_pages = {} # Dictionnaire de la forme nom_fichier => contenu_latex
for etudid in etudids: for etudid in etudids:

View File

@ -1266,8 +1266,8 @@ def export_csv_to_apogee(
nf += 1 nf += 1
log_filename = "scodoc-" + basename + ".log.txt" log_filename = "scodoc-" + basename + ".log.txt"
nar_filename = basename + "-nar.xls" nar_filename = basename + "-nar" + scu.XLSX_SUFFIX
cr_filename = basename + "-decisions.xls" cr_filename = basename + "-decisions" + scu.XLSX_SUFFIX
logf = StringIO() logf = StringIO()
logf.write("export_to_apogee du %s\n\n" % time.ctime()) logf.write("export_to_apogee du %s\n\n" % time.ctime())

View File

@ -237,7 +237,13 @@ class BaseArchiver(object):
REQUEST.RESPONSE.setHeader("content-type", scu.XML_MIMETYPE) REQUEST.RESPONSE.setHeader("content-type", scu.XML_MIMETYPE)
return data return data
elif ext == ".xls": 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": elif ext == ".csv":
return scu.sendCSVFile(REQUEST, data, filename) return scu.sendCSVFile(REQUEST, data, filename)
elif ext == ".pdf": elif ext == ".pdf":
@ -297,7 +303,7 @@ def do_formsemestre_archive(
context, REQUEST, formsemestre_id, format="xls" context, REQUEST, formsemestre_id, format="xls"
) )
if data: 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) # Tableau recap notes en HTML (pour tous les etudiants, n'utilise pas les groupes)
data, _, _ = make_formsemestre_recapcomplet( data, _, _ = make_formsemestre_recapcomplet(
context, REQUEST, formsemestre_id, format="html", disable_etudlink=True 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 context, formsemestre_id, format="xls", REQUEST=REQUEST, publish=False
) )
if data: if data:
PVArchive.store(archive_id, "Decisions_Jury.xls", data) PVArchive.store(archive_id, "Decisions_Jury" + scu.XLSX_SUFFIX, data)
# Classeur bulletins (PDF) # Classeur bulletins (PDF)
data, _ = sco_bulletins_pdf.get_formsemestre_bulletins_pdf( data, _ = sco_bulletins_pdf.get_formsemestre_bulletins_pdf(
context, formsemestre_id, REQUEST, version=bulVersion context, formsemestre_id, REQUEST, version=bulVersion

View File

@ -256,7 +256,9 @@ def etudarchive_generate_excel_sample(context, group_id=None, REQUEST=None):
extra_cols=["fichier_a_charger"], extra_cols=["fichier_a_charger"],
REQUEST=REQUEST, 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): def etudarchive_import_files_form(context, group_id, REQUEST=None):

View File

@ -56,7 +56,7 @@ class COLORS(Enum):
LIGHT_YELLOW = "FFFFFF99" LIGHT_YELLOW = "FFFFFF99"
def send_excel_file(request, data, filename): def send_excel_file(request, data, filename, mime=scu.XLSX_MIMETYPE):
"""publication fichier. """publication fichier.
(on ne doit rien avoir émis avant, car ici sont générés les entetes) (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("&", "")
.replace(" ", "_") .replace(" ", "_")
) )
request.RESPONSE.setHeader("content-type", scu.XLSX_MIMETYPE) request.RESPONSE.setHeader("content-type", mime)
request.RESPONSE.setHeader( request.RESPONSE.setHeader(
"content-disposition", 'attachment; filename="%s"' % filename "content-disposition", 'attachment; filename="%s"' % filename
) )
@ -131,6 +131,7 @@ class ScoExcelBook:
... ...
steam = wb.generate() steam = wb.generate()
""" """
def __init__(self): def __init__(self):
self.sheets = [] # list of sheets self.sheets = [] # list of sheets
@ -139,7 +140,7 @@ class ScoExcelBook:
self.sheets.append(sheet) self.sheets.append(sheet)
def generate(self): 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 retourne le flux
""" """
wb = Workbook(write_only=True) 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, ..) 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 * pour finit appel de la méthode de génération
""" """
def __init__(self, sheet_name="feuille", default_style=None, wb=None): def __init__(self, sheet_name="feuille", default_style=None, wb=None):
"""Création de la feuille. """Création de la feuille.
sheet_name -- le nom 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) wb = load_workbook(filename=filelike, read_only=True, data_only=True)
except: except:
log("Excel_to_list: failure to import document") 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( raise ScoValueError(
"Fichier illisible: assurez-vous qu'il s'agit bien d'un document Excel !" "Fichier illisible: assurez-vous qu'il s'agit bien d'un document Excel !"
) )

View File

@ -724,10 +724,15 @@ def groups_table(
return tab.make_page(context, format=format, REQUEST=REQUEST) return tab.make_page(context, format=format, REQUEST=REQUEST)
elif format == "xlsappel": elif format == "xlsappel":
xls = sco_excel.excel_feuille_listeappel(groups_infos.formsemestre, groups_infos.groups_titles, xls = sco_excel.excel_feuille_listeappel(
groups_infos.members, partitions=groups_infos.partitions, groups_infos.formsemestre,
with_codes=with_codes, with_paiement=with_paiement, groups_infos.groups_titles,
server_name=REQUEST.BASE0) 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" filename = "liste_%s" % groups_infos.groups_filename + ".xlsx"
return sco_excel.send_excel_file(REQUEST, xls, filename) return sco_excel.send_excel_file(REQUEST, xls, filename)
elif format == "allxls": elif format == "allxls":
@ -798,8 +803,8 @@ def groups_table(
L = [dicttakestr(m, keys) for m in groups_infos.members] L = [dicttakestr(m, keys) for m in groups_infos.members]
title = "etudiants_%s" % groups_infos.groups_filename title = "etudiants_%s" % groups_infos.groups_filename
xls = sco_excel.Excel_SimpleTable(titles=titles, lines=L, SheetName=title) xls = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title)
filename = title + ".xls" filename = title + scu.XLSX_SUFFIX
return sco_excel.send_excel_file(REQUEST, xls, filename) return sco_excel.send_excel_file(REQUEST, xls, filename)
else: else:
raise ValueError("unsupported format") raise ValueError("unsupported format")

View File

@ -49,8 +49,8 @@ def generate_excel_sample():
style = sco_excel.Excel_MakeStyle(bold=True) style = sco_excel.Excel_MakeStyle(bold=True)
titles = TITLES titles = TITLES
titlesStyles = [style] * len(titles) titlesStyles = [style] * len(titles)
return sco_excel.Excel_SimpleTable( return sco_excel.excel_simple_table(
titles=titles, titlesStyles=titlesStyles, SheetName="Utilisateurs ScoDoc" titles=titles, titlesStyles=titlesStyles, sheet_name="Utilisateurs ScoDoc"
) )

View File

@ -322,11 +322,11 @@ def do_placement(context, REQUEST):
maxlines = sem_preferences.get("feuille_placement_positions") maxlines = sem_preferences.get("feuille_placement_positions")
if placement_method == "xls": 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( xls = Excel_feuille_placement(
E, desceval, listetud, columns, space, maxlines, building, room, numbering 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: else:
nbcolumns = int(columns) nbcolumns = int(columns)
@ -663,8 +663,8 @@ def Excel_feuille_placement(
li = 0 li = 0
line = 0 line = 0
dt = time.strftime("%d/%m/%Y a %Hh%M") dt = time.strftime("%d/%m/%Y a %Hh%M")
ws0.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, u"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: for desceval in description:
if line % 2 == 0: if line % 2 == 0:
li += 2 li += 2
@ -677,41 +677,39 @@ def Excel_feuille_placement(
ws0.write( ws0.write(
li, li,
0, 0,
u"Date : %s - Horaire : %s à %s" "Date : %s - Horaire : %s à %s" % (E["jour"], E["heure_debut"], E["heure_fin"]),
% (E["jour"], E["heure_debut"], E["heure_fin"]),
style_titres, style_titres,
) )
ws1.write( ws1.write(
li, li,
0, 0,
u"Date : %s - Horaire : %s à %s" "Date : %s - Horaire : %s à %s" % (E["jour"], E["heure_debut"], E["heure_fin"]),
% (E["jour"], E["heure_debut"], E["heure_fin"]),
style_titres, style_titres,
) )
li += 1 li += 1
# entetes colonnes - feuille0 # entetes colonnes - feuille0
for col in range(nbcolumns): 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 # entetes colonnes - feuille1
if numbering == "coordinate": if numbering == "coordinate":
ws1.write(li, 0, u"Nom", style2bi) ws1.write(li, 0, "Nom", style2bi)
ws1.write(li, 1, u"Prénom", style2bi) ws1.write(li, 1, "Prénom", style2bi)
ws1.write(li, 2, u"Colonne", style2bi) ws1.write(li, 2, "Colonne", style2bi)
ws1.write(li, 3, u"Ligne", style2bi) ws1.write(li, 3, "Ligne", style2bi)
ws1.write(li, 5, u"Nom", style2bi) ws1.write(li, 5, "Nom", style2bi)
ws1.write(li, 6, u"Prénom", style2bi) ws1.write(li, 6, "Prénom", style2bi)
ws1.write(li, 7, u"Colonne", style2bi) ws1.write(li, 7, "Colonne", style2bi)
ws1.write(li, 8, u"Ligne", style2bi) ws1.write(li, 8, "Ligne", style2bi)
else: else:
ws1.write(li, 0, u"Nom", style2bi) ws1.write(li, 0, "Nom", style2bi)
ws1.write(li, 1, u"Prénom", style2bi) ws1.write(li, 1, "Prénom", style2bi)
ws1.write(li, 2, u"Place", style2bi) ws1.write(li, 2, "Place", style2bi)
ws1.write(li, 4, u"Nom", style2bi) ws1.write(li, 4, "Nom", style2bi)
ws1.write(li, 5, u"Prénom", style2bi) ws1.write(li, 5, "Prénom", style2bi)
ws1.write(li, 6, u"Place", style2bi) ws1.write(li, 6, "Place", style2bi)
# etudiants # etudiants
line = 1 line = 1
@ -751,7 +749,7 @@ def Excel_feuille_placement(
ws0.write(li0 + 2, col, " ", style1bb) ws0.write(li0 + 2, col, " ", style1bb)
else: else:
ws0.write( 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+3,col, ' ', style1bm )
# ws0.write(li+4,col, ' ', style1bb ) # ws0.write(li+4,col, ' ', style1bb )
@ -793,21 +791,21 @@ def Excel_feuille_placement(
nbcol = 0 nbcol = 0
col = 0 col = 0
if numbering == "coordinate": if numbering == "coordinate":
ws1.write(li, 0, u"Nom", style2bi) ws1.write(li, 0, "Nom", style2bi)
ws1.write(li, 1, u"Prénom", style2bi) ws1.write(li, 1, "Prénom", style2bi)
ws1.write(li, 2, u"Colonne", style2bi) ws1.write(li, 2, "Colonne", style2bi)
ws1.write(li, 3, u"Ligne", style2bi) ws1.write(li, 3, "Ligne", style2bi)
ws1.write(li, 5, u"Nom", style2bi) ws1.write(li, 5, "Nom", style2bi)
ws1.write(li, 6, u"Prénom", style2bi) ws1.write(li, 6, "Prénom", style2bi)
ws1.write(li, 7, u"Colonne", style2bi) ws1.write(li, 7, "Colonne", style2bi)
ws1.write(li, 8, u"Ligne", style2bi) ws1.write(li, 8, "Ligne", style2bi)
else: else:
ws1.write(li, 0, u"Nom", style2bi) ws1.write(li, 0, "Nom", style2bi)
ws1.write(li, 1, u"Prénom", style2bi) ws1.write(li, 1, "Prénom", style2bi)
ws1.write(li, 2, u"Place", style2bi) ws1.write(li, 2, "Place", style2bi)
ws1.write(li, 4, u"Nom", style2bi) ws1.write(li, 4, "Nom", style2bi)
ws1.write(li, 5, u"Prénom", style2bi) ws1.write(li, 5, "Prénom", style2bi)
ws1.write(li, 6, u"Place", style2bi) ws1.write(li, 6, "Place", style2bi)
return wb.savetostr() return wb.savetostr()

View File

@ -190,14 +190,14 @@ def feuille_preparation_jury(context, formsemestre_id, REQUEST):
titles.append("Autorisations") titles.append("Autorisations")
# titles.append('Assidu') # titles.append('Assidu')
L.append(titles) L.append(titles)
style_bold = sco_excel.Excel_MakeStyle(bold=True) style_bold = sco_excel.excel_make_style(bold=True)
style_center = sco_excel.Excel_MakeStyle(halign="center") style_center = sco_excel.excel_make_style(halign="center")
style_boldcenter = sco_excel.Excel_MakeStyle(bold=True, halign="center") style_boldcenter = sco_excel.excel_make_style(bold=True, halign="center")
style_moy = sco_excel.Excel_MakeStyle( style_moy = sco_excel.excel_make_style(
bold=True, halign="center", bgcolor="lightyellow" bold=True, halign="center", bgcolor="lightyellow"
) )
style_note = sco_excel.Excel_MakeStyle(halign="right") style_note = sco_excel.excel_make_style(halign="right")
style_note_bold = sco_excel.Excel_MakeStyle(halign="right", bold=True) style_note_bold = sco_excel.excel_make_style(halign="right", bold=True)
if prev_moy: if prev_moy:
tit_prev_moy = "Moy " + sp tit_prev_moy = "Moy " + sp
col_prev_moy = titles.index(tit_prev_moy) col_prev_moy = titles.index(tit_prev_moy)
@ -315,4 +315,4 @@ def feuille_preparation_jury(context, formsemestre_id, REQUEST):
xls = L.gen_workbook() 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}")

View File

@ -216,7 +216,7 @@ def do_formsemestre_recapcomplet(
elif format == "csv": elif format == "csv":
return scu.sendCSVFile(REQUEST, data, filename) return scu.sendCSVFile(REQUEST, data, filename)
elif format[:3] == "xls": elif format[:3] == "xls":
return sco_excel.sendExcelFile(REQUEST, data, filename) return sco_excel.send_excel_file(REQUEST, data, filename)
elif format == "json": elif format == "json":
return scu.sendJSON(REQUEST, data) return scu.sendJSON(REQUEST, data)
else: else:
@ -776,15 +776,15 @@ def make_formsemestre_recapcomplet(
semname = sem["titre_num"].replace(" ", "_") semname = sem["titre_num"].replace(" ", "_")
date = time.strftime("%d-%m-%Y") date = time.strftime("%d-%m-%Y")
if format == "xls": if format == "xls":
filename = "notes_modules-%s-%s.xls" % (semname, date) filename = "notes_modules-%s-%s%s" % (semname, date, scu.XLSX_SUFFIX)
else: else:
filename = "notes_modules_evals-%s-%s.xls" % (semname, date) filename = "notes_modules_evals-%s-%s%s" % (semname, date, scu.XLSX_SUFFIX)
xls = sco_excel.Excel_SimpleTable( xls = sco_excel.excel_simple_table(
titles=["etudid", "code_nip"] + F[0][:-2], titles=["etudid", "code_nip"] + F[0][:-2],
lines=[ lines=[
[x[-1], x[-2]] + x[:-2] for x in F[1:] [x[-1], x[-2]] + x[:-2] for x in F[1:]
], # reordonne cols (etudid et nip en 1er) ], # reordonne cols (etudid et nip en 1er)
SheetName="notes %s %s" % (semname, date), sheet_name="notes %s %s" % (semname, date),
) )
return xls, filename, "xls" return xls, filename, "xls"
else: else:

View File

@ -143,7 +143,7 @@ def formsemestre_synchro_etuds(
base_url=base_url, base_url=base_url,
read_only=read_only, 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] H = [header]
if not submitted: if not submitted:

View File

@ -491,7 +491,7 @@ def photos_generate_excel_sample(context, group_ids=[], REQUEST=None):
extra_cols=["fichier_photo"], extra_cols=["fichier_photo"],
REQUEST=REQUEST, 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): def photos_import_files_form(context, group_ids=[], REQUEST=None):

View File

@ -302,6 +302,7 @@ CSV_LINESEP = "\n"
CSV_MIMETYPE = "text/comma-separated-values" CSV_MIMETYPE = "text/comma-separated-values"
XLS_MIMETYPE = "application/vnd.ms-excel" XLS_MIMETYPE = "application/vnd.ms-excel"
XLSX_MIMETYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" XLSX_MIMETYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
XLSX_SUFFIX = ".xlsx"
PDF_MIMETYPE = "application/pdf" PDF_MIMETYPE = "application/pdf"
XML_MIMETYPE = "text/xml" XML_MIMETYPE = "text/xml"
JSON_MIMETYPE = "application/json" JSON_MIMETYPE = "application/json"

View File

@ -1852,7 +1852,7 @@ def import_generate_excel_sample(context, REQUEST, with_codesemestre="1"):
data = sco_import_etuds.sco_import_generate_excel_sample( data = sco_import_etuds.sco_import_generate_excel_sample(
format, with_codesemestre, exclude_cols=["photo_filename"], REQUEST=REQUEST 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 # --- Données admission
@ -1871,7 +1871,9 @@ def import_generate_admission_sample(context, REQUEST, formsemestre_id):
context=context, context=context,
REQUEST=REQUEST, 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) # --- Données admission depuis fichier excel (version nov 2016)