This commit is contained in:
Emmanuel Viennet 2023-09-25 23:52:05 +02:00
commit 34486ac6f8

View File

@ -19,26 +19,35 @@ class Trace:
"""gestionnaire de la trace des fichiers justificatifs""" """gestionnaire de la trace des fichiers justificatifs"""
def __init__(self, path: str) -> None: def __init__(self, path: str) -> None:
log(f"init Trace {path}")
self.path: str = path + "/_trace.csv" self.path: str = path + "/_trace.csv"
self.content: dict[str, list[datetime, datetime, str]] = {} self.content: dict[str, list[datetime, datetime, str]] = {}
self.import_from_file() self.import_from_file()
def import_from_file(self): def import_from_file(self):
"""import trace from file""" """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(): for line in file.readlines():
csv = line.split(",") csv = line.split(",")
if len(csv) < 4: if len(csv) < 4:
continue continue
fname: str = csv[0] 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) entry_date: datetime = is_iso_formated(csv[1], True)
delete_date: datetime = is_iso_formated(csv[2], True) delete_date: datetime = is_iso_formated(csv[2], True)
user_id = csv[3] user_id = csv[3]
self.content[fname] = [entry_date, delete_date, user_id] 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): def set_trace(self, *fnames: str, mode: str = "entry", current_user: str = None):
"""Ajoute une trace du fichier donné """Ajoute une trace du fichier donné
mode : entry / delete mode : entry / delete
@ -57,9 +66,11 @@ class Trace:
) )
self.save_trace() self.save_trace()
def save_trace(self): def save_trace(self, new_path: str = None):
"""Enregistre la trace dans le fichier _trace.csv""" """Enregistre la trace dans le fichier _trace.csv"""
lines: list[str] = [] lines: list[str] = []
if new_path is not None:
self.path = new_path
for fname, traced in self.content.items(): for fname, traced in self.content.items():
date_fin: datetime or None = traced[1].isoformat() if traced[1] else "None" date_fin: datetime or None = traced[1].isoformat() if traced[1] else "None"
if traced[0] is not 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) 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 = Trace(archive_id)
trace.set_trace(fname, mode="entry") trace.set_trace(fname, mode="entry")
if user_id is not None: if user_id is not None: