ScoDoc/app/models/config.py

332 lines
10 KiB
Python
Raw Normal View History

2022-01-21 10:27:47 +01:00
# -*- coding: UTF-8 -*
"""Model : site config WORK IN PROGRESS #WIP
"""
from flask import flash
2023-02-26 23:27:40 +01:00
from app import current_app, db, log
from app.comp import bonus_spo
from app.scodoc import sco_utils as scu
2022-01-21 10:27:47 +01:00
from app.scodoc.codes_cursus import (
ABAN,
ABL,
2022-01-21 10:27:47 +01:00
ADC,
ADJ,
2023-01-12 14:14:54 +01:00
ADJR,
2022-01-21 10:27:47 +01:00
ADM,
AJ,
ATB,
ATJ,
ATT,
CMP,
DEF,
DEM,
EXCLU,
2022-01-21 10:27:47 +01:00
NAR,
PASD,
PAS1NCI,
2022-01-21 10:27:47 +01:00
RAT,
2022-06-30 15:32:22 +02:00
RED,
2022-01-21 10:27:47 +01:00
)
CODES_SCODOC_TO_APO = {
ABAN: "ABAN",
ABL: "ABL",
2022-01-21 10:27:47 +01:00
ADC: "ADMC",
ADJ: "ADM",
2023-01-12 14:14:54 +01:00
ADJR: "ADM",
2022-01-21 10:27:47 +01:00
ADM: "ADM",
AJ: "AJ",
ATB: "AJAC",
ATJ: "AJAC",
ATT: "AJAC",
CMP: "COMP",
DEF: "NAR",
DEM: "NAR",
EXCLU: "EXC",
2022-01-21 10:27:47 +01:00
NAR: "NAR",
PASD: "PASD",
PAS1NCI: "PAS1NCI",
2022-01-21 10:27:47 +01:00
RAT: "ATT",
2022-06-30 15:32:22 +02:00
RED: "RED",
"NOTES_FMT": "%3.2f",
2022-01-21 10:27:47 +01:00
}
def code_scodoc_to_apo_default(code):
"""Conversion code jury ScoDoc en code Apogée
(codes par défaut, c'est configurable via ScoDocSiteConfig.get_code_apo)
"""
return CODES_SCODOC_TO_APO.get(code, "DEF")
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"
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,
"enable_entreprises": bool,
"month_debut_annee_scolaire": int,
"month_debut_periode2": int,
2023-02-26 21:24:07 +01:00
# CAS
"cas_enable": bool,
"cas_server": str,
"cas_attribute_id": str,
2022-01-21 10:27:47 +01:00
}
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return f"<{self.__class__.__name__}('{self.name}', '{self.value}')>"
@classmethod
def get_dict(cls) -> dict:
"Returns all data as a dict name = value"
return {
c.name: cls.NAMES.get(c.name, lambda x: x)(c.value)
for c in ScoDocSiteConfig.query.all()
}
@classmethod
def set_bonus_sport_class(cls, class_name):
2022-01-21 10:27:47 +01:00
"""Record bonus_sport config.
If class_name not defined, raise NameError
2022-01-21 10:27:47 +01:00
"""
if class_name not in cls.get_bonus_sport_class_names():
raise NameError("invalid class name for bonus_sport")
2022-01-21 10:27:47 +01:00
c = ScoDocSiteConfig.query.filter_by(name=cls.BONUS_SPORT).first()
if c:
log("setting to " + class_name)
c.value = class_name
2022-01-21 10:27:47 +01:00
else:
c = ScoDocSiteConfig(cls.BONUS_SPORT, class_name)
2022-01-21 10:27:47 +01:00
db.session.add(c)
db.session.commit()
@classmethod
def get_bonus_sport_class_name(cls):
2022-01-21 10:27:47 +01:00
"""Get configured bonus function name, or None if None."""
klass = cls.get_bonus_sport_class_from_name()
if klass is None:
2022-01-21 10:27:47 +01:00
return ""
else:
return klass.name
2022-01-21 10:27:47 +01:00
2022-01-25 22:18:49 +01:00
@classmethod
def get_bonus_sport_class(cls):
"""Get configured bonus function, or None if None."""
return cls.get_bonus_sport_class_from_name()
2022-01-21 10:27:47 +01:00
@classmethod
def get_bonus_sport_class_from_name(cls, class_name=None):
"""returns bonus class with specified name.
If name not specified, return the configured function.
None if no bonus function configured.
If class_name not found in module bonus_sport, returns None
and flash a warning.
"""
2022-01-27 18:12:40 +01:00
if not class_name: # None or ""
c = ScoDocSiteConfig.query.filter_by(name=cls.BONUS_SPORT).first()
if c is None:
return None
class_name = c.value
if class_name == "": # pas de bonus défini
return None
klass = bonus_spo.get_bonus_class_dict().get(class_name)
if klass is None:
flash(
f"""Fonction de calcul bonus sport inexistante: {class_name}.
Changez ou contactez votre administrateur local."""
)
return klass
2022-01-21 10:27:47 +01:00
@classmethod
2022-01-27 18:12:40 +01:00
def get_bonus_sport_class_names(cls) -> list:
"""List available bonus class names
(starting with empty string to represent "no bonus function").
"""
return [""] + sorted(bonus_spo.get_bonus_class_dict().keys())
2022-01-27 18:12:40 +01:00
@classmethod
def get_bonus_sport_class_list(cls) -> list[tuple]:
"""List available bonus class names
(starting with empty string to represent "no bonus function").
"""
d = bonus_spo.get_bonus_class_dict()
2023-02-26 21:24:07 +01:00
class_list = [(name, d[name].displayed_name) for name in d]
2022-01-27 18:12:40 +01:00
class_list.sort(key=lambda x: x[1].replace(" du ", " de "))
return [("", "")] + class_list
2022-01-21 10:27:47 +01:00
@classmethod
def get_code_apo(cls, code: str) -> str:
"""La représentation d'un code pour les exports Apogée.
Par exemple, à l'IUT du H., le code ADM est réprésenté par VAL
2022-01-21 10:27:47 +01:00
Les codes par défaut sont donnés dans sco_apogee_csv.
"""
cfg = ScoDocSiteConfig.query.filter_by(name=code).first()
if not cfg:
code_apo = code_scodoc_to_apo_default(code)
else:
code_apo = cfg.value
return code_apo
@classmethod
def get_codes_apo_dict(cls) -> dict[str:str]:
"Un dict avec code jury : code exporté"
return {code: cls.get_code_apo(code) for code in CODES_SCODOC_TO_APO}
2022-01-21 10:27:47 +01:00
@classmethod
def set_code_apo(cls, code: str, code_apo: str):
"""Enregistre nouvelle représentation du code"""
if code_apo != cls.get_code_apo(code):
cfg = ScoDocSiteConfig.query.filter_by(name=code).first()
if cfg is None:
cfg = ScoDocSiteConfig(code, code_apo)
else:
cfg.value = code_apo
db.session.add(cfg)
db.session.commit()
2023-02-26 21:24:07 +01:00
@classmethod
def is_cas_enabled(cls) -> bool:
"""True si on utilise le CAS"""
cfg = ScoDocSiteConfig.query.filter_by(name="cas_enable").first()
return cfg is not None and cfg.value
@classmethod
def is_entreprises_enabled(cls) -> bool:
"""True si on doit activer le module entreprise"""
cfg = ScoDocSiteConfig.query.filter_by(name="enable_entreprises").first()
2023-02-26 21:24:07 +01:00
return cfg is not None and cfg.value
@classmethod
def enable_entreprises(cls, enabled=True) -> bool:
"""Active (ou déactive) le module entreprises. True si changement."""
if enabled != ScoDocSiteConfig.is_entreprises_enabled():
cfg = ScoDocSiteConfig.query.filter_by(name="enable_entreprises").first()
if cfg is None:
cfg = ScoDocSiteConfig(
name="enable_entreprises", value="on" if enabled else ""
)
else:
cfg.value = "on" if enabled else ""
db.session.add(cfg)
db.session.commit()
return True
return False
2023-02-26 21:24:07 +01:00
@classmethod
def get(cls, name: str) -> str:
"Get configuration param; empty string if unset"
cfg = ScoDocSiteConfig.query.filter_by(name=name).first()
return (cfg.value or "") if cfg else ""
@classmethod
def set(cls, name: str, value: str) -> bool:
"Set parameter, returns True if change. Commit session."
value_str = str(value or "")
if (cls.get(name) or "") != value_str:
2023-02-26 21:24:07 +01:00
cfg = ScoDocSiteConfig.query.filter_by(name=name).first()
if cfg is None:
cfg = ScoDocSiteConfig(name=name, value=value_str)
2023-02-26 21:24:07 +01:00
else:
cfg.value = str(value or "")
2023-02-26 23:27:40 +01:00
current_app.logger.info(
f"""ScoDocSiteConfig: recording {cfg.name}='{cfg.value[:32]}...'"""
)
2023-02-26 21:24:07 +01:00
db.session.add(cfg)
db.session.commit()
return True
return False
@classmethod
def _get_int_field(cls, name: str, default=None) -> int:
"""Valeur d'un champs integer"""
cfg = ScoDocSiteConfig.query.filter_by(name=name).first()
if (cfg is None) or cfg.value is None:
return default
return int(cfg.value)
@classmethod
def _set_int_field(
cls,
name: str,
value: int,
default=None,
range_values: tuple = (),
) -> bool:
"""Set champs integer. True si changement."""
if value != cls._get_int_field(name, default=default):
if not isinstance(value, int) or (
range_values and (value < range_values[0]) or (value > range_values[1])
):
raise ValueError("invalid value")
cfg = ScoDocSiteConfig.query.filter_by(name=name).first()
if cfg is None:
cfg = ScoDocSiteConfig(name=name, value=str(value))
else:
cfg.value = str(value)
db.session.add(cfg)
db.session.commit()
return True
return False
@classmethod
def get_month_debut_annee_scolaire(cls) -> int:
"""Mois de début de l'année scolaire."""
return cls._get_int_field(
"month_debut_annee_scolaire", scu.MONTH_DEBUT_ANNEE_SCOLAIRE
)
@classmethod
def get_month_debut_periode2(cls) -> int:
"""Mois de début de l'année scolaire."""
return cls._get_int_field("month_debut_periode2", scu.MONTH_DEBUT_PERIODE2)
@classmethod
def set_month_debut_annee_scolaire(
cls, month: int = scu.MONTH_DEBUT_ANNEE_SCOLAIRE
) -> bool:
"""Fixe le mois de début des années scolaires.
True si changement.
"""
if cls._set_int_field(
"month_debut_annee_scolaire", month, scu.MONTH_DEBUT_ANNEE_SCOLAIRE, (1, 12)
):
log(f"set_month_debut_annee_scolaire({month})")
return True
return False
@classmethod
def set_month_debut_periode2(cls, month: int = scu.MONTH_DEBUT_PERIODE2) -> bool:
"""Fixe le mois de début des années scolaires.
True si changement.
"""
if cls._set_int_field(
"month_debut_periode2", month, scu.MONTH_DEBUT_PERIODE2, (1, 12)
):
log(f"set_month_debut_periode2({month})")
return True
return False