2023-04-17 15:35:42 +02:00
|
|
|
"""
|
|
|
|
Gestion de l'archivage des justificatifs
|
|
|
|
|
|
|
|
Ecrit par Matthias HARTMANN
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
from shutil import rmtree
|
|
|
|
|
|
|
|
from app.models import Identite
|
|
|
|
from app.scodoc.sco_archives import BaseArchiver
|
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
|
|
|
from app.scodoc.sco_utils import is_iso_formated
|
|
|
|
|
|
|
|
|
|
|
|
class Trace:
|
|
|
|
"""gestionnaire de la trace des fichiers justificatifs"""
|
|
|
|
|
|
|
|
def __init__(self, path: str) -> None:
|
|
|
|
self.path: str = path + "/_trace.csv"
|
2023-07-04 15:04:58 +02:00
|
|
|
self.content: dict[str, list[datetime, datetime, str]] = {}
|
2023-04-17 15:35:42 +02:00
|
|
|
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:
|
|
|
|
for line in file.readlines():
|
|
|
|
csv = line.split(",")
|
2023-07-04 15:04:58 +02:00
|
|
|
if len(csv) < 4:
|
|
|
|
continue
|
2023-04-17 15:35:42 +02:00
|
|
|
fname: str = csv[0]
|
|
|
|
entry_date: datetime = is_iso_formated(csv[1], True)
|
|
|
|
delete_date: datetime = is_iso_formated(csv[2], True)
|
2023-07-04 15:04:58 +02:00
|
|
|
user_id = csv[3]
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
self.content[fname] = [entry_date, delete_date, user_id]
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
def set_trace(self, *fnames: str, mode: str = "entry", current_user: str = None):
|
2023-04-17 15:35:42 +02:00
|
|
|
"""Ajoute une trace du fichier donné
|
|
|
|
mode : entry / delete
|
|
|
|
"""
|
2023-07-04 15:04:58 +02:00
|
|
|
modes: list[str] = ["entry", "delete", "user_id"]
|
2023-04-17 15:35:42 +02:00
|
|
|
for fname in fnames:
|
|
|
|
if fname in modes:
|
|
|
|
continue
|
2023-07-04 15:04:58 +02:00
|
|
|
traced: list[datetime, datetime, str] = self.content.get(fname, False)
|
2023-04-17 15:35:42 +02:00
|
|
|
if not traced:
|
2023-07-04 15:04:58 +02:00
|
|
|
self.content[fname] = [None, None, None]
|
2023-04-17 15:35:42 +02:00
|
|
|
traced = self.content[fname]
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
traced[modes.index(mode)] = (
|
|
|
|
datetime.now() if mode != "user_id" else current_user
|
|
|
|
)
|
2023-04-17 15:35:42 +02:00
|
|
|
self.save_trace()
|
|
|
|
|
|
|
|
def save_trace(self):
|
|
|
|
"""Enregistre la trace dans le fichier _trace.csv"""
|
|
|
|
lines: list[str] = []
|
|
|
|
for fname, traced in self.content.items():
|
|
|
|
date_fin: datetime or None = traced[1].isoformat() if traced[1] else "None"
|
2023-06-23 16:12:36 +02:00
|
|
|
if traced[0] is not None:
|
2023-07-04 15:04:58 +02:00
|
|
|
lines.append(f"{fname},{traced[0].isoformat()},{date_fin}, {traced[2]}")
|
2023-04-17 15:35:42 +02:00
|
|
|
with open(self.path, "w", encoding="utf-8") as file:
|
|
|
|
file.write("\n".join(lines))
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
def get_trace(
|
2023-07-11 11:35:50 +02:00
|
|
|
self, fnames: list[str] = None
|
2023-07-04 15:04:58 +02:00
|
|
|
) -> dict[str, list[datetime, datetime, str]]:
|
2023-04-17 15:35:42 +02:00
|
|
|
"""Récupère la trace pour les noms de fichiers.
|
|
|
|
si aucun nom n'est donné, récupère tous les fichiers"""
|
|
|
|
|
2023-07-11 11:35:50 +02:00
|
|
|
if fnames is None:
|
2023-04-17 15:35:42 +02:00
|
|
|
return self.content
|
|
|
|
|
|
|
|
traced: dict = {}
|
|
|
|
for fname in fnames:
|
|
|
|
traced[fname] = self.content.get(fname, None)
|
|
|
|
|
|
|
|
return traced
|
|
|
|
|
|
|
|
|
|
|
|
class JustificatifArchiver(BaseArchiver):
|
|
|
|
"""
|
|
|
|
|
|
|
|
TOTALK:
|
|
|
|
- oid -> etudid
|
|
|
|
- archive_id -> date de création de l'archive (une archive par dépot de document)
|
|
|
|
|
|
|
|
justificatif
|
|
|
|
└── <dept_id>
|
|
|
|
└── <etudid/oid>
|
|
|
|
├── [_trace.csv]
|
|
|
|
└── <archive_id>
|
|
|
|
├── [_description.txt]
|
|
|
|
└── [<filename.ext>]
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
BaseArchiver.__init__(self, archive_type="justificatifs")
|
|
|
|
|
|
|
|
def save_justificatif(
|
|
|
|
self,
|
2023-09-07 23:09:39 +02:00
|
|
|
etud: Identite,
|
2023-04-17 15:35:42 +02:00
|
|
|
filename: str,
|
|
|
|
data: bytes or str,
|
|
|
|
archive_name: str = None,
|
|
|
|
description: str = "",
|
2023-07-04 15:04:58 +02:00
|
|
|
user_id: str = None,
|
2023-04-17 15:35:42 +02:00
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Ajoute un fichier dans une archive "justificatif" pour l'etudid donné
|
|
|
|
Retourne l'archive_name utilisé
|
|
|
|
"""
|
|
|
|
if archive_name is None:
|
|
|
|
archive_id: str = self.create_obj_archive(
|
2023-09-07 23:09:39 +02:00
|
|
|
oid=etud.id, description=description, dept_id=etud.dept_id
|
2023-04-17 15:35:42 +02:00
|
|
|
)
|
|
|
|
else:
|
2023-09-07 23:09:39 +02:00
|
|
|
archive_id: str = self.get_id_from_name(
|
|
|
|
etud.id, archive_name, dept_id=etud.dept_id
|
|
|
|
)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
fname: str = self.store(archive_id, filename, data, dept_id=etud.dept_id)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
trace = Trace(self.get_obj_dir(etud.id, dept_id=etud.dept_id))
|
2023-07-04 15:04:58 +02:00
|
|
|
trace.set_trace(fname, mode="entry")
|
|
|
|
if user_id is not None:
|
|
|
|
trace.set_trace(fname, mode="user_id", current_user=user_id)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
|
|
|
return self.get_archive_name(archive_id), fname
|
|
|
|
|
|
|
|
def delete_justificatif(
|
|
|
|
self,
|
2023-09-07 23:09:39 +02:00
|
|
|
etud: Identite,
|
2023-04-17 15:35:42 +02:00
|
|
|
archive_name: str,
|
|
|
|
filename: str = None,
|
|
|
|
has_trace: bool = True,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Supprime une archive ou un fichier particulier de l'archivage de l'étudiant donné
|
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
Si trace == True : sauvegarde le nom du/des fichier(s) supprimé(s)
|
|
|
|
dans la trace de l'étudiant
|
2023-04-17 15:35:42 +02:00
|
|
|
"""
|
2023-09-10 22:11:10 +02:00
|
|
|
|
|
|
|
if str(etud.id) not in self.list_oids(etud.dept_id):
|
2023-09-07 23:09:39 +02:00
|
|
|
raise ValueError(f"Aucune archive pour etudid[{etud.id}]")
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
archive_id = self.get_id_from_name(etud.id, archive_name, dept_id=etud.dept_id)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
|
|
|
if filename is not None:
|
2023-09-07 23:09:39 +02:00
|
|
|
if filename not in self.list_archive(archive_id, dept_id=etud.dept_id):
|
2023-04-17 15:35:42 +02:00
|
|
|
raise ValueError(
|
2023-09-07 23:09:39 +02:00
|
|
|
f"""filename {filename} inconnu dans l'archive archive_id[{
|
|
|
|
archive_id}] -> etudid[{etud.id}]"""
|
2023-04-17 15:35:42 +02:00
|
|
|
)
|
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
path: str = os.path.join(
|
|
|
|
self.get_obj_dir(etud.id, dept_id=etud.dept_id), archive_id, filename
|
|
|
|
)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
if has_trace:
|
2023-09-07 23:09:39 +02:00
|
|
|
trace = Trace(self.get_obj_dir(etud.id, dept_id=etud.dept_id))
|
2023-07-04 15:04:58 +02:00
|
|
|
trace.set_trace(filename, mode="delete")
|
2023-04-17 15:35:42 +02:00
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
else:
|
|
|
|
if has_trace:
|
2023-09-07 23:09:39 +02:00
|
|
|
trace = Trace(self.get_obj_dir(etud.id, dept_id=etud.dept_id))
|
|
|
|
trace.set_trace(
|
|
|
|
*self.list_archive(archive_id, dept_id=etud.dept_id), mode="delete"
|
|
|
|
)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
|
|
|
self.delete_archive(
|
|
|
|
os.path.join(
|
2023-09-07 23:09:39 +02:00
|
|
|
self.get_obj_dir(etud.id, dept_id=etud.dept_id),
|
2023-04-17 15:35:42 +02:00
|
|
|
archive_id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-07-04 15:04:58 +02:00
|
|
|
def list_justificatifs(
|
2023-09-07 23:09:39 +02:00
|
|
|
self, archive_name: str, etud: Identite
|
2023-07-04 15:04:58 +02:00
|
|
|
) -> list[tuple[str, int]]:
|
2023-04-17 15:35:42 +02:00
|
|
|
"""
|
|
|
|
Retourne la liste des noms de fichiers dans l'archive donnée
|
|
|
|
"""
|
|
|
|
filenames: list[str] = []
|
2023-09-07 23:09:39 +02:00
|
|
|
archive_id = self.get_id_from_name(etud.id, archive_name, dept_id=etud.dept_id)
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
filenames = self.list_archive(archive_id, dept_id=etud.dept_id)
|
|
|
|
trace: Trace = Trace(self.get_obj_dir(etud.id, dept_id=etud.dept_id))
|
2023-07-04 15:04:58 +02:00
|
|
|
traced = trace.get_trace(filenames)
|
2023-07-11 11:35:50 +02:00
|
|
|
retour = [(key, value[2]) for key, value in traced.items()]
|
2023-07-04 15:04:58 +02:00
|
|
|
|
2023-07-11 11:35:50 +02:00
|
|
|
return retour
|
2023-04-17 15:35:42 +02:00
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
def get_justificatif_file(self, archive_name: str, etud: Identite, filename: str):
|
2023-04-17 15:35:42 +02:00
|
|
|
"""
|
|
|
|
Retourne une réponse de téléchargement de fichier si le fichier existe
|
|
|
|
"""
|
2023-09-07 23:09:39 +02:00
|
|
|
archive_id: str = self.get_id_from_name(
|
|
|
|
etud.id, archive_name, dept_id=etud.dept_id
|
|
|
|
)
|
|
|
|
if filename in self.list_archive(archive_id, dept_id=etud.dept_id):
|
|
|
|
return self.get_archived_file(
|
|
|
|
etud.id, archive_name, filename, dept_id=etud.dept_id
|
|
|
|
)
|
2023-04-17 15:35:42 +02:00
|
|
|
raise ScoValueError(
|
|
|
|
f"Fichier {filename} introuvable dans l'archive {archive_name}"
|
|
|
|
)
|
|
|
|
|
|
|
|
def remove_dept_archive(self, dept_id: int = None):
|
|
|
|
"""
|
|
|
|
Supprime toutes les archives d'un département (ou de tous les départements)
|
|
|
|
⚠ Supprime aussi les fichiers de trace ⚠
|
|
|
|
"""
|
2023-09-07 23:09:39 +02:00
|
|
|
# juste pour récupérer .root, dept_id n'a pas d'importance
|
|
|
|
self.initialize(dept_id=1)
|
2023-04-17 15:35:42 +02:00
|
|
|
if dept_id is None:
|
|
|
|
rmtree(self.root, ignore_errors=True)
|
|
|
|
else:
|
|
|
|
rmtree(os.path.join(self.root, str(dept_id)), ignore_errors=True)
|
|
|
|
|
2023-09-07 23:09:39 +02:00
|
|
|
def get_trace( # XXX inutilisée ?
|
|
|
|
self, etud: Identite, *fnames: str
|
2023-04-17 15:35:42 +02:00
|
|
|
) -> dict[str, list[datetime, datetime]]:
|
|
|
|
"""Récupère la trace des justificatifs de l'étudiant"""
|
2023-09-07 23:09:39 +02:00
|
|
|
trace = Trace(self.get_obj_dir(etud.id, etud.dept_id))
|
2023-04-17 15:35:42 +02:00
|
|
|
return trace.get_trace(fnames)
|