2021-08-07 15:20:30 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
|
|
|
|
"""Model : preferences
|
|
|
|
"""
|
2021-09-05 12:30:11 +02:00
|
|
|
from app import db, log
|
2022-01-25 10:45:13 +01:00
|
|
|
from app.comp import bonus_spo
|
2021-11-27 18:55:47 +01:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
|
2021-08-13 00:34:58 +02:00
|
|
|
class ScoPreference(db.Model):
|
2021-09-05 12:30:11 +02:00
|
|
|
"""ScoDoc preferences (par département)"""
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
__tablename__ = "sco_prefs"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
pref_id = db.synonym("id")
|
2021-08-13 00:34:58 +02:00
|
|
|
|
|
|
|
dept_id = db.Column(db.Integer, db.ForeignKey("departement.id"))
|
|
|
|
|
|
|
|
name = db.Column(db.String(128), nullable=False, index=True)
|
2021-08-07 15:20:30 +02:00
|
|
|
value = db.Column(db.Text())
|
|
|
|
formsemestre_id = db.Column(db.Integer, db.ForeignKey("notes_formsemestre.id"))
|
2021-09-05 12:30:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ScoDocSiteConfig(db.Model):
|
|
|
|
"""Config. d'un site
|
|
|
|
Nouveau en ScoDoc 9: va regrouper les paramètres qui dans les versions
|
|
|
|
antérieures étaient dans scodoc_config.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
__tablename__ = "scodoc_site_config"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(128), nullable=False, index=True)
|
|
|
|
value = db.Column(db.Text())
|
|
|
|
|
|
|
|
BONUS_SPORT = "bonus_sport_func_name"
|
2021-09-08 23:00:01 +02:00
|
|
|
NAMES = {
|
|
|
|
BONUS_SPORT: str,
|
|
|
|
"always_require_ine": bool,
|
|
|
|
"SCOLAR_FONT": str,
|
|
|
|
"SCOLAR_FONT_SIZE": str,
|
|
|
|
"SCOLAR_FONT_SIZE_FOOT": str,
|
|
|
|
"INSTITUTION_NAME": str,
|
|
|
|
"INSTITUTION_ADDRESS": str,
|
|
|
|
"INSTITUTION_CITY": str,
|
|
|
|
"DEFAULT_PDF_FOOTER_TEMPLATE": str,
|
|
|
|
}
|
2021-09-05 12:30:11 +02:00
|
|
|
|
|
|
|
def __init__(self, name, value):
|
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<{self.__class__.__name__}('{self.name}', '{self.value}')>"
|
|
|
|
|
2021-09-08 23:00:01 +02:00
|
|
|
def get_dict(self) -> dict:
|
|
|
|
"Returns all data as a dict name = value"
|
|
|
|
return {
|
|
|
|
c.name: self.NAMES.get(c.name, lambda x: x)(c.value)
|
|
|
|
for c in ScoDocSiteConfig.query.all()
|
|
|
|
}
|
|
|
|
|
2021-09-05 12:30:11 +02:00
|
|
|
@classmethod
|
2022-01-25 10:45:13 +01:00
|
|
|
def set_bonus_sport_class(cls, class_name):
|
2021-09-05 12:30:11 +02:00
|
|
|
"""Record bonus_sport config.
|
2022-01-25 10:45:13 +01:00
|
|
|
If class_name not defined, raise NameError
|
2021-09-05 12:30:11 +02:00
|
|
|
"""
|
2022-01-25 10:45:13 +01:00
|
|
|
if class_name not in cls.get_bonus_sport_class_names():
|
|
|
|
raise NameError("invalid class name for bonus_sport")
|
2021-09-05 12:30:11 +02:00
|
|
|
c = ScoDocSiteConfig.query.filter_by(name=cls.BONUS_SPORT).first()
|
|
|
|
if c:
|
2022-01-25 10:45:13 +01:00
|
|
|
log("setting to " + class_name)
|
|
|
|
c.value = class_name
|
2021-09-05 12:30:11 +02:00
|
|
|
else:
|
2022-01-25 10:45:13 +01:00
|
|
|
c = ScoDocSiteConfig(cls.BONUS_SPORT, class_name)
|
2021-09-05 12:30:11 +02:00
|
|
|
db.session.add(c)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-25 10:45:13 +01:00
|
|
|
def get_bonus_sport_class_name(cls):
|
2021-09-05 12:30:11 +02:00
|
|
|
"""Get configured bonus function name, or None if None."""
|
2022-01-25 10:45:13 +01:00
|
|
|
klass = cls.get_bonus_sport_class_from_name()
|
|
|
|
if klass is None:
|
2021-09-05 12:30:11 +02:00
|
|
|
return ""
|
|
|
|
else:
|
2022-01-25 10:45:13 +01:00
|
|
|
return klass.name
|
2021-09-05 12:30:11 +02:00
|
|
|
|
|
|
|
@classmethod
|
2022-01-25 10:45:13 +01:00
|
|
|
def get_bonus_sport_class(cls):
|
2021-09-05 12:30:11 +02:00
|
|
|
"""Get configured bonus function, or None if None."""
|
2022-01-25 10:45:13 +01:00
|
|
|
return cls.get_bonus_sport_class_from_name()
|
2021-09-05 12:30:11 +02:00
|
|
|
|
|
|
|
@classmethod
|
2022-01-25 10:45:13 +01:00
|
|
|
def get_bonus_sport_class_from_name(cls, class_name=None):
|
|
|
|
"""returns bonus class with specified name.
|
2021-09-05 12:30:11 +02:00
|
|
|
If name not specified, return the configured function.
|
|
|
|
None if no bonus function configured.
|
2022-01-25 10:45:13 +01:00
|
|
|
Raises ScoValueError if class_name not found in module bonus_sport.
|
2021-09-05 12:30:11 +02:00
|
|
|
"""
|
2022-01-25 10:45:13 +01:00
|
|
|
if class_name is None:
|
2021-09-05 12:30:11 +02:00
|
|
|
c = ScoDocSiteConfig.query.filter_by(name=cls.BONUS_SPORT).first()
|
|
|
|
if c is None:
|
|
|
|
return None
|
2022-01-25 10:45:13 +01:00
|
|
|
class_name = c.value
|
|
|
|
if class_name == "": # pas de bonus défini
|
2021-09-05 12:30:11 +02:00
|
|
|
return None
|
2022-01-25 10:45:13 +01:00
|
|
|
klass = bonus_spo.get_bonus_class_dict().get(class_name)
|
|
|
|
if klass is None:
|
2021-11-27 18:55:47 +01:00
|
|
|
raise ScoValueError(
|
2022-01-25 10:45:13 +01:00
|
|
|
f"""Fonction de calcul bonus sport inexistante: {class_name}.
|
2021-11-27 18:55:47 +01:00
|
|
|
(contacter votre administrateur local)."""
|
|
|
|
)
|
2022-01-25 10:45:13 +01:00
|
|
|
return klass
|
2021-09-05 12:30:11 +02:00
|
|
|
|
|
|
|
@classmethod
|
2022-01-25 10:45:13 +01:00
|
|
|
def get_bonus_sport_class_names(cls):
|
2021-09-05 12:30:11 +02:00
|
|
|
"""List available functions names
|
|
|
|
(starting with empty string to represent "no bonus function").
|
|
|
|
"""
|
2022-01-25 10:45:13 +01:00
|
|
|
return [""] + sorted(bonus_spo.get_bonus_class_dict().keys())
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_bonus_sport_func(cls):
|
|
|
|
"""Fonction bonus_sport ScoDoc 7 XXX
|
|
|
|
Transitoire pour les tests durant la transition #sco92
|
|
|
|
"""
|
|
|
|
"""returns bonus func with specified name.
|
|
|
|
If name not specified, return the configured function.
|
|
|
|
None if no bonus function configured.
|
|
|
|
Raises ScoValueError if func_name not found in module bonus_sport.
|
|
|
|
"""
|
|
|
|
from app.scodoc import bonus_sport
|
|
|
|
|
|
|
|
c = ScoDocSiteConfig.query.filter_by(name=cls.BONUS_SPORT).first()
|
|
|
|
if c is None:
|
|
|
|
return None
|
|
|
|
func_name = c.value
|
|
|
|
if func_name == "": # pas de bonus défini
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return getattr(bonus_sport, func_name)
|
|
|
|
except AttributeError:
|
|
|
|
raise ScoValueError(
|
|
|
|
f"""Fonction de calcul maison inexistante: {func_name}.
|
|
|
|
(contacter votre administrateur local)."""
|
|
|
|
)
|