2021-08-07 15:20:30 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
2022-07-20 15:07:31 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
|
|
|
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
|
|
|
|
# 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
|
|
|
"""
|
|
|
|
|
|
|
|
from app import db
|
|
|
|
from app.models import SHORT_STR_LEN
|
2021-08-14 18:54:32 +02:00
|
|
|
from app.models import GROUPNAME_STR_LEN
|
2022-07-20 15:07:31 +02:00
|
|
|
from app.scodoc import sco_utils as scu
|
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)
|
2021-08-07 15:20:30 +02:00
|
|
|
numero = db.Column(db.Integer)
|
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-05-26 23:45:57 +02:00
|
|
|
# Editable ? (faux pour les groupes de parcours)
|
|
|
|
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",
|
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
|
|
|
|
if not len(partition_name.strip()) > 0:
|
|
|
|
return False
|
|
|
|
if (not existing) and (
|
|
|
|
partition_name in [p.partition_name for p in formsemestre.partitions]
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_parcours(self) -> bool:
|
|
|
|
"Vrai s'il s'agit de la partitoon de parcours"
|
|
|
|
return self.partition_name == scu.PARTITION_PARCOURS
|
|
|
|
|
2022-05-18 20:43:01 +02:00
|
|
|
def to_dict(self, with_groups=False) -> dict:
|
|
|
|
"""as a dict, with or without groups"""
|
2022-07-19 22:17:10 +02:00
|
|
|
d = dict(self.__dict__)
|
|
|
|
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:
|
2022-07-23 09:07:53 +02:00
|
|
|
groups = sorted(self.groups, key=lambda g: g.group_name)
|
|
|
|
# un dict et non plus une liste, pour JSON
|
|
|
|
d["groups"] = {
|
|
|
|
group.id: group.to_dict(with_partition=False) for group in groups
|
|
|
|
}
|
2022-05-18 20:43:01 +02:00
|
|
|
return d
|
|
|
|
|
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"))
|
|
|
|
# "A", "C2", ... (NULL for 'all'):
|
2021-08-14 18:54:32 +02:00
|
|
|
group_name = db.Column(db.String(GROUPNAME_STR_LEN))
|
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)'}">"""
|
|
|
|
)
|
|
|
|
|
2022-04-08 16:36:56 +02:00
|
|
|
def get_nom_with_part(self) -> str:
|
|
|
|
"Nom avec partition: 'TD A'"
|
|
|
|
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"""
|
|
|
|
d = {
|
|
|
|
"id": self.id,
|
|
|
|
"partition_id": self.partition_id,
|
|
|
|
"name": self.group_name,
|
|
|
|
}
|
|
|
|
if with_partition:
|
|
|
|
d["partition"] = self.partition.to_dict(with_groups=False)
|
|
|
|
return d
|
|
|
|
|
2022-07-20 09:50:02 +02:00
|
|
|
@classmethod
|
|
|
|
def check_name(
|
|
|
|
cls, partition: "Partition", group_name: str, existing=False
|
|
|
|
) -> 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.
|
|
|
|
"""
|
|
|
|
if not isinstance(group_name, str):
|
|
|
|
return False
|
|
|
|
if not len(group_name.strip()) > 0:
|
|
|
|
return False
|
|
|
|
if (not existing) and (group_name in [g.group_name for g in partition.groups]):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
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"))
|