From 0330544a0bb4e23bb8c742cb4b10c1c843bd0bb7 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 30 Jun 2022 09:27:46 +0200 Subject: [PATCH] Fix:archives (again...) --- app/scodoc/sco_archives.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/scodoc/sco_archives.py b/app/scodoc/sco_archives.py index 05f5f00ce..26ce9f8c7 100644 --- a/app/scodoc/sco_archives.py +++ b/app/scodoc/sco_archives.py @@ -47,6 +47,7 @@ qui est une description (humaine, format libre) de l'archive. """ +import chardet import datetime import glob import json @@ -55,7 +56,7 @@ import os import re import shutil import time -import chardet +from typing import Union import flask from flask import g, request @@ -232,14 +233,17 @@ class BaseArchiver(object): os.mkdir(archive_id) # if exists, raises an OSError finally: scu.GSL.release() - self.store(archive_id, "_description.txt", description.encode("utf-8")) + self.store(archive_id, "_description.txt", description) return archive_id - def store(self, archive_id: str, filename: str, data: bytes): + def store(self, archive_id: str, filename: str, data: Union[str, bytes]): """Store data in archive, under given filename. Filename may be modified (sanitized): return used filename The file is created or replaced. + data may be str or bytes """ + if isinstance(data, str): + data = data.encode(scu.SCO_ENCODING) self.initialize() filename = scu.sanitize_filename(filename) log("storing %s (%d bytes) in %s" % (filename, len(data), archive_id)) @@ -350,13 +354,11 @@ def do_formsemestre_archive( html_sco_header.sco_footer(), ] ) - data = data.encode(scu.SCO_ENCODING) PVArchive.store(archive_id, "Tableau_moyennes.html", data) # Bulletins en JSON data = gen_formsemestre_recapcomplet_json(formsemestre_id, xml_with_decisions=True) data_js = json.dumps(data, indent=1, cls=scu.ScoDocJSONEncoder) - data_js = data_js.encode(scu.SCO_ENCODING) if data: PVArchive.store(archive_id, "Bulletins.json", data_js) # Decisions de jury, en XLS @@ -365,7 +367,7 @@ def do_formsemestre_archive( PVArchive.store( archive_id, "Decisions_Jury" + scu.XLSX_SUFFIX, - data.encode(scu.SCO_ENCODING), + data, ) # Classeur bulletins (PDF) data, _ = sco_bulletins_pdf.get_formsemestre_bulletins_pdf(