2022-01-21 10:27:47 +01:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
|
|
|
|
"""Model : site config WORK IN PROGRESS #WIP
|
|
|
|
"""
|
|
|
|
|
2023-07-31 16:14:16 +02:00
|
|
|
import json
|
2023-09-23 09:48:05 +02:00
|
|
|
import re
|
2023-11-06 22:05:38 +01:00
|
|
|
import urllib.parse
|
2023-07-31 16:14:16 +02:00
|
|
|
|
2022-01-26 22:52:56 +01:00
|
|
|
from flask import flash
|
2024-10-29 19:18:36 +01:00
|
|
|
from app import current_app, db, log, models
|
2022-01-25 11:11:03 +01:00
|
|
|
from app.comp import bonus_spo
|
2023-07-31 16:14:16 +02:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2022-11-13 14:55:18 +01:00
|
|
|
from app.scodoc import sco_utils as scu
|
2022-01-21 10:27:47 +01:00
|
|
|
|
2023-02-12 13:36:47 +01:00
|
|
|
from app.scodoc.codes_cursus import (
|
2022-06-26 15:43:46 +02:00
|
|
|
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,
|
2023-06-25 21:05:19 +02:00
|
|
|
ADSUP,
|
2022-01-21 10:27:47 +01:00
|
|
|
AJ,
|
|
|
|
ATB,
|
|
|
|
ATJ,
|
|
|
|
ATT,
|
|
|
|
CMP,
|
|
|
|
DEF,
|
|
|
|
DEM,
|
2022-06-26 15:43:46 +02:00
|
|
|
EXCLU,
|
2022-01-21 10:27:47 +01:00
|
|
|
NAR,
|
2022-06-26 15:43:46 +02:00
|
|
|
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 = {
|
2022-06-26 15:43:46 +02:00
|
|
|
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",
|
2023-06-25 21:05:19 +02:00
|
|
|
ADSUP: "ADM",
|
2022-01-21 10:27:47 +01:00
|
|
|
AJ: "AJ",
|
|
|
|
ATB: "AJAC",
|
|
|
|
ATJ: "AJAC",
|
|
|
|
ATT: "AJAC",
|
|
|
|
CMP: "COMP",
|
|
|
|
DEF: "NAR",
|
|
|
|
DEM: "NAR",
|
2022-06-26 15:43:46 +02:00
|
|
|
EXCLU: "EXC",
|
2022-01-21 10:27:47 +01:00
|
|
|
NAR: "NAR",
|
2022-06-26 15:43:46 +02:00
|
|
|
PASD: "PASD",
|
|
|
|
PAS1NCI: "PAS1NCI",
|
2022-01-21 10:27:47 +01:00
|
|
|
RAT: "ATT",
|
2022-06-30 15:32:22 +02:00
|
|
|
RED: "RED",
|
2022-04-05 11:44:08 +02:00
|
|
|
"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")
|
|
|
|
|
|
|
|
|
2024-10-29 19:18:36 +01:00
|
|
|
class ScoDocSiteConfig(models.ScoDocModel):
|
2022-01-21 10:27:47 +01:00
|
|
|
"""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,
|
2022-03-22 22:14:45 +01:00
|
|
|
"enable_entreprises": bool,
|
2024-03-27 16:27:45 +01:00
|
|
|
"disable_passerelle": bool, # remplace pref. bul_display_publication
|
2022-11-13 14:55:18 +01:00
|
|
|
"month_debut_annee_scolaire": int,
|
|
|
|
"month_debut_periode2": int,
|
2023-09-10 21:16:31 +02:00
|
|
|
"disable_bul_pdf": bool,
|
2024-01-09 19:56:05 +01:00
|
|
|
"user_require_email_institutionnel": bool,
|
2023-02-26 21:24:07 +01:00
|
|
|
# CAS
|
|
|
|
"cas_enable": bool,
|
|
|
|
"cas_server": str,
|
2023-03-02 23:29:25 +01:00
|
|
|
"cas_login_route": str,
|
|
|
|
"cas_logout_route": str,
|
2023-03-09 11:59:58 +01:00
|
|
|
"cas_validate_route": str,
|
2023-02-26 21:24:07 +01:00
|
|
|
"cas_attribute_id": str,
|
2023-09-23 09:48:05 +02:00
|
|
|
"cas_uid_from_mail_regexp": str,
|
2024-09-15 16:50:25 +02:00
|
|
|
"cas_uid_use_scodoc": bool,
|
2023-11-06 22:05:38 +01:00
|
|
|
"cas_edt_id_from_xml_regexp": str,
|
2023-09-21 08:45:46 +02:00
|
|
|
# Assiduité
|
2023-04-25 22:59:06 +02:00
|
|
|
"morning_time": str,
|
|
|
|
"lunch_time": str,
|
|
|
|
"afternoon_time": 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
|
2022-01-25 11:11:03 +01:00
|
|
|
def set_bonus_sport_class(cls, class_name):
|
2022-01-21 10:27:47 +01:00
|
|
|
"""Record bonus_sport config.
|
2022-01-25 11:11:03 +01:00
|
|
|
If class_name not defined, raise NameError
|
2022-01-21 10:27:47 +01:00
|
|
|
"""
|
2022-01-25 11:11:03 +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:
|
2022-01-25 11:11:03 +01:00
|
|
|
log("setting to " + class_name)
|
|
|
|
c.value = class_name
|
2022-01-21 10:27:47 +01:00
|
|
|
else:
|
2022-01-25 11:11:03 +01:00
|
|
|
c = ScoDocSiteConfig(cls.BONUS_SPORT, class_name)
|
2022-01-21 10:27:47 +01:00
|
|
|
db.session.add(c)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@classmethod
|
2022-01-25 11:11:03 +01:00
|
|
|
def get_bonus_sport_class_name(cls):
|
2022-01-21 10:27:47 +01:00
|
|
|
"""Get configured bonus function name, or None if None."""
|
2022-01-25 11:11:03 +01:00
|
|
|
klass = cls.get_bonus_sport_class_from_name()
|
|
|
|
if klass is None:
|
2022-01-21 10:27:47 +01:00
|
|
|
return ""
|
|
|
|
else:
|
2022-01-25 11:11:03 +01:00
|
|
|
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
|
2022-01-25 11:11:03 +01:00
|
|
|
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.
|
2022-01-26 22:52:56 +01:00
|
|
|
If class_name not found in module bonus_sport, returns None
|
|
|
|
and flash a warning.
|
2022-01-25 11:11:03 +01:00
|
|
|
"""
|
2022-01-27 18:12:40 +01:00
|
|
|
if not class_name: # None or ""
|
2022-01-25 11:11:03 +01:00
|
|
|
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:
|
2022-01-26 22:52:56 +01:00
|
|
|
flash(
|
2023-11-06 22:05:38 +01:00
|
|
|
f"""Fonction de calcul bonus sport inexistante: {class_name}.
|
2022-01-26 22:52:56 +01:00
|
|
|
Changez là ou contactez votre administrateur local."""
|
2022-01-25 11:11:03 +01:00
|
|
|
)
|
|
|
|
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:
|
2022-01-25 11:11:03 +01:00
|
|
|
"""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.
|
2022-06-26 15:43:46 +02:00
|
|
|
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
|
|
|
|
|
2022-06-26 15:43:46 +02:00
|
|
|
@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()
|
2022-03-22 22:14:45 +01:00
|
|
|
|
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
|
|
|
|
|
2024-01-05 23:55:05 +01:00
|
|
|
@classmethod
|
|
|
|
def is_cas_forced(cls) -> bool:
|
|
|
|
"""True si CAS forcé"""
|
|
|
|
cfg = ScoDocSiteConfig.query.filter_by(name="cas_force").first()
|
|
|
|
return cfg is not None and cfg.value
|
|
|
|
|
2024-09-15 16:50:25 +02:00
|
|
|
@classmethod
|
|
|
|
def cas_uid_use_scodoc(cls) -> bool:
|
|
|
|
"""True si cas_uid_use_scodoc"""
|
|
|
|
cfg = ScoDocSiteConfig.query.filter_by(name="cas_uid_use_scodoc").first()
|
|
|
|
return cfg is not None and cfg.value
|
|
|
|
|
2022-03-22 22:14:45 +01:00
|
|
|
@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
|
2022-03-22 22:14:45 +01:00
|
|
|
|
2024-03-27 16:27:45 +01:00
|
|
|
@classmethod
|
|
|
|
def is_passerelle_disabled(cls):
|
|
|
|
"""True si on doit cacher les fonctions passerelle ("oeil")."""
|
|
|
|
cfg = ScoDocSiteConfig.query.filter_by(name="disable_passerelle").first()
|
|
|
|
return cfg is not None and cfg.value
|
|
|
|
|
2024-01-09 19:56:05 +01:00
|
|
|
@classmethod
|
|
|
|
def is_user_require_email_institutionnel_enabled(cls) -> bool:
|
|
|
|
"""True si impose saisie email_institutionnel"""
|
|
|
|
cfg = ScoDocSiteConfig.query.filter_by(
|
|
|
|
name="user_require_email_institutionnel"
|
|
|
|
).first()
|
|
|
|
return cfg is not None and cfg.value
|
|
|
|
|
2023-09-10 21:16:31 +02:00
|
|
|
@classmethod
|
|
|
|
def is_bul_pdf_disabled(cls) -> bool:
|
2024-07-15 19:08:15 +02:00
|
|
|
"""True si on interdit les exports PDF des bulletins"""
|
2023-09-10 21:16:31 +02:00
|
|
|
cfg = ScoDocSiteConfig.query.filter_by(name="disable_bul_pdf").first()
|
|
|
|
return cfg is not None and cfg.value
|
|
|
|
|
2022-03-22 22:14:45 +01:00
|
|
|
@classmethod
|
2024-01-09 19:56:05 +01:00
|
|
|
def enable_entreprises(cls, enabled: bool = True) -> bool:
|
2022-03-22 22:14:45 +01:00
|
|
|
"""Active (ou déactive) le module entreprises. True si changement."""
|
2024-01-09 19:56:05 +01:00
|
|
|
return cls.set("enable_entreprises", "on" if enabled else "")
|
2023-09-10 21:16:31 +02:00
|
|
|
|
2024-03-27 16:27:45 +01:00
|
|
|
@classmethod
|
|
|
|
def disable_passerelle(cls, disabled: bool = True) -> bool:
|
|
|
|
"""Désactive (ou active) les fonctions liées à la présence d'une passerelle. True si changement."""
|
|
|
|
return cls.set("disable_passerelle", "on" if disabled else "")
|
|
|
|
|
2023-09-10 21:16:31 +02:00
|
|
|
@classmethod
|
|
|
|
def disable_bul_pdf(cls, enabled=True) -> bool:
|
2024-01-09 19:56:05 +01:00
|
|
|
"""Interdit (ou autorise) les exports PDF. True si changement."""
|
|
|
|
return cls.set("disable_bul_pdf", "on" if enabled else "")
|
2022-11-13 14:55:18 +01:00
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
@classmethod
|
2023-03-02 23:29:25 +01:00
|
|
|
def get(cls, name: str, default: str = "") -> str:
|
|
|
|
"Get configuration param; empty string or specified default if unset"
|
2023-02-26 21:24:07 +01:00
|
|
|
cfg = ScoDocSiteConfig.query.filter_by(name=name).first()
|
2023-03-02 23:29:25 +01:00
|
|
|
if cfg is None:
|
|
|
|
return default
|
2023-07-30 21:48:51 +02:00
|
|
|
return cls.NAMES.get(name, lambda x: x)(cfg.value or "")
|
2023-02-26 21:24:07 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set(cls, name: str, value: str) -> bool:
|
|
|
|
"Set parameter, returns True if change. Commit session."
|
2023-11-08 17:58:11 +01:00
|
|
|
value_str = str(value or "").strip()
|
2023-03-01 19:10:37 +01:00
|
|
|
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:
|
2023-03-01 19:10:37 +01:00
|
|
|
cfg = ScoDocSiteConfig(name=name, value=value_str)
|
2023-02-26 21:24:07 +01:00
|
|
|
else:
|
2024-01-09 19:56:05 +01:00
|
|
|
cfg.value = value_str
|
2023-02-26 23:27:40 +01:00
|
|
|
current_app.logger.info(
|
2024-01-09 19:56:05 +01:00
|
|
|
f"""ScoDocSiteConfig: recording {cfg.name}='{cfg.value[:32]}{
|
|
|
|
'...' if len(cfg.value)>32 else ''}'"""
|
2023-02-26 23:27:40 +01:00
|
|
|
)
|
2023-02-26 21:24:07 +01:00
|
|
|
db.session.add(cfg)
|
|
|
|
db.session.commit()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-11-13 14:55:18 +01:00
|
|
|
@classmethod
|
|
|
|
def _get_int_field(cls, name: str, default=None) -> int:
|
2024-02-12 10:12:46 +01:00
|
|
|
"""Valeur d'un champ integer"""
|
2022-11-13 14:55:18 +01:00
|
|
|
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:
|
2024-02-12 10:12:46 +01:00
|
|
|
"""Set champ integer. True si changement."""
|
2022-11-13 14:55:18 +01:00
|
|
|
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
|
2023-07-31 16:14:16 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_perso_links(cls) -> list["PersonalizedLink"]:
|
|
|
|
"Return links"
|
|
|
|
data_links = cls.get("personalized_links")
|
|
|
|
if not data_links:
|
|
|
|
return []
|
|
|
|
try:
|
|
|
|
links_dict = json.loads(data_links)
|
|
|
|
except json.decoder.JSONDecodeError as exc:
|
|
|
|
# Corrupted data ? erase content
|
|
|
|
cls.set("personalized_links", "")
|
|
|
|
raise ScoValueError(
|
|
|
|
"Attention: liens personnalisés erronés: ils ont été effacés."
|
2023-09-20 22:38:01 +02:00
|
|
|
) from exc
|
2023-07-31 16:14:16 +02:00
|
|
|
return [PersonalizedLink(**item) for item in links_dict]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_perso_links(cls, links: list["PersonalizedLink"] = None):
|
|
|
|
"Store all links"
|
|
|
|
if not links:
|
|
|
|
links = []
|
|
|
|
links_dict = [link.to_dict() for link in links]
|
|
|
|
data_links = json.dumps(links_dict)
|
|
|
|
cls.set("personalized_links", data_links)
|
|
|
|
|
2023-09-23 09:48:05 +02:00
|
|
|
@classmethod
|
|
|
|
def extract_cas_id(cls, email_addr: str) -> str | None:
|
2024-09-15 16:50:25 +02:00
|
|
|
"Extract cas_id from mail, using regexp in config. None if not possible."
|
2023-09-23 09:48:05 +02:00
|
|
|
exp = cls.get("cas_uid_from_mail_regexp")
|
|
|
|
if not exp or not email_addr:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
match = re.search(exp, email_addr)
|
|
|
|
except re.error:
|
|
|
|
log("error extracting CAS id from '{email_addr}' using regexp '{exp}'")
|
|
|
|
return None
|
|
|
|
if not match:
|
|
|
|
log("no match extracting CAS id from '{email_addr}' using regexp '{exp}'")
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
cas_id = match.group(1)
|
|
|
|
except IndexError:
|
|
|
|
log(
|
|
|
|
"no group found extracting CAS id from '{email_addr}' using regexp '{exp}'"
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
return cas_id
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def cas_uid_from_mail_regexp_is_valid(cls, exp: str) -> bool:
|
|
|
|
"True si l'expression régulière semble valide"
|
|
|
|
# check that it compiles
|
|
|
|
try:
|
|
|
|
pattern = re.compile(exp)
|
|
|
|
except re.error:
|
|
|
|
return False
|
|
|
|
# and returns at least one group on a simple cannonical address
|
|
|
|
match = pattern.search("emmanuel@exemple.fr")
|
2023-11-08 17:58:11 +01:00
|
|
|
return match is not None and len(match.groups()) > 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def cas_edt_id_from_xml_regexp_is_valid(cls, exp: str) -> bool:
|
|
|
|
"True si l'expression régulière semble valide"
|
|
|
|
# check that it compiles
|
|
|
|
try:
|
|
|
|
_ = re.compile(exp)
|
|
|
|
except re.error:
|
|
|
|
return False
|
|
|
|
return True
|
2023-09-23 09:48:05 +02:00
|
|
|
|
2023-09-20 22:38:01 +02:00
|
|
|
@classmethod
|
|
|
|
def assi_get_rounded_time(cls, label: str, default: str) -> float:
|
|
|
|
"Donne l'heure stockée dans la config globale sous label, en float arrondi au quart d'heure"
|
2023-09-21 08:45:46 +02:00
|
|
|
return _round_time_str_to_quarter(cls.get(label, default))
|
2023-09-20 22:38:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _round_time_str_to_quarter(string: str) -> float:
|
|
|
|
"""Prend une heure iso '12:20:23', et la converti en un nombre d'heures
|
|
|
|
en arrondissant au quart d'heure: (les secondes sont ignorées)
|
|
|
|
"12:20:00" -> 12.25
|
|
|
|
"12:29:00" -> 12.25
|
|
|
|
"12:30:00" -> 12.5
|
|
|
|
"""
|
|
|
|
parts = [*map(float, string.split(":"))]
|
|
|
|
hour = parts[0]
|
|
|
|
minutes = round(parts[1] / 60 * 4) / 4
|
|
|
|
return hour + minutes
|
|
|
|
|
2023-07-31 16:14:16 +02:00
|
|
|
|
|
|
|
class PersonalizedLink:
|
|
|
|
def __init__(self, title: str = "", url: str = "", with_args: bool = False):
|
|
|
|
self.title = str(title or "")
|
|
|
|
self.url = str(url or "")
|
|
|
|
self.with_args = bool(with_args)
|
|
|
|
|
|
|
|
def get_url(self, params: dict = {}) -> str:
|
|
|
|
if not self.with_args:
|
|
|
|
return self.url
|
|
|
|
query_string = urllib.parse.urlencode(params)
|
|
|
|
if "?" in self.url:
|
|
|
|
return self.url + "&" + query_string
|
|
|
|
return self.url + "?" + query_string
|
|
|
|
|
|
|
|
def to_dict(self) -> dict:
|
|
|
|
"as dict"
|
|
|
|
return {"title": self.title, "url": self.url, "with_args": self.with_args}
|