forked from ScoDoc/DocScoDoc
82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
# -*- coding: UTF-8 -*
|
|
|
|
"""ScoDoc models : departements
|
|
"""
|
|
|
|
from app import db
|
|
from app.models import SHORT_STR_LEN
|
|
from app.models.preferences import ScoPreference
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
|
|
|
|
|
class Departement(db.Model):
|
|
"""Un département ScoDoc"""
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
acronym = db.Column(
|
|
db.String(SHORT_STR_LEN), nullable=False, index=True, unique=True
|
|
) # ne change jamais, voir la pref. DeptName
|
|
description = db.Column(db.Text()) # pas utilisé par ScoDoc : voir DeptFullName
|
|
date_creation = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
|
|
visible = db.Column(
|
|
db.Boolean(), nullable=False, default=True, server_default="true"
|
|
) # sur page d'accueil
|
|
|
|
# entreprises = db.relationship("Entreprise", lazy="dynamic", backref="departement")
|
|
etudiants = db.relationship("Identite", lazy="dynamic", backref="departement")
|
|
formations = db.relationship("Formation", lazy="dynamic", backref="departement")
|
|
formsemestres = db.relationship(
|
|
"FormSemestre", lazy="dynamic", backref="departement"
|
|
)
|
|
preferences = db.relationship(
|
|
"ScoPreference", lazy="dynamic", backref="departement"
|
|
)
|
|
semsets = db.relationship("NotesSemSet", lazy="dynamic", backref="departement")
|
|
referentiels = db.relationship(
|
|
"ApcReferentielCompetences", lazy="dynamic", backref="departement"
|
|
)
|
|
|
|
def __repr__(self):
|
|
return f"<{self.__class__.__name__}(id={self.id}, acronym='{self.acronym}')>"
|
|
|
|
def to_dict(self, with_dept_name=True, with_dept_preferences=False):
|
|
data = {
|
|
"id": self.id,
|
|
"acronym": self.acronym,
|
|
"description": self.description,
|
|
"visible": self.visible,
|
|
"date_creation": self.date_creation,
|
|
}
|
|
if with_dept_name:
|
|
pref = ScoPreference.query.filter_by(
|
|
dept_id=self.id, name="DeptName"
|
|
).first()
|
|
data["dept_name"] = pref.value if pref else None
|
|
# Ceci n'est pas encore utilisé, mais pourrait être publié
|
|
# par l'API après nettoyage des préférences.
|
|
if with_dept_preferences:
|
|
data["preferences"] = {
|
|
p.name: p.value for p in ScoPreference.query.filter_by(dept_id=self.id)
|
|
}
|
|
return data
|
|
|
|
@classmethod
|
|
def from_acronym(cls, acronym):
|
|
dept = cls.query.filter_by(acronym=acronym).first_or_404()
|
|
return dept
|
|
|
|
|
|
def create_dept(acronym: str, visible=True) -> Departement:
|
|
"Create new departement"
|
|
from app.models import ScoPreference
|
|
|
|
existing = Departement.query.filter_by(acronym=acronym).count()
|
|
if existing:
|
|
raise ScoValueError(f"acronyme {acronym} déjà existant")
|
|
departement = Departement(acronym=acronym, visible=visible)
|
|
p1 = ScoPreference(name="DeptName", value=acronym, departement=departement)
|
|
db.session.add(p1)
|
|
db.session.add(departement)
|
|
db.session.commit()
|
|
return departement
|