2023-04-17 15:34:00 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
"""Gestion de l'assiduité (assiduités + justificatifs)
|
|
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
|
2023-07-26 13:27:57 +02:00
|
|
|
from app import db, log
|
|
|
|
from app.models import ModuleImpl, Scolog
|
2023-04-17 15:34:00 +02:00
|
|
|
from app.models.etudiants import Identite
|
2023-06-30 17:24:16 +02:00
|
|
|
from app.auth.models import User
|
2023-04-17 15:34:00 +02:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
|
|
|
from app.scodoc.sco_utils import (
|
|
|
|
EtatAssiduite,
|
|
|
|
EtatJustificatif,
|
|
|
|
localize_datetime,
|
|
|
|
)
|
|
|
|
|
2023-08-14 01:08:04 +02:00
|
|
|
from flask_sqlalchemy.query import Query
|
|
|
|
|
2023-04-17 15:34:00 +02:00
|
|
|
|
|
|
|
class Assiduite(db.Model):
|
|
|
|
"""
|
|
|
|
Représente une assiduité:
|
|
|
|
- une plage horaire lié à un état et un étudiant
|
|
|
|
- un module si spécifiée
|
|
|
|
- une description si spécifiée
|
|
|
|
"""
|
|
|
|
|
|
|
|
__tablename__ = "assiduites"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True, nullable=False)
|
|
|
|
assiduite_id = db.synonym("id")
|
|
|
|
|
|
|
|
date_debut = db.Column(
|
|
|
|
db.DateTime(timezone=True), server_default=db.func.now(), nullable=False
|
|
|
|
)
|
|
|
|
date_fin = db.Column(
|
|
|
|
db.DateTime(timezone=True), server_default=db.func.now(), nullable=False
|
|
|
|
)
|
|
|
|
|
|
|
|
moduleimpl_id = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("notes_moduleimpl.id", ondelete="SET NULL"),
|
|
|
|
)
|
|
|
|
etudid = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("identite.id", ondelete="CASCADE"),
|
|
|
|
index=True,
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
etat = db.Column(db.Integer, nullable=False)
|
|
|
|
|
2023-07-20 13:10:26 +02:00
|
|
|
description = db.Column(db.Text)
|
2023-04-17 15:34:00 +02:00
|
|
|
|
|
|
|
entry_date = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
|
|
|
|
|
|
|
|
user_id = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("user.id", ondelete="SET NULL"),
|
|
|
|
nullable=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
est_just = db.Column(db.Boolean, server_default="false", nullable=False)
|
|
|
|
|
2023-07-31 09:41:32 +02:00
|
|
|
external_data = db.Column(db.JSON, nullable=True)
|
|
|
|
|
2023-07-29 18:32:29 +02:00
|
|
|
# Déclare la relation "joined" car on va très souvent vouloir récupérer
|
|
|
|
# l'étudiant en même tant que l'assiduité (perf.: évite nouvelle requete SQL)
|
|
|
|
etudiant = db.relationship("Identite", back_populates="assiduites", lazy="joined")
|
|
|
|
|
2023-04-17 15:34:00 +02:00
|
|
|
def to_dict(self, format_api=True) -> dict:
|
|
|
|
"""Retourne la représentation json de l'assiduité"""
|
|
|
|
etat = self.etat
|
2023-06-30 17:24:16 +02:00
|
|
|
username = self.user_id
|
2023-04-17 15:34:00 +02:00
|
|
|
if format_api:
|
|
|
|
etat = EtatAssiduite.inverse().get(self.etat).name
|
2023-06-30 17:24:16 +02:00
|
|
|
if self.user_id is not None:
|
2023-07-11 11:35:50 +02:00
|
|
|
user: User = db.session.get(User, self.user_id)
|
|
|
|
|
2023-06-30 17:24:16 +02:00
|
|
|
if user is None:
|
|
|
|
username = "Non renseigné"
|
|
|
|
else:
|
|
|
|
username = user.get_prenomnom()
|
2023-04-17 15:34:00 +02:00
|
|
|
data = {
|
|
|
|
"assiduite_id": self.id,
|
|
|
|
"etudid": self.etudid,
|
2023-07-29 19:04:35 +02:00
|
|
|
"code_nip": self.etudiant.code_nip,
|
2023-04-17 15:34:00 +02:00
|
|
|
"moduleimpl_id": self.moduleimpl_id,
|
|
|
|
"date_debut": self.date_debut,
|
|
|
|
"date_fin": self.date_fin,
|
|
|
|
"etat": etat,
|
2023-07-20 13:10:26 +02:00
|
|
|
"desc": self.description,
|
2023-04-17 15:34:00 +02:00
|
|
|
"entry_date": self.entry_date,
|
2023-06-30 17:24:16 +02:00
|
|
|
"user_id": username,
|
2023-04-17 15:34:00 +02:00
|
|
|
"est_just": self.est_just,
|
2023-07-31 09:41:32 +02:00
|
|
|
"external_data": self.external_data,
|
2023-04-17 15:34:00 +02:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
2023-07-26 13:27:57 +02:00
|
|
|
def __str__(self) -> str:
|
|
|
|
"chaine pour journaux et debug (lisible par humain français)"
|
|
|
|
try:
|
|
|
|
etat_str = EtatAssiduite(self.etat).name.lower().capitalize()
|
|
|
|
except ValueError:
|
|
|
|
etat_str = "Invalide"
|
|
|
|
return f"""{etat_str} {
|
|
|
|
"just." if self.est_just else "non just."
|
|
|
|
} de {
|
|
|
|
self.date_debut.strftime("%d/%m/%Y %Hh%M")
|
|
|
|
} à {
|
|
|
|
self.date_fin.strftime("%d/%m/%Y %Hh%M")
|
|
|
|
}"""
|
|
|
|
|
2023-04-17 15:34:00 +02:00
|
|
|
@classmethod
|
|
|
|
def create_assiduite(
|
|
|
|
cls,
|
|
|
|
etud: Identite,
|
|
|
|
date_debut: datetime,
|
|
|
|
date_fin: datetime,
|
|
|
|
etat: EtatAssiduite,
|
|
|
|
moduleimpl: ModuleImpl = None,
|
|
|
|
description: str = None,
|
|
|
|
entry_date: datetime = None,
|
|
|
|
user_id: int = None,
|
|
|
|
est_just: bool = False,
|
2023-07-31 09:41:32 +02:00
|
|
|
external_data: dict = None,
|
2023-09-12 19:57:39 +02:00
|
|
|
) -> "Assiduite":
|
2023-04-17 15:34:00 +02:00
|
|
|
"""Créer une nouvelle assiduité pour l'étudiant"""
|
|
|
|
# Vérification de non duplication des périodes
|
2023-08-14 01:08:04 +02:00
|
|
|
assiduites: Query = etud.assiduites
|
2023-04-17 15:34:00 +02:00
|
|
|
if is_period_conflicting(date_debut, date_fin, assiduites, Assiduite):
|
|
|
|
raise ScoValueError(
|
|
|
|
"Duplication des assiduités (la période rentrée rentre en conflit avec une assiduité enregistrée)"
|
|
|
|
)
|
2023-08-09 09:57:47 +02:00
|
|
|
|
|
|
|
if not est_just:
|
|
|
|
est_just = (
|
2023-09-11 09:05:29 +02:00
|
|
|
len(
|
|
|
|
get_justifs_from_date(etud.etudid, date_debut, date_fin, valid=True)
|
|
|
|
)
|
|
|
|
> 0
|
2023-08-09 09:57:47 +02:00
|
|
|
)
|
|
|
|
|
2023-04-17 15:34:00 +02:00
|
|
|
if moduleimpl is not None:
|
|
|
|
# Vérification de l'existence du module pour l'étudiant
|
|
|
|
if moduleimpl.est_inscrit(etud):
|
|
|
|
nouv_assiduite = Assiduite(
|
|
|
|
date_debut=date_debut,
|
|
|
|
date_fin=date_fin,
|
|
|
|
etat=etat,
|
|
|
|
etudiant=etud,
|
|
|
|
moduleimpl_id=moduleimpl.id,
|
2023-07-20 13:10:26 +02:00
|
|
|
description=description,
|
2023-04-17 15:34:00 +02:00
|
|
|
entry_date=entry_date,
|
|
|
|
user_id=user_id,
|
|
|
|
est_just=est_just,
|
2023-07-31 09:41:32 +02:00
|
|
|
external_data=external_data,
|
2023-04-17 15:34:00 +02:00
|
|
|
)
|
|
|
|
else:
|
2023-09-20 12:45:24 +02:00
|
|
|
raise ScoValueError("L'étudiant n'est pas inscrit au module")
|
2023-04-17 15:34:00 +02:00
|
|
|
else:
|
|
|
|
nouv_assiduite = Assiduite(
|
|
|
|
date_debut=date_debut,
|
|
|
|
date_fin=date_fin,
|
|
|
|
etat=etat,
|
|
|
|
etudiant=etud,
|
2023-07-20 13:10:26 +02:00
|
|
|
description=description,
|
2023-04-17 15:34:00 +02:00
|
|
|
entry_date=entry_date,
|
|
|
|
user_id=user_id,
|
|
|
|
est_just=est_just,
|
2023-07-31 09:41:32 +02:00
|
|
|
external_data=external_data,
|
2023-04-17 15:34:00 +02:00
|
|
|
)
|
2023-07-31 09:41:32 +02:00
|
|
|
db.session.add(nouv_assiduite)
|
2023-07-26 16:00:23 +02:00
|
|
|
log(f"create_assiduite: {etud.id} {nouv_assiduite}")
|
2023-07-26 13:27:57 +02:00
|
|
|
Scolog.logdb(
|
|
|
|
method="create_assiduite",
|
|
|
|
etudid=etud.id,
|
|
|
|
msg=f"assiduité: {nouv_assiduite}",
|
2023-04-17 15:34:00 +02:00
|
|
|
)
|
|
|
|
return nouv_assiduite
|
|
|
|
|
|
|
|
|
|
|
|
class Justificatif(db.Model):
|
|
|
|
"""
|
|
|
|
Représente un justificatif:
|
|
|
|
- une plage horaire lié à un état et un étudiant
|
|
|
|
- une raison si spécifiée
|
|
|
|
- un fichier si spécifié
|
|
|
|
"""
|
|
|
|
|
|
|
|
__tablename__ = "justificatifs"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
justif_id = db.synonym("id")
|
|
|
|
|
|
|
|
date_debut = db.Column(
|
|
|
|
db.DateTime(timezone=True), server_default=db.func.now(), nullable=False
|
|
|
|
)
|
|
|
|
date_fin = db.Column(
|
|
|
|
db.DateTime(timezone=True), server_default=db.func.now(), nullable=False
|
|
|
|
)
|
|
|
|
|
|
|
|
etudid = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("identite.id", ondelete="CASCADE"),
|
|
|
|
index=True,
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
etat = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
entry_date = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
|
|
|
|
|
|
|
|
user_id = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("user.id", ondelete="SET NULL"),
|
|
|
|
nullable=True,
|
|
|
|
index=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
raison = db.Column(db.Text())
|
|
|
|
|
|
|
|
# Archive_id -> sco_archives_justificatifs.py
|
|
|
|
fichier = db.Column(db.Text())
|
|
|
|
|
2023-07-31 09:41:32 +02:00
|
|
|
# Déclare la relation "joined" car on va très souvent vouloir récupérer
|
|
|
|
# l'étudiant en même tant que le justificatif (perf.: évite nouvelle requete SQL)
|
|
|
|
etudiant = db.relationship(
|
|
|
|
"Identite", back_populates="justificatifs", lazy="joined"
|
|
|
|
)
|
|
|
|
|
|
|
|
external_data = db.Column(db.JSON, nullable=True)
|
2023-07-29 18:32:29 +02:00
|
|
|
|
2023-04-17 15:34:00 +02:00
|
|
|
def to_dict(self, format_api: bool = False) -> dict:
|
|
|
|
"""transformation de l'objet en dictionnaire sérialisable"""
|
|
|
|
|
|
|
|
etat = self.etat
|
2023-06-30 17:24:16 +02:00
|
|
|
username = self.user_id
|
2023-04-17 15:34:00 +02:00
|
|
|
|
|
|
|
if format_api:
|
|
|
|
etat = EtatJustificatif.inverse().get(self.etat).name
|
2023-06-30 17:24:16 +02:00
|
|
|
if self.user_id is not None:
|
2023-07-11 11:35:50 +02:00
|
|
|
user: User = db.session.get(User, self.user_id)
|
2023-06-30 17:24:16 +02:00
|
|
|
if user is None:
|
|
|
|
username = "Non renseigné"
|
|
|
|
else:
|
|
|
|
username = user.get_prenomnom()
|
2023-04-17 15:34:00 +02:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"justif_id": self.justif_id,
|
|
|
|
"etudid": self.etudid,
|
2023-07-31 09:41:32 +02:00
|
|
|
"code_nip": self.etudiant.code_nip,
|
2023-04-17 15:34:00 +02:00
|
|
|
"date_debut": self.date_debut,
|
|
|
|
"date_fin": self.date_fin,
|
|
|
|
"etat": etat,
|
|
|
|
"raison": self.raison,
|
|
|
|
"fichier": self.fichier,
|
|
|
|
"entry_date": self.entry_date,
|
2023-06-30 17:24:16 +02:00
|
|
|
"user_id": username,
|
2023-07-31 09:41:32 +02:00
|
|
|
"external_data": self.external_data,
|
2023-04-17 15:34:00 +02:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
2023-07-26 16:00:23 +02:00
|
|
|
def __str__(self) -> str:
|
|
|
|
"chaine pour journaux et debug (lisible par humain français)"
|
|
|
|
try:
|
|
|
|
etat_str = EtatJustificatif(self.etat).name
|
|
|
|
except ValueError:
|
|
|
|
etat_str = "Invalide"
|
|
|
|
return f"""Justificatif {etat_str} de {
|
|
|
|
self.date_debut.strftime("%d/%m/%Y %Hh%M")
|
|
|
|
} à {
|
|
|
|
self.date_fin.strftime("%d/%m/%Y %Hh%M")
|
|
|
|
}"""
|
|
|
|
|
2023-04-17 15:34:00 +02:00
|
|
|
@classmethod
|
|
|
|
def create_justificatif(
|
|
|
|
cls,
|
|
|
|
etud: Identite,
|
|
|
|
date_debut: datetime,
|
|
|
|
date_fin: datetime,
|
|
|
|
etat: EtatJustificatif,
|
|
|
|
raison: str = None,
|
|
|
|
entry_date: datetime = None,
|
|
|
|
user_id: int = None,
|
2023-07-31 09:41:32 +02:00
|
|
|
external_data: dict = None,
|
2023-09-12 19:57:39 +02:00
|
|
|
) -> "Justificatif":
|
2023-04-17 15:34:00 +02:00
|
|
|
"""Créer un nouveau justificatif pour l'étudiant"""
|
|
|
|
nouv_justificatif = Justificatif(
|
|
|
|
date_debut=date_debut,
|
|
|
|
date_fin=date_fin,
|
|
|
|
etat=etat,
|
|
|
|
etudiant=etud,
|
|
|
|
raison=raison,
|
|
|
|
entry_date=entry_date,
|
|
|
|
user_id=user_id,
|
2023-07-31 09:41:32 +02:00
|
|
|
external_data=external_data,
|
2023-04-17 15:34:00 +02:00
|
|
|
)
|
2023-07-31 09:41:32 +02:00
|
|
|
|
|
|
|
db.session.add(nouv_justificatif)
|
|
|
|
|
2023-07-26 16:00:23 +02:00
|
|
|
log(f"create_justificatif: {etud.id} {nouv_justificatif}")
|
|
|
|
Scolog.logdb(
|
|
|
|
method="create_justificatif",
|
|
|
|
etudid=etud.id,
|
|
|
|
msg=f"justificatif: {nouv_justificatif}",
|
|
|
|
)
|
2023-04-17 15:34:00 +02:00
|
|
|
return nouv_justificatif
|
|
|
|
|
|
|
|
|
|
|
|
def is_period_conflicting(
|
|
|
|
date_debut: datetime,
|
|
|
|
date_fin: datetime,
|
2023-08-14 01:08:04 +02:00
|
|
|
collection: Query,
|
2023-09-12 19:57:39 +02:00
|
|
|
collection_cls: Assiduite | Justificatif,
|
2023-04-17 15:34:00 +02:00
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Vérifie si une date n'entre pas en collision
|
|
|
|
avec les justificatifs ou assiduites déjà présentes
|
|
|
|
"""
|
|
|
|
|
|
|
|
date_debut = localize_datetime(date_debut)
|
|
|
|
date_fin = localize_datetime(date_fin)
|
|
|
|
|
|
|
|
if (
|
|
|
|
collection.filter_by(date_debut=date_debut, date_fin=date_fin).first()
|
|
|
|
is not None
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
|
|
|
|
count: int = collection.filter(
|
|
|
|
collection_cls.date_debut < date_fin, collection_cls.date_fin > date_debut
|
|
|
|
).count()
|
|
|
|
|
|
|
|
return count > 0
|
|
|
|
|
|
|
|
|
|
|
|
def compute_assiduites_justified(
|
2023-07-30 16:34:05 +02:00
|
|
|
etudid: int, justificatifs: list[Justificatif] = None, reset: bool = False
|
2023-04-17 15:34:00 +02:00
|
|
|
) -> list[int]:
|
2023-07-30 16:34:05 +02:00
|
|
|
"""
|
|
|
|
compute_assiduites_justified_faster
|
|
|
|
|
|
|
|
Args:
|
|
|
|
etudid (int): l'identifiant de l'étudiant
|
|
|
|
justificatifs (list[Justificatif]): La liste des justificatifs qui seront utilisés
|
|
|
|
reset (bool, optional): remet à false les assiduites non justifiés. Defaults to False.
|
2023-04-17 15:34:00 +02:00
|
|
|
|
2023-07-30 16:34:05 +02:00
|
|
|
Returns:
|
|
|
|
list[int]: la liste des assiduités qui ont été justifiées.
|
2023-04-17 15:34:00 +02:00
|
|
|
"""
|
2023-07-30 16:34:05 +02:00
|
|
|
if justificatifs is None:
|
|
|
|
justificatifs: Justificatif = Justificatif.query.filter_by(etudid=etudid).all()
|
2023-04-17 15:34:00 +02:00
|
|
|
|
2023-09-05 14:25:38 +02:00
|
|
|
justificatifs = [j for j in justificatifs if j.etat == EtatJustificatif.VALIDE]
|
|
|
|
|
2023-07-30 16:34:05 +02:00
|
|
|
assiduites: Assiduite = Assiduite.query.filter_by(etudid=etudid)
|
2023-04-17 15:34:00 +02:00
|
|
|
|
2023-07-30 16:34:05 +02:00
|
|
|
assiduites_justifiees: list[int] = []
|
|
|
|
|
|
|
|
for assi in assiduites:
|
2023-09-05 14:25:38 +02:00
|
|
|
if assi.etat == EtatAssiduite.PRESENT:
|
|
|
|
continue
|
2023-09-24 09:18:13 +02:00
|
|
|
|
|
|
|
assi_justificatifs = Justificatif.query.filter(
|
|
|
|
Justificatif.etudid == assi.etudid,
|
|
|
|
Justificatif.date_debut <= assi.date_debut,
|
|
|
|
Justificatif.date_fin >= assi.date_fin,
|
|
|
|
Justificatif.etat == EtatJustificatif.VALIDE,
|
|
|
|
).all()
|
|
|
|
|
2023-07-30 16:34:05 +02:00
|
|
|
if any(
|
|
|
|
assi.date_debut >= j.date_debut and assi.date_fin <= j.date_fin
|
2023-09-24 09:18:13 +02:00
|
|
|
for j in justificatifs + assi_justificatifs
|
2023-07-30 16:34:05 +02:00
|
|
|
):
|
2023-04-17 15:34:00 +02:00
|
|
|
assi.est_just = True
|
2023-07-30 16:34:05 +02:00
|
|
|
assiduites_justifiees.append(assi.assiduite_id)
|
2023-04-17 15:34:00 +02:00
|
|
|
db.session.add(assi)
|
2023-07-30 16:34:05 +02:00
|
|
|
elif reset:
|
2023-04-17 15:34:00 +02:00
|
|
|
assi.est_just = False
|
|
|
|
db.session.add(assi)
|
|
|
|
db.session.commit()
|
2023-07-30 16:34:05 +02:00
|
|
|
return assiduites_justifiees
|
2023-08-09 09:57:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_assiduites_justif(assiduite_id: int, long: bool):
|
|
|
|
assi: Assiduite = Assiduite.query.get_or_404(assiduite_id)
|
2023-09-11 09:05:29 +02:00
|
|
|
return get_justifs_from_date(assi.etudid, assi.date_debut, assi.date_fin, long)
|
2023-08-09 09:57:47 +02:00
|
|
|
|
|
|
|
|
2023-09-11 09:05:29 +02:00
|
|
|
def get_justifs_from_date(
|
|
|
|
etudid: int,
|
|
|
|
date_debut: datetime,
|
|
|
|
date_fin: datetime,
|
|
|
|
long: bool = False,
|
|
|
|
valid: bool = False,
|
2023-08-09 09:57:47 +02:00
|
|
|
):
|
2023-09-11 09:05:29 +02:00
|
|
|
justifs: Query = Justificatif.query.filter(
|
2023-08-09 09:57:47 +02:00
|
|
|
Justificatif.etudid == etudid,
|
|
|
|
Justificatif.date_debut <= date_debut,
|
|
|
|
Justificatif.date_fin >= date_fin,
|
|
|
|
)
|
|
|
|
|
2023-09-11 09:05:29 +02:00
|
|
|
if valid:
|
|
|
|
justifs = justifs.filter(Justificatif.etat == EtatJustificatif.VALIDE)
|
|
|
|
|
2023-08-09 09:57:47 +02:00
|
|
|
return [j.justif_id if not long else j.to_dict(True) for j in justifs]
|