2021-08-07 15:20:30 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
2022-07-20 15:07:31 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2023-01-02 13:16:27 +01:00
|
|
|
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
|
2022-07-20 15:07:31 +02:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
2021-08-07 15:20:30 +02:00
|
|
|
|
2022-07-20 15:07:31 +02:00
|
|
|
"""ScoDoc models: Groups & partitions
|
2021-08-07 15:20:30 +02:00
|
|
|
"""
|
2023-04-03 17:46:31 +02:00
|
|
|
from operator import attrgetter
|
2023-07-05 19:15:33 +02:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2021-08-07 15:20:30 +02:00
|
|
|
|
2023-07-08 16:35:32 +02:00
|
|
|
from app import db, log
|
2023-08-31 13:38:17 +02:00
|
|
|
from app.models import Scolog, GROUPNAME_STR_LEN, SHORT_STR_LEN
|
2023-09-20 19:09:02 +02:00
|
|
|
from app.models.etudiants import Identite
|
2023-08-31 13:38:17 +02:00
|
|
|
from app.scodoc import sco_cache
|
2022-07-20 15:07:31 +02:00
|
|
|
from app.scodoc import sco_utils as scu
|
2023-07-08 16:35:32 +02:00
|
|
|
from app.scodoc.sco_exceptions import AccessDenied, ScoValueError
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Partition(db.Model):
|
|
|
|
"""Partition: découpage d'une promotion en groupes"""
|
|
|
|
|
|
|
|
__table_args__ = (db.UniqueConstraint("formsemestre_id", "partition_name"),)
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
partition_id = db.synonym("id")
|
|
|
|
formsemestre_id = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("notes_formsemestre.id"),
|
2021-08-08 17:38:46 +02:00
|
|
|
index=True,
|
2021-08-07 15:20:30 +02:00
|
|
|
)
|
|
|
|
# "TD", "TP", ... (NULL for 'all')
|
|
|
|
partition_name = db.Column(db.String(SHORT_STR_LEN))
|
2022-05-26 23:45:57 +02:00
|
|
|
# Numero = ordre de presentation)
|
2023-04-03 17:46:31 +02:00
|
|
|
numero = db.Column(db.Integer, nullable=False, default=0)
|
2022-05-18 20:43:01 +02:00
|
|
|
# Calculer le rang ?
|
2021-08-10 00:23:30 +02:00
|
|
|
bul_show_rank = db.Column(
|
|
|
|
db.Boolean(), nullable=False, default=False, server_default="false"
|
|
|
|
)
|
2022-05-18 20:43:01 +02:00
|
|
|
# Montrer quand on indique les groupes de l'étudiant ?
|
2021-08-10 00:23:30 +02:00
|
|
|
show_in_lists = db.Column(
|
|
|
|
db.Boolean(), nullable=False, default=True, server_default="true"
|
|
|
|
)
|
2022-08-03 21:42:53 +02:00
|
|
|
# Editable (créer/renommer groupes) ? (faux pour les groupes de parcours)
|
2022-05-26 23:45:57 +02:00
|
|
|
groups_editable = db.Column(
|
|
|
|
db.Boolean(), nullable=False, default=True, server_default="true"
|
|
|
|
)
|
2022-02-17 18:13:04 +01:00
|
|
|
groups = db.relationship(
|
|
|
|
"GroupDescr",
|
|
|
|
backref=db.backref("partition", lazy=True),
|
|
|
|
lazy="dynamic",
|
2022-07-20 15:07:31 +02:00
|
|
|
cascade="all, delete-orphan",
|
2023-09-20 19:09:02 +02:00
|
|
|
order_by="GroupDescr.numero, GroupDescr.group_name",
|
2022-02-17 18:13:04 +01:00
|
|
|
)
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super(Partition, self).__init__(**kwargs)
|
|
|
|
if self.numero is None:
|
|
|
|
# génère numero à la création
|
|
|
|
last_partition = Partition.query.order_by(Partition.numero.desc()).first()
|
|
|
|
if last_partition:
|
|
|
|
self.numero = last_partition.numero + 1
|
|
|
|
else:
|
|
|
|
self.numero = 1
|
|
|
|
|
2022-02-17 18:13:04 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return f"""<{self.__class__.__name__} {self.id} "{self.partition_name or '(default)'}">"""
|
|
|
|
|
2022-07-20 15:07:31 +02:00
|
|
|
@classmethod
|
|
|
|
def check_name(
|
|
|
|
cls, formsemestre: "FormSemestre", partition_name: str, existing=False
|
|
|
|
) -> bool:
|
|
|
|
"""check if a partition named 'partition_name' can be created in the given formsemestre.
|
|
|
|
If existing is True, allow a partition_name already existing in the formsemestre.
|
|
|
|
"""
|
|
|
|
if not isinstance(partition_name, str):
|
|
|
|
return False
|
2023-02-22 02:13:06 +01:00
|
|
|
if not (0 < len(partition_name.strip()) < SHORT_STR_LEN):
|
2022-07-20 15:07:31 +02:00
|
|
|
return False
|
|
|
|
if (not existing) and (
|
|
|
|
partition_name in [p.partition_name for p in formsemestre.partitions]
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-08-31 13:38:17 +02:00
|
|
|
@classmethod
|
|
|
|
def formsemestre_remove_etud(cls, formsemestre_id: int, etud: "Identite"):
|
|
|
|
"retire l'étudiant de toutes les partitions de ce semestre"
|
|
|
|
for group in GroupDescr.query.join(Partition).filter_by(
|
|
|
|
formsemestre_id=formsemestre_id
|
|
|
|
):
|
|
|
|
group.remove_etud(etud)
|
|
|
|
|
2022-07-20 15:07:31 +02:00
|
|
|
def is_parcours(self) -> bool:
|
2022-08-03 21:42:53 +02:00
|
|
|
"Vrai s'il s'agit de la partition de parcours"
|
2022-07-20 15:07:31 +02:00
|
|
|
return self.partition_name == scu.PARTITION_PARCOURS
|
|
|
|
|
2023-04-12 13:21:13 +02:00
|
|
|
def to_dict(self, with_groups=False, str_keys: bool = False) -> dict:
|
|
|
|
"""as a dict, with or without groups.
|
|
|
|
If str_keys, convert integer dict keys to strings (useful for JSON)
|
|
|
|
"""
|
2022-07-19 22:17:10 +02:00
|
|
|
d = dict(self.__dict__)
|
2023-01-22 22:15:56 +01:00
|
|
|
d["partition_id"] = self.id
|
2022-07-19 22:17:10 +02:00
|
|
|
d.pop("_sa_instance_state", None)
|
2022-07-20 22:33:41 +02:00
|
|
|
d.pop("formsemestre", None)
|
2022-07-19 22:17:10 +02:00
|
|
|
|
2022-05-18 20:43:01 +02:00
|
|
|
if with_groups:
|
2023-04-03 17:46:31 +02:00
|
|
|
groups = sorted(self.groups, key=attrgetter("numero", "group_name"))
|
2022-07-23 09:07:53 +02:00
|
|
|
# un dict et non plus une liste, pour JSON
|
2023-04-12 13:21:13 +02:00
|
|
|
if str_keys:
|
|
|
|
d["groups"] = {
|
|
|
|
str(group.id): group.to_dict(with_partition=False)
|
|
|
|
for group in groups
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
d["groups"] = {
|
|
|
|
group.id: group.to_dict(with_partition=False) for group in groups
|
|
|
|
}
|
2022-05-18 20:43:01 +02:00
|
|
|
return d
|
|
|
|
|
2023-03-27 18:30:36 +02:00
|
|
|
def get_etud_group(self, etudid: int) -> "GroupDescr":
|
|
|
|
"Le groupe de l'étudiant dans cette partition, ou None si pas présent"
|
|
|
|
return (
|
|
|
|
GroupDescr.query.filter_by(partition_id=self.id)
|
|
|
|
.join(group_membership)
|
|
|
|
.filter_by(etudid=etudid)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
2023-07-08 16:35:32 +02:00
|
|
|
def set_etud_group(self, etud: "Identite", group: "GroupDescr") -> bool:
|
2023-07-05 19:15:33 +02:00
|
|
|
"""Affect etudid to group_id in given partition.
|
|
|
|
Raises IntegrityError si conflit,
|
|
|
|
or ValueError si ce group_id n'est pas dans cette partition
|
|
|
|
ou que l'étudiant n'est pas inscrit au semestre.
|
2023-07-08 13:57:44 +02:00
|
|
|
Return True si changement, False s'il était déjà dans ce groupe.
|
2023-07-05 19:15:33 +02:00
|
|
|
"""
|
|
|
|
if not group.id in (g.id for g in self.groups):
|
|
|
|
raise ScoValueError(
|
2023-07-08 16:35:32 +02:00
|
|
|
f"""Le groupe {group.id} n'est pas dans la partition {
|
|
|
|
self.partition_name or "tous"}"""
|
2023-07-05 19:15:33 +02:00
|
|
|
)
|
2023-07-08 16:35:32 +02:00
|
|
|
if etud.id not in (e.id for e in self.formsemestre.etuds):
|
2023-07-05 19:15:33 +02:00
|
|
|
raise ScoValueError(
|
2023-07-08 16:35:32 +02:00
|
|
|
f"""étudiant {etud.nomprenom} non inscrit au formsemestre du groupe {
|
|
|
|
group.group_name}"""
|
2023-07-05 19:15:33 +02:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
existing_row = (
|
|
|
|
db.session.query(group_membership)
|
2023-07-08 16:35:32 +02:00
|
|
|
.filter_by(etudid=etud.id)
|
2023-07-05 19:15:33 +02:00
|
|
|
.join(GroupDescr)
|
|
|
|
.filter_by(partition_id=self.id)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if existing_row:
|
2023-07-08 16:35:32 +02:00
|
|
|
existing_group_id = existing_row[1]
|
2023-07-08 13:57:44 +02:00
|
|
|
if group.id == existing_group_id:
|
|
|
|
return False
|
2023-07-08 16:35:32 +02:00
|
|
|
# Fait le changement avec l'ORM sinon risque élevé de blocage
|
2023-07-11 06:57:38 +02:00
|
|
|
existing_group = db.session.get(GroupDescr, existing_group_id)
|
2023-07-08 16:35:32 +02:00
|
|
|
db.session.commit()
|
|
|
|
group.etuds.append(etud)
|
|
|
|
existing_group.etuds.remove(etud)
|
|
|
|
db.session.add(etud)
|
|
|
|
db.session.add(existing_group)
|
|
|
|
db.session.add(group)
|
2023-07-05 19:15:33 +02:00
|
|
|
else:
|
|
|
|
new_row = group_membership.insert().values(
|
2023-07-08 16:35:32 +02:00
|
|
|
etudid=etud.id, group_id=group.id
|
2023-07-05 19:15:33 +02:00
|
|
|
)
|
|
|
|
db.session.execute(new_row)
|
|
|
|
db.session.commit()
|
|
|
|
except IntegrityError:
|
|
|
|
db.session.rollback()
|
|
|
|
raise
|
2023-07-08 13:57:44 +02:00
|
|
|
return True
|
2023-07-05 19:15:33 +02:00
|
|
|
|
2023-07-08 16:35:32 +02:00
|
|
|
def create_group(self, group_name="", default=False) -> "GroupDescr":
|
|
|
|
"Crée un groupe dans cette partition"
|
|
|
|
if not self.formsemestre.can_change_groups():
|
|
|
|
raise AccessDenied(
|
2023-11-06 22:05:38 +01:00
|
|
|
"""Vous n'avez pas le droit d'effectuer cette opération,
|
2023-07-08 16:35:32 +02:00
|
|
|
ou bien le semestre est verrouillé !"""
|
|
|
|
)
|
|
|
|
if group_name:
|
|
|
|
group_name = group_name.strip()
|
|
|
|
if not group_name and not default:
|
|
|
|
raise ValueError("invalid group name: ()")
|
|
|
|
if not GroupDescr.check_name(self, group_name, default=default):
|
|
|
|
raise ScoValueError(
|
|
|
|
f"Le groupe {group_name} existe déjà dans cette partition"
|
|
|
|
)
|
|
|
|
numeros = [g.numero if g.numero is not None else 0 for g in self.groups]
|
|
|
|
if len(numeros) > 0:
|
|
|
|
new_numero = max(numeros) + 1
|
|
|
|
else:
|
|
|
|
new_numero = 0
|
|
|
|
group = GroupDescr(partition=self, group_name=group_name, numero=new_numero)
|
|
|
|
db.session.add(group)
|
|
|
|
db.session.commit()
|
|
|
|
log(f"create_group: created group_id={group.id}")
|
|
|
|
#
|
|
|
|
return group
|
|
|
|
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
class GroupDescr(db.Model):
|
|
|
|
"""Description d'un groupe d'une partition"""
|
|
|
|
|
|
|
|
__tablename__ = "group_descr"
|
|
|
|
__table_args__ = (db.UniqueConstraint("partition_id", "group_name"),)
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
group_id = db.synonym("id")
|
|
|
|
partition_id = db.Column(db.Integer, db.ForeignKey("partition.id"))
|
2021-08-14 18:54:32 +02:00
|
|
|
group_name = db.Column(db.String(GROUPNAME_STR_LEN))
|
2023-11-06 22:05:38 +01:00
|
|
|
"""nom du groupe: "A", "C2", ... (NULL for 'all')"""
|
|
|
|
edt_id: str | None = db.Column(db.Text(), index=True, nullable=True)
|
|
|
|
"identifiant emplois du temps (unicité non imposée)"
|
2023-04-03 17:46:31 +02:00
|
|
|
numero = db.Column(db.Integer, nullable=False, default=0)
|
2023-11-06 22:05:38 +01:00
|
|
|
"Numero = ordre de presentation"
|
2021-08-07 15:20:30 +02:00
|
|
|
|
2022-03-09 18:03:18 +01:00
|
|
|
etuds = db.relationship(
|
|
|
|
"Identite",
|
|
|
|
secondary="group_membership",
|
|
|
|
lazy="dynamic",
|
|
|
|
)
|
|
|
|
|
2022-02-17 18:13:04 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
f"""<{self.__class__.__name__} {self.id} "{self.group_name or '(tous)'}">"""
|
|
|
|
)
|
|
|
|
|
2023-11-11 18:13:18 +01:00
|
|
|
def get_nom_with_part(self, default="-") -> str:
|
|
|
|
"""Nom avec partition: 'TD A'
|
|
|
|
Si groupe par défaut (tous), utilise default ou "-"
|
|
|
|
"""
|
|
|
|
if self.partition.partition_name is None:
|
|
|
|
return default
|
2022-04-08 16:36:56 +02:00
|
|
|
return f"{self.partition.partition_name or ''} {self.group_name or '-'}"
|
|
|
|
|
2022-05-18 20:43:01 +02:00
|
|
|
def to_dict(self, with_partition=True) -> dict:
|
|
|
|
"""as a dict, with or without partition"""
|
2022-08-02 09:48:11 +02:00
|
|
|
d = dict(self.__dict__)
|
|
|
|
d.pop("_sa_instance_state", None)
|
2022-05-18 20:43:01 +02:00
|
|
|
if with_partition:
|
2022-07-25 06:53:35 +02:00
|
|
|
d["partition"] = self.partition.to_dict(with_groups=False)
|
2022-05-18 20:43:01 +02:00
|
|
|
return d
|
|
|
|
|
2023-11-11 18:13:18 +01:00
|
|
|
def get_edt_id(self) -> str:
|
|
|
|
"l'id pour l'emploi du temps: à défaut, le nom scodoc du groupe"
|
|
|
|
return self.edt_id or self.group_name or ""
|
|
|
|
|
2023-09-20 19:09:02 +02:00
|
|
|
def get_nb_inscrits(self) -> int:
|
|
|
|
"""Nombre inscrits à ce group et au formsemestre.
|
|
|
|
C'est nécessaire car lors d'une désinscription, on conserve l'appartenance
|
2023-11-11 18:13:18 +01:00
|
|
|
aux groupes pour faciliter une éventuelle ré-inscription.
|
2023-09-20 19:09:02 +02:00
|
|
|
"""
|
|
|
|
from app.models.formsemestre import FormSemestreInscription
|
|
|
|
|
|
|
|
return (
|
|
|
|
Identite.query.join(group_membership)
|
|
|
|
.filter_by(group_id=self.id)
|
|
|
|
.join(FormSemestreInscription)
|
|
|
|
.filter_by(formsemestre_id=self.partition.formsemestre.id)
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
|
2022-07-20 09:50:02 +02:00
|
|
|
@classmethod
|
|
|
|
def check_name(
|
2022-09-03 10:07:34 +02:00
|
|
|
cls, partition: "Partition", group_name: str, existing=False, default=False
|
2022-07-20 09:50:02 +02:00
|
|
|
) -> bool:
|
|
|
|
"""check if a group named 'group_name' can be created in the given partition.
|
|
|
|
If existing is True, allow a group_name already existing in the partition.
|
2022-09-03 10:07:34 +02:00
|
|
|
If default, name must be empty and default group must not yet exists.
|
2022-07-20 09:50:02 +02:00
|
|
|
"""
|
|
|
|
if not isinstance(group_name, str):
|
|
|
|
return False
|
2023-02-22 02:13:06 +01:00
|
|
|
if not default and not (0 < len(group_name.strip()) < GROUPNAME_STR_LEN):
|
2022-07-20 09:50:02 +02:00
|
|
|
return False
|
|
|
|
if (not existing) and (group_name in [g.group_name for g in partition.groups]):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-11-06 22:05:38 +01:00
|
|
|
def set_name(
|
|
|
|
self, group_name: str, edt_id: str | bool = False, dest_url: str = None
|
|
|
|
):
|
|
|
|
"""Set group name, and optionally edt_id.
|
|
|
|
Check permission and invalidate caches. Commit session.
|
|
|
|
dest_url is used for error messages.
|
|
|
|
"""
|
|
|
|
if not self.partition.formsemestre.can_change_groups():
|
|
|
|
raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !")
|
|
|
|
if self.group_name is None:
|
|
|
|
raise ValueError("can't set a name to default group")
|
|
|
|
|
|
|
|
if group_name:
|
|
|
|
group_name = group_name.strip()
|
|
|
|
if not group_name:
|
|
|
|
raise ScoValueError("nom de groupe vide !", dest_url=dest_url)
|
|
|
|
if group_name != self.group_name and not GroupDescr.check_name(
|
|
|
|
self.partition, group_name
|
|
|
|
):
|
|
|
|
raise ScoValueError(
|
|
|
|
"Le nom de groupe existe déjà dans la partition", dest_url=dest_url
|
|
|
|
)
|
|
|
|
|
|
|
|
self.group_name = group_name
|
|
|
|
if edt_id is not False:
|
|
|
|
if isinstance(edt_id, str):
|
|
|
|
edt_id = edt_id.strip() or None
|
|
|
|
self.edt_id = edt_id
|
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
sco_cache.invalidate_formsemestre(
|
|
|
|
formsemestre_id=self.partition.formsemestre_id
|
|
|
|
)
|
|
|
|
|
2023-08-31 13:38:17 +02:00
|
|
|
def remove_etud(self, etud: "Identite"):
|
|
|
|
"Enlève l'étudiant de ce groupe s'il en fait partie (ne fait rien sinon)"
|
|
|
|
if etud in self.etuds:
|
|
|
|
self.etuds.remove(etud)
|
|
|
|
db.session.commit()
|
|
|
|
Scolog.logdb(
|
|
|
|
method="group_remove_etud",
|
|
|
|
etudid=etud.id,
|
|
|
|
msg=f"Retrait du groupe {self.group_name} de {self.partition.partition_name}",
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
# Update parcours
|
|
|
|
if self.partition.partition_name == scu.PARTITION_PARCOURS:
|
|
|
|
self.partition.formsemestre.update_inscriptions_parcours_from_groups(
|
|
|
|
etudid=etud.id
|
|
|
|
)
|
|
|
|
sco_cache.invalidate_formsemestre(self.partition.formsemestre_id)
|
|
|
|
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
group_membership = db.Table(
|
|
|
|
"group_membership",
|
2022-05-26 03:55:03 +02:00
|
|
|
db.Column("etudid", db.Integer, db.ForeignKey("identite.id", ondelete="CASCADE")),
|
2021-08-07 15:20:30 +02:00
|
|
|
db.Column("group_id", db.Integer, db.ForeignKey("group_descr.id")),
|
|
|
|
db.UniqueConstraint("etudid", "group_id"),
|
|
|
|
)
|
2022-05-18 20:43:01 +02:00
|
|
|
# class GroupMembership(db.Model):
|
|
|
|
# """Association groupe / étudiant"""
|
|
|
|
|
|
|
|
# __tablename__ = "group_membership"
|
|
|
|
# __table_args__ = (db.UniqueConstraint("etudid", "group_id"),)
|
|
|
|
# id = db.Column(db.Integer, primary_key=True)
|
2022-05-26 03:55:03 +02:00
|
|
|
# etudid = db.Column(db.Integer, db.ForeignKey("identite.id", ondelete="CASCADE"))
|
2022-05-18 20:43:01 +02:00
|
|
|
# group_id = db.Column(db.Integer, db.ForeignKey("group_descr.id"))
|