From b92efefb64f2ad6ed46e05bc26f511bc15c6f46d Mon Sep 17 00:00:00 2001 From: Iziram Date: Mon, 25 Sep 2023 15:12:18 +0200 Subject: [PATCH] Assiduites : maj trace justi --- app/scodoc/sco_archives_justificatifs.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/scodoc/sco_archives_justificatifs.py b/app/scodoc/sco_archives_justificatifs.py index c6dbd4472b..125569d019 100644 --- a/app/scodoc/sco_archives_justificatifs.py +++ b/app/scodoc/sco_archives_justificatifs.py @@ -19,26 +19,35 @@ class Trace: """gestionnaire de la trace des fichiers justificatifs""" def __init__(self, path: str) -> None: - log(f"init Trace {path}") self.path: str = path + "/_trace.csv" self.content: dict[str, list[datetime, datetime, str]] = {} self.import_from_file() def import_from_file(self): """import trace from file""" - if os.path.isfile(self.path): - with open(self.path, "r", encoding="utf-8") as file: + + def import_from_csv(path): + with open(path, "r", encoding="utf-8") as file: for line in file.readlines(): csv = line.split(",") if len(csv) < 4: continue fname: str = csv[0] + if fname not in os.listdir(self.path.replace("/_trace.csv", "")): + continue entry_date: datetime = is_iso_formated(csv[1], True) delete_date: datetime = is_iso_formated(csv[2], True) user_id = csv[3] - self.content[fname] = [entry_date, delete_date, user_id] + if os.path.isfile(self.path): + import_from_csv(self.path) + else: + parent_dir: str = self.path[: self.path.rfind("/", 0, self.path.rfind("/"))] + if os.path.isfile(parent_dir + "/_trace.csv"): + import_from_csv(parent_dir + "/_trace.csv") + self.save_trace() + def set_trace(self, *fnames: str, mode: str = "entry", current_user: str = None): """Ajoute une trace du fichier donné mode : entry / delete @@ -57,9 +66,11 @@ class Trace: ) self.save_trace() - def save_trace(self): + def save_trace(self, new_path: str = None): """Enregistre la trace dans le fichier _trace.csv""" lines: list[str] = [] + if new_path is not None: + self.path = new_path for fname, traced in self.content.items(): date_fin: datetime or None = traced[1].isoformat() if traced[1] else "None" if traced[0] is not None: @@ -126,7 +137,6 @@ class JustificatifArchiver(BaseArchiver): ) fname: str = self.store(archive_id, filename, data, dept_id=etud.dept_id) - log(f"obj_dir {self.get_obj_dir(etud.id, dept_id=etud.dept_id)} | {archive_id}") trace = Trace(archive_id) trace.set_trace(fname, mode="entry") if user_id is not None: