2020-09-26 16:19:37 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Gestion scolarite IUT
|
|
|
|
#
|
2023-12-31 23:04:06 +01:00
|
|
|
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
|
2020-09-26 16:19:37 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
# Emmanuel Viennet emmanuel.viennet@viennet.net
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""Accès aux emplois du temps
|
|
|
|
|
2023-12-29 13:58:18 +01:00
|
|
|
Lecture et conversion des ics.
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
"""
|
2024-05-30 10:51:24 +02:00
|
|
|
|
2023-11-13 23:43:54 +01:00
|
|
|
from datetime import timezone
|
2024-01-02 23:05:08 +01:00
|
|
|
import glob
|
|
|
|
import os
|
2023-11-11 18:13:18 +01:00
|
|
|
import re
|
2023-12-29 13:58:18 +01:00
|
|
|
import time
|
2020-09-26 16:19:37 +02:00
|
|
|
import icalendar
|
2023-11-06 22:05:38 +01:00
|
|
|
|
2023-11-19 22:35:04 +01:00
|
|
|
from flask import g, url_for
|
2021-08-29 19:57:32 +02:00
|
|
|
from app import log
|
2023-12-28 23:05:19 +01:00
|
|
|
from app.auth.models import User
|
2023-11-11 18:13:18 +01:00
|
|
|
from app.models import FormSemestre, GroupDescr, ModuleImpl, ScoDocSiteConfig
|
2024-10-23 22:45:34 +02:00
|
|
|
from app.scodoc.html_sidebar import retreive_formsemestre_from_request
|
2023-11-13 15:08:09 +01:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2023-11-11 18:13:18 +01:00
|
|
|
import app.scodoc.sco_utils as scu
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
2023-11-15 00:17:47 +01:00
|
|
|
def get_ics_filename(edt_id: str) -> str | None:
|
2023-11-14 15:55:52 +01:00
|
|
|
"Le chemin vers l'ics de cet edt_id"
|
|
|
|
edt_ics_path = ScoDocSiteConfig.get("edt_ics_path")
|
|
|
|
if not edt_ics_path.strip():
|
|
|
|
return None
|
|
|
|
return edt_ics_path.format(edt_id=edt_id)
|
|
|
|
|
|
|
|
|
2024-01-02 23:05:08 +01:00
|
|
|
def get_ics_directory() -> str | None:
|
|
|
|
"Le répertoire contenant les ics: prend le parent de edt_ics_path"
|
|
|
|
edt_ics_path = ScoDocSiteConfig.get("edt_ics_path")
|
|
|
|
if not edt_ics_path.strip():
|
|
|
|
return None
|
|
|
|
return os.path.split(edt_ics_path)[0]
|
|
|
|
|
|
|
|
|
|
|
|
def get_ics_user_edt_filename(edt_id) -> str | None:
|
|
|
|
"""Le chemin vers le fichier ics de l'emploi du temps de l'utilisateur,
|
|
|
|
ou None.
|
|
|
|
edt_id est l'edt_id de l'utilisateur
|
|
|
|
"""
|
|
|
|
if not edt_id:
|
|
|
|
return None
|
|
|
|
edt_ics_user_path = ScoDocSiteConfig.get("edt_ics_user_path")
|
|
|
|
if not edt_ics_user_path.strip():
|
|
|
|
return None
|
|
|
|
return edt_ics_user_path.format(edt_id=edt_id)
|
|
|
|
|
|
|
|
|
|
|
|
def list_edt_calendars() -> list[str]:
|
|
|
|
"""Liste des chemins complets vers tous les ics des calendriers de semestres"""
|
|
|
|
path = get_ics_directory()
|
|
|
|
return glob.glob(path + "/*.ics") if path else []
|
|
|
|
|
|
|
|
|
2023-12-29 04:11:37 +01:00
|
|
|
def is_edt_configured() -> bool:
|
|
|
|
"True si accès EDT configuré"
|
|
|
|
return bool(ScoDocSiteConfig.get("edt_ics_path"))
|
|
|
|
|
|
|
|
|
2023-11-11 18:13:18 +01:00
|
|
|
def formsemestre_load_calendar(
|
2023-11-14 15:55:52 +01:00
|
|
|
formsemestre: FormSemestre = None, edt_id: str = None
|
|
|
|
) -> tuple[bytes, icalendar.cal.Calendar]:
|
|
|
|
"""Load ics data, return raw ics and decoded calendar.
|
|
|
|
Raises ScoValueError if not configured or not available or invalid format.
|
|
|
|
"""
|
2023-11-21 11:34:52 +01:00
|
|
|
edt_ids = []
|
2023-12-28 23:05:19 +01:00
|
|
|
if edt_id is None:
|
|
|
|
if formsemestre:
|
|
|
|
edt_ids = formsemestre.get_edt_ids()
|
|
|
|
else:
|
|
|
|
edt_ids = [edt_id]
|
2023-11-19 22:35:04 +01:00
|
|
|
if not edt_ids:
|
2023-11-14 15:55:52 +01:00
|
|
|
raise ScoValueError(
|
2023-11-11 18:13:18 +01:00
|
|
|
"accès aux emplois du temps non configuré pour ce semestre (pas d'edt_id)"
|
|
|
|
)
|
2023-11-19 22:35:04 +01:00
|
|
|
# Ne charge qu'un seul ics pour le semestre, prend uniquement
|
|
|
|
# le premier edt_id
|
|
|
|
ics_filename = get_ics_filename(edt_ids[0])
|
2024-01-02 23:05:08 +01:00
|
|
|
return load_calendar(ics_filename, formsemestre=formsemestre, edt_id=edt_id)
|
|
|
|
|
|
|
|
|
|
|
|
def load_calendar(
|
|
|
|
ics_filename: str, formsemestre: FormSemestre | None = None, edt_id: str = None
|
|
|
|
) -> tuple[bytes, icalendar.cal.Calendar]:
|
|
|
|
"""Load ics from given (full) path. Return raw ics and decoded calendar.
|
|
|
|
formsemestre and edt_id are optionaly used for error messages.
|
|
|
|
May raise ScoValueError.
|
|
|
|
"""
|
2023-11-15 00:17:47 +01:00
|
|
|
if ics_filename is None:
|
|
|
|
raise ScoValueError("accès aux emplois du temps non configuré (pas de chemin)")
|
2020-09-26 16:19:37 +02:00
|
|
|
try:
|
2023-11-11 18:13:18 +01:00
|
|
|
with open(ics_filename, "rb") as file:
|
|
|
|
log(f"Loading edt from {ics_filename}")
|
2023-11-14 15:55:52 +01:00
|
|
|
data = file.read()
|
2023-11-14 14:06:47 +01:00
|
|
|
try:
|
2023-11-14 15:55:52 +01:00
|
|
|
calendar = icalendar.Calendar.from_ical(data)
|
2023-11-14 14:06:47 +01:00
|
|
|
except ValueError as exc:
|
|
|
|
log(
|
|
|
|
f"""formsemestre_load_calendar: error importing ics for {
|
2023-11-14 15:55:52 +01:00
|
|
|
formsemestre or ''}\npath='{ics_filename}'"""
|
2023-11-14 14:06:47 +01:00
|
|
|
)
|
|
|
|
raise ScoValueError(
|
|
|
|
f"calendrier ics illisible (edt_id={edt_id})"
|
|
|
|
) from exc
|
|
|
|
except FileNotFoundError as exc:
|
2020-09-26 16:19:37 +02:00
|
|
|
log(
|
2024-01-03 22:11:49 +01:00
|
|
|
f"""formsemestre_load_calendar: ics not found for {
|
|
|
|
formsemestre or ''}\npath='{ics_filename}'"""
|
2020-09-26 16:19:37 +02:00
|
|
|
)
|
2023-11-14 14:06:47 +01:00
|
|
|
raise ScoValueError(
|
|
|
|
f"Fichier ics introuvable (filename={ics_filename})"
|
|
|
|
) from exc
|
|
|
|
except PermissionError as exc:
|
|
|
|
log(
|
2023-11-14 15:55:52 +01:00
|
|
|
f"""formsemestre_load_calendar: permission denied for {formsemestre or ''
|
2023-11-14 14:06:47 +01:00
|
|
|
}\npath='{ics_filename}'"""
|
|
|
|
)
|
|
|
|
raise ScoValueError(
|
|
|
|
f"Fichier ics inaccessible: vérifier permissions (filename={ics_filename})"
|
|
|
|
) from exc
|
2023-11-11 18:13:18 +01:00
|
|
|
|
2023-11-14 15:55:52 +01:00
|
|
|
return data, calendar
|
2023-11-11 18:13:18 +01:00
|
|
|
|
|
|
|
|
2023-11-13 15:08:09 +01:00
|
|
|
# --- Couleurs des évènements emploi du temps
|
2023-11-11 18:13:18 +01:00
|
|
|
_COLOR_PALETTE = [
|
|
|
|
"#ff6961",
|
|
|
|
"#ffb480",
|
|
|
|
"#f8f38d",
|
|
|
|
"#42d6a4",
|
|
|
|
"#08cad1",
|
|
|
|
"#59adf6",
|
|
|
|
"#9d94ff",
|
|
|
|
"#c780e8",
|
|
|
|
]
|
2023-11-13 15:08:09 +01:00
|
|
|
_EVENT_DEFAULT_COLOR = "rgb(214, 233, 248)"
|
2023-11-11 18:13:18 +01:00
|
|
|
|
|
|
|
|
2023-11-16 23:34:47 +01:00
|
|
|
def formsemestre_edt_dict(
|
2023-12-24 16:09:07 +01:00
|
|
|
formsemestre: FormSemestre,
|
|
|
|
group_ids: list[int] = None,
|
|
|
|
show_modules_titles=True,
|
2023-11-16 23:34:47 +01:00
|
|
|
) -> list[dict]:
|
2023-11-11 18:13:18 +01:00
|
|
|
"""EDT complet du semestre, comme une liste de dict serialisable en json.
|
2023-11-16 23:34:47 +01:00
|
|
|
Fonction appelée par l'API /formsemestre/<int:formsemestre_id>/edt
|
|
|
|
group_ids indiquer les groupes ScoDoc à afficher (les autres sont filtrés).
|
|
|
|
Les évènements pour lesquels le groupe ScoDoc n'est pas reconnu sont
|
|
|
|
toujours présents.
|
2023-11-11 18:13:18 +01:00
|
|
|
TODO: spécifier intervalle de dates start et end
|
2020-09-26 16:19:37 +02:00
|
|
|
"""
|
2023-12-29 13:58:18 +01:00
|
|
|
t0 = time.time()
|
2023-11-14 14:06:47 +01:00
|
|
|
try:
|
2023-12-24 16:09:07 +01:00
|
|
|
events_scodoc, _ = load_and_convert_ics(formsemestre)
|
2023-11-14 14:06:47 +01:00
|
|
|
except ScoValueError as exc:
|
|
|
|
return exc.args[0]
|
2024-01-17 21:58:45 +01:00
|
|
|
edt_dict = translate_calendar(
|
|
|
|
events_scodoc, group_ids, show_modules_titles=show_modules_titles
|
|
|
|
)
|
|
|
|
log(
|
|
|
|
f"formsemestre_edt_dict: loaded edt for {formsemestre} in {(time.time()-t0):g}s"
|
|
|
|
)
|
|
|
|
return edt_dict
|
|
|
|
|
|
|
|
|
|
|
|
def translate_calendar(
|
|
|
|
events_scodoc: list[dict],
|
|
|
|
group_ids: list[int] = None,
|
|
|
|
show_modules_titles=True,
|
|
|
|
) -> list[dict]:
|
|
|
|
"""Génération des événements pour le calendrier html"""
|
|
|
|
group_ids_set = set(group_ids) if group_ids else set()
|
2024-01-06 12:46:22 +01:00
|
|
|
promo_icon = f"""<img height="18px" src="{scu.STATIC_DIR}/icons/promo.svg"
|
2023-12-24 16:09:07 +01:00
|
|
|
title="promotion complète" alt="promotion"/>"""
|
2023-12-28 23:05:19 +01:00
|
|
|
abs_icon = f"""<img height="28px" src="{scu.STATIC_DIR}/icons/absences.svg"
|
|
|
|
title="saisir absences" alt="saisir absences"/>"""
|
2023-11-13 15:08:09 +01:00
|
|
|
events_cal = []
|
|
|
|
for event in events_scodoc:
|
|
|
|
group: GroupDescr | bool = event["group"]
|
|
|
|
if group is False:
|
|
|
|
group_disp = f"""<div class="group-edt">
|
|
|
|
<span title="extraction emploi du temps non configurée">
|
|
|
|
{scu.EMO_WARNING} non configuré</span>
|
|
|
|
</div>"""
|
|
|
|
else:
|
2023-11-11 18:13:18 +01:00
|
|
|
group_disp = (
|
2023-12-24 16:09:07 +01:00
|
|
|
f"""<div class="group-name">{group.get_nom_with_part(default=promo_icon)}</div>"""
|
2023-11-11 18:13:18 +01:00
|
|
|
if group
|
2023-11-13 15:08:09 +01:00
|
|
|
else f"""<div class="group-edt">{event['edt_group']}
|
|
|
|
<span title="vérifier noms de groupe ou configuration extraction edt">
|
|
|
|
{scu.EMO_WARNING} non reconnu</span>
|
|
|
|
</div>"""
|
2023-11-11 18:13:18 +01:00
|
|
|
)
|
2023-11-16 23:34:47 +01:00
|
|
|
if group and group_ids_set and group.id not in group_ids_set:
|
|
|
|
continue # ignore cet évènement
|
2023-11-13 15:08:09 +01:00
|
|
|
modimpl: ModuleImpl | bool = event["modimpl"]
|
2024-10-23 22:45:34 +02:00
|
|
|
params = {
|
|
|
|
"scodoc_dept": g.scodoc_dept,
|
|
|
|
"group_ids": group.id,
|
|
|
|
"heure_deb": event["heure_deb"].replace("h", ":") if "h" in event["heure_deb"] else event["heure_deb"],
|
|
|
|
"heure_fin": event["heure_fin"].replace("h", ":") if "h" in event["heure_fin"] else event["heure_fin"],
|
|
|
|
"day": event["jour"]
|
|
|
|
}
|
|
|
|
if modimpl:
|
|
|
|
params["moduleimpl_id"] = modimpl.id
|
|
|
|
else:
|
|
|
|
params["formsemestre_id"] = retreive_formsemestre_from_request()
|
|
|
|
|
|
|
|
# Génération de l'URL si group et l'un des deux (modimpl ou formsem) sont définis
|
|
|
|
url_abs = (
|
|
|
|
url_for("assiduites.signal_assiduites_group", **params)
|
|
|
|
if group
|
|
|
|
else None
|
2023-11-19 22:35:04 +01:00
|
|
|
)
|
|
|
|
match modimpl:
|
|
|
|
case False: # EDT non configuré
|
|
|
|
mod_disp = f"""<span>{scu.EMO_WARNING} non configuré</span>"""
|
|
|
|
bubble = "extraction emploi du temps non configurée"
|
|
|
|
case None: # Module edt non trouvé dans ScoDoc
|
|
|
|
mod_disp = f"""<span class="mod-etd">{
|
|
|
|
scu.EMO_WARNING} {event['edt_module']}</span>"""
|
2024-01-06 12:46:22 +01:00
|
|
|
bubble = "code module non trouvé dans ce semestre ScoDoc. Vérifier configuration."
|
2023-11-19 22:35:04 +01:00
|
|
|
case _: # module EDT bien retrouvé dans ScoDoc
|
2024-01-03 22:11:49 +01:00
|
|
|
bubble = f"""{modimpl.module.abbrev or modimpl.module.titre or ''
|
|
|
|
} ({event['edt_module']})"""
|
2023-12-24 16:09:07 +01:00
|
|
|
mod_disp = (
|
|
|
|
f"""<span class="mod-name mod-code">{modimpl.module.code}</span>"""
|
|
|
|
)
|
|
|
|
span_title = f" <span>{event['title']}</span>" if show_modules_titles else ""
|
|
|
|
title = f"""<div class = "module-edt" title="{bubble}">
|
|
|
|
<a class="discretelink" href="{url_abs or ''}">{mod_disp}{span_title}</a>
|
2023-11-19 22:35:04 +01:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
2023-11-13 15:08:09 +01:00
|
|
|
# --- Lien saisie abs
|
|
|
|
link_abs = (
|
|
|
|
f"""<div class="module-edt link-abs"><a class="stdlink" href="{
|
2023-12-28 23:05:19 +01:00
|
|
|
url_abs}">{abs_icon}</a>
|
2023-11-12 21:45:06 +01:00
|
|
|
</div>"""
|
2023-11-19 22:35:04 +01:00
|
|
|
if url_abs
|
2023-11-13 15:08:09 +01:00
|
|
|
else ""
|
|
|
|
)
|
2023-12-28 23:05:19 +01:00
|
|
|
|
2024-01-06 12:46:22 +01:00
|
|
|
if event["users"]:
|
|
|
|
# enseignants reconnus dans l'evènement EDT
|
|
|
|
ens_nomprenoms = f"""<span class="edt-ens">({
|
|
|
|
", ".join([u.get_nomprenom() for u in event["users"]])
|
|
|
|
})</span>"""
|
|
|
|
else:
|
|
|
|
ens_nomprenoms = f"""<span class="ens-non-reconnu"
|
|
|
|
title="enseignants edt: {', '.join(event['edt_ens_ids'])
|
|
|
|
if event["edt_ens_ids"] else '?'
|
|
|
|
}">(ens. ?)</span>"""
|
|
|
|
|
2024-01-03 22:11:49 +01:00
|
|
|
ens_user_names = (
|
|
|
|
",".join([u.user_name for u in event["users"]]) if event["users"] else ""
|
|
|
|
)
|
2023-11-13 15:08:09 +01:00
|
|
|
d = {
|
|
|
|
# Champs utilisés par tui.calendar
|
|
|
|
"calendarId": "cal1",
|
2024-01-03 22:11:49 +01:00
|
|
|
"title": f"""{title} {group_disp} {ens_nomprenoms} {link_abs}""",
|
2023-11-13 15:08:09 +01:00
|
|
|
"start": event["start"],
|
|
|
|
"end": event["end"],
|
|
|
|
"backgroundColor": event["group_bg_color"],
|
2024-01-17 21:58:45 +01:00
|
|
|
"raw": event["raw"],
|
2023-11-13 15:08:09 +01:00
|
|
|
# Infos brutes pour usage API éventuel
|
2024-01-03 22:11:49 +01:00
|
|
|
"edt_ens_ids": event["edt_ens_ids"],
|
|
|
|
"ens_user_names": ens_user_names,
|
2023-11-13 15:08:09 +01:00
|
|
|
"group_id": group.id if group else None,
|
|
|
|
"group_edt_id": event["edt_group"],
|
|
|
|
"moduleimpl_id": modimpl.id if modimpl else None,
|
2024-01-17 21:58:45 +01:00
|
|
|
"UID": event["UID"], # icalendar event UID
|
2023-11-13 15:08:09 +01:00
|
|
|
}
|
|
|
|
events_cal.append(d)
|
2024-01-17 21:58:45 +01:00
|
|
|
|
2023-11-13 15:08:09 +01:00
|
|
|
return events_cal
|
|
|
|
|
|
|
|
|
2024-01-02 23:05:08 +01:00
|
|
|
def get_ics_uid_pattern() -> re.Pattern:
|
|
|
|
"""L'expression régulière compilée pour extraire l'identifiant de l'enseignant.
|
|
|
|
May raise ScoValueError.
|
|
|
|
"""
|
|
|
|
edt_ics_uid_regexp = ScoDocSiteConfig.get("edt_ics_uid_regexp")
|
|
|
|
try:
|
|
|
|
edt_ics_uid_pattern = (
|
|
|
|
re.compile(edt_ics_uid_regexp) if edt_ics_uid_regexp else None
|
|
|
|
)
|
|
|
|
except re.error as exc:
|
|
|
|
raise ScoValueError(
|
|
|
|
"expression d'extraction de l'enseignant depuis l'emploi du temps invalide"
|
|
|
|
) from exc
|
|
|
|
return edt_ics_uid_pattern
|
|
|
|
|
|
|
|
|
2023-12-24 16:09:07 +01:00
|
|
|
def load_and_convert_ics(formsemestre: FormSemestre) -> tuple[list[dict], list[str]]:
|
2024-01-17 21:58:45 +01:00
|
|
|
"""Chargement fichier ics.
|
|
|
|
Renvoie une liste d'évènements, et la liste des identifiants de groupes
|
|
|
|
trouvés (utilisée pour l'aide).
|
|
|
|
"""
|
|
|
|
# Chargement du calendier ics
|
|
|
|
_, calendar = formsemestre_load_calendar(formsemestre)
|
|
|
|
if not calendar:
|
|
|
|
return [], []
|
|
|
|
# --- Correspondances id edt -> id scodoc pour groupes
|
|
|
|
edt2group = formsemestre_retreive_groups_from_edt_id(formsemestre)
|
|
|
|
default_group = formsemestre.get_default_group()
|
|
|
|
# --- Correspondances id edt -> id scodoc pour modimpls
|
|
|
|
edt2modimpl = formsemestre_retreive_modimpls_from_edt_id(formsemestre)
|
|
|
|
return convert_ics(
|
|
|
|
calendar,
|
|
|
|
edt2group=edt2group,
|
|
|
|
default_group=default_group,
|
|
|
|
edt2modimpl=edt2modimpl,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def convert_ics(
|
|
|
|
calendar: icalendar.cal.Calendar,
|
|
|
|
edt2group: dict[str, GroupDescr] = None,
|
|
|
|
default_group: GroupDescr = None,
|
|
|
|
edt2modimpl: dict[str, ModuleImpl] = None,
|
|
|
|
) -> tuple[list[dict], list[str]]:
|
|
|
|
"""Filtrage et extraction des identifiants des évènements calendrier.
|
|
|
|
|
2023-12-24 16:09:07 +01:00
|
|
|
Renvoie une liste d'évènements, et la liste des identifiants de groupes
|
|
|
|
trouvés (utilisée pour l'aide).
|
|
|
|
|
|
|
|
Groupes:
|
|
|
|
- False si extraction regexp non configuré
|
|
|
|
- "tous" (promo) si pas de correspondance trouvée.
|
|
|
|
"""
|
2023-11-13 15:08:09 +01:00
|
|
|
# --- Paramètres d'extraction
|
|
|
|
edt_ics_title_field = ScoDocSiteConfig.get("edt_ics_title_field")
|
|
|
|
edt_ics_title_regexp = ScoDocSiteConfig.get("edt_ics_title_regexp")
|
|
|
|
try:
|
|
|
|
edt_ics_title_pattern = (
|
|
|
|
re.compile(edt_ics_title_regexp) if edt_ics_title_regexp else None
|
|
|
|
)
|
|
|
|
except re.error as exc:
|
|
|
|
raise ScoValueError(
|
|
|
|
"expression d'extraction du titre depuis l'emploi du temps invalide"
|
|
|
|
) from exc
|
|
|
|
edt_ics_group_field = ScoDocSiteConfig.get("edt_ics_group_field")
|
|
|
|
edt_ics_group_regexp = ScoDocSiteConfig.get("edt_ics_group_regexp")
|
|
|
|
try:
|
|
|
|
edt_ics_group_pattern = (
|
|
|
|
re.compile(edt_ics_group_regexp) if edt_ics_group_regexp else None
|
|
|
|
)
|
|
|
|
except re.error as exc:
|
|
|
|
raise ScoValueError(
|
|
|
|
"expression d'extraction du groupe depuis l'emploi du temps invalide"
|
|
|
|
) from exc
|
|
|
|
edt_ics_mod_field = ScoDocSiteConfig.get("edt_ics_mod_field")
|
|
|
|
edt_ics_mod_regexp = ScoDocSiteConfig.get("edt_ics_mod_regexp")
|
|
|
|
try:
|
|
|
|
edt_ics_mod_pattern = (
|
|
|
|
re.compile(edt_ics_mod_regexp) if edt_ics_mod_regexp else None
|
|
|
|
)
|
|
|
|
except re.error as exc:
|
|
|
|
raise ScoValueError(
|
|
|
|
"expression d'extraction du module depuis l'emploi du temps invalide"
|
|
|
|
) from exc
|
2023-12-28 23:05:19 +01:00
|
|
|
edt_ics_uid_field = ScoDocSiteConfig.get("edt_ics_uid_field")
|
2024-01-02 23:05:08 +01:00
|
|
|
edt_ics_uid_pattern = get_ics_uid_pattern()
|
|
|
|
|
2024-01-17 21:58:45 +01:00
|
|
|
# --- Groupes
|
2023-11-11 18:13:18 +01:00
|
|
|
group_colors = {
|
|
|
|
group_name: _COLOR_PALETTE[i % (len(_COLOR_PALETTE) - 1) + 1]
|
|
|
|
for i, group_name in enumerate(edt2group)
|
|
|
|
}
|
2024-01-03 14:43:26 +01:00
|
|
|
edt_groups_ids = set() # les ids de groupes normalisés tels que dans l'ics
|
2024-01-17 21:58:45 +01:00
|
|
|
|
2023-12-28 23:05:19 +01:00
|
|
|
edt2user: dict[str, User | None] = {} # construit au fur et à mesure (cache)
|
2023-11-13 15:08:09 +01:00
|
|
|
# ---
|
2023-11-11 18:13:18 +01:00
|
|
|
events = [e for e in calendar.walk() if e.name == "VEVENT"]
|
2023-11-13 15:08:09 +01:00
|
|
|
events_sco = []
|
2023-11-11 18:13:18 +01:00
|
|
|
for event in events:
|
|
|
|
if "DESCRIPTION" in event:
|
2023-11-13 15:08:09 +01:00
|
|
|
# --- Titre de l'évènement
|
2023-11-19 22:35:04 +01:00
|
|
|
title_edt = (
|
2024-01-03 22:11:49 +01:00
|
|
|
extract_event_edt_id(event, edt_ics_title_field, edt_ics_title_pattern)
|
2023-11-13 15:08:09 +01:00
|
|
|
if edt_ics_title_pattern
|
|
|
|
else "non configuré"
|
2023-11-11 18:13:18 +01:00
|
|
|
)
|
2023-11-19 22:35:04 +01:00
|
|
|
# title remplacé par le nom du module scodoc quand il est trouvé
|
|
|
|
title = title_edt
|
2024-01-17 21:58:45 +01:00
|
|
|
# --- ModuleImpl
|
|
|
|
if edt_ics_mod_pattern:
|
|
|
|
edt_module = extract_event_edt_id(
|
|
|
|
event, edt_ics_mod_field, edt_ics_mod_pattern
|
|
|
|
)
|
|
|
|
modimpl: ModuleImpl = edt2modimpl.get(edt_module, None)
|
|
|
|
if modimpl:
|
|
|
|
title = modimpl.module.titre_str()
|
|
|
|
else:
|
|
|
|
modimpl = False
|
|
|
|
edt_module = ""
|
2023-11-13 15:08:09 +01:00
|
|
|
# --- Group
|
|
|
|
if edt_ics_group_pattern:
|
2024-01-03 22:11:49 +01:00
|
|
|
edt_group = extract_event_edt_id(
|
2023-11-13 15:08:09 +01:00
|
|
|
event, edt_ics_group_field, edt_ics_group_pattern
|
|
|
|
)
|
2023-12-24 16:09:07 +01:00
|
|
|
edt_groups_ids.add(edt_group)
|
2023-11-14 15:55:52 +01:00
|
|
|
# si pas de groupe dans l'event, ou si groupe non reconnu,
|
|
|
|
# prend toute la promo ("tous")
|
2024-01-17 21:58:45 +01:00
|
|
|
event_default_group = (
|
|
|
|
default_group
|
|
|
|
if default_group
|
|
|
|
else (modimpl.formsemestre.get_default_group() if modimpl else None)
|
|
|
|
)
|
2023-11-13 15:08:09 +01:00
|
|
|
group: GroupDescr = (
|
2024-01-17 21:58:45 +01:00
|
|
|
edt2group.get(edt_group, event_default_group)
|
2023-11-13 15:08:09 +01:00
|
|
|
if edt_group
|
2024-01-17 21:58:45 +01:00
|
|
|
else event_default_group
|
2023-11-13 15:08:09 +01:00
|
|
|
)
|
|
|
|
group_bg_color = (
|
|
|
|
group_colors.get(edt_group, _EVENT_DEFAULT_COLOR)
|
|
|
|
if group
|
|
|
|
else "lightgrey"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
edt_group = ""
|
|
|
|
group = False
|
|
|
|
group_bg_color = _EVENT_DEFAULT_COLOR
|
|
|
|
|
2024-01-03 22:11:49 +01:00
|
|
|
# --- Enseignants
|
|
|
|
users: list[User] = []
|
2023-12-28 23:05:19 +01:00
|
|
|
if edt_ics_uid_pattern:
|
2024-01-03 22:11:49 +01:00
|
|
|
ens_edt_ids = extract_event_edt_ids(
|
2023-12-28 23:05:19 +01:00
|
|
|
event, edt_ics_uid_field, edt_ics_uid_pattern
|
|
|
|
)
|
2024-01-03 22:11:49 +01:00
|
|
|
for ens_edt_id in ens_edt_ids:
|
|
|
|
if ens_edt_id in edt2user:
|
|
|
|
ens = edt2user[ens_edt_id]
|
|
|
|
else:
|
|
|
|
ens = User.query.filter_by(edt_id=ens_edt_id).first()
|
|
|
|
edt2user[ens_edt_id] = ens
|
|
|
|
if ens:
|
|
|
|
users.append(ens)
|
2023-12-28 23:05:19 +01:00
|
|
|
else:
|
2024-01-03 22:11:49 +01:00
|
|
|
ens_edt_ids = []
|
2023-11-13 15:08:09 +01:00
|
|
|
#
|
|
|
|
events_sco.append(
|
|
|
|
{
|
2023-11-19 22:35:04 +01:00
|
|
|
"title": title, # titre event ou nom module
|
|
|
|
"title_edt": title_edt, # titre event
|
2023-11-13 15:08:09 +01:00
|
|
|
"edt_group": edt_group, # id group edt non traduit
|
|
|
|
"group": group, # False si extracteur non configuré
|
|
|
|
"group_bg_color": group_bg_color, # associée au groupe
|
|
|
|
"modimpl": modimpl, # False si extracteur non configuré
|
|
|
|
"edt_module": edt_module, # id module edt non traduit
|
2023-12-28 23:05:19 +01:00
|
|
|
# Enseignant
|
2024-01-03 22:11:49 +01:00
|
|
|
"edt_ens_ids": ens_edt_ids, # ids ens edt, normalisés mais non traduits
|
|
|
|
"users": users,
|
2023-11-13 23:43:54 +01:00
|
|
|
# heures pour saisie abs: en heure LOCALE DU SERVEUR
|
|
|
|
"heure_deb": event.decoded("dtstart")
|
|
|
|
.replace(tzinfo=timezone.utc)
|
|
|
|
.astimezone(tz=None)
|
2024-04-02 23:37:23 +02:00
|
|
|
.strftime(scu.TIME_FMT),
|
2023-11-13 23:43:54 +01:00
|
|
|
"heure_fin": event.decoded("dtend")
|
|
|
|
.replace(tzinfo=timezone.utc)
|
|
|
|
.astimezone(tz=None)
|
2024-04-02 23:37:23 +02:00
|
|
|
.strftime(scu.TIME_FMT),
|
2023-11-17 00:22:46 +01:00
|
|
|
"jour": event.decoded("dtstart").date().isoformat(),
|
2023-11-13 15:08:09 +01:00
|
|
|
"start": event.decoded("dtstart").isoformat(),
|
|
|
|
"end": event.decoded("dtend").isoformat(),
|
2024-01-17 21:58:45 +01:00
|
|
|
"UID": event.decoded("UID").decode("utf-8"),
|
|
|
|
"raw": event.to_ical().decode("utf-8"),
|
2023-11-13 15:08:09 +01:00
|
|
|
}
|
2023-11-12 21:45:06 +01:00
|
|
|
)
|
2023-12-24 16:09:07 +01:00
|
|
|
return events_sco, sorted(edt_groups_ids)
|
2023-11-11 18:13:18 +01:00
|
|
|
|
|
|
|
|
2024-01-03 22:11:49 +01:00
|
|
|
def extract_event_edt_id(
|
2024-01-02 23:05:08 +01:00
|
|
|
event: icalendar.cal.Event,
|
|
|
|
ics_field: str,
|
|
|
|
pattern: re.Pattern,
|
|
|
|
none_if_no_match=False,
|
2024-01-03 14:43:26 +01:00
|
|
|
) -> str | None:
|
|
|
|
"""Extrait la chaine (id) de l'évènement et la normalise.
|
2024-01-03 22:11:49 +01:00
|
|
|
Si l'event n'a pas le champ: "-"
|
2024-01-03 14:43:26 +01:00
|
|
|
Si pas de match: None
|
|
|
|
"""
|
2023-11-13 15:08:09 +01:00
|
|
|
if not event.has_key(ics_field):
|
2023-11-11 18:13:18 +01:00
|
|
|
return "-"
|
2023-11-13 15:08:09 +01:00
|
|
|
data = event.decoded(ics_field).decode("utf-8") # assume ics in utf8
|
|
|
|
m = pattern.search(data)
|
2023-11-11 18:13:18 +01:00
|
|
|
if m and len(m.groups()) > 0:
|
2024-01-03 14:43:26 +01:00
|
|
|
return scu.normalize_edt_id(m.group(1))
|
2024-01-02 23:05:08 +01:00
|
|
|
# fallback: if not none_if_no_match, ics field complete
|
|
|
|
return None if none_if_no_match else data
|
2023-11-11 18:13:18 +01:00
|
|
|
|
|
|
|
|
2024-01-03 22:11:49 +01:00
|
|
|
def extract_event_edt_ids(
|
|
|
|
event: icalendar.cal.Event,
|
|
|
|
ics_field: str,
|
|
|
|
pattern: re.Pattern,
|
|
|
|
) -> list[str] | None:
|
|
|
|
"""Extrait les edt_id de l'évènement et les normalise.
|
|
|
|
Si l'event n'a pas le champ: None
|
|
|
|
Si pas de match: liste vide
|
|
|
|
Utilisé pour les enseignants uniquement.
|
|
|
|
"""
|
|
|
|
if not event.has_key(ics_field):
|
|
|
|
return
|
|
|
|
data = event.decoded(ics_field).decode("utf-8") # assume ics in utf8
|
|
|
|
matches = pattern.findall(data)
|
|
|
|
# nota: pattern may have zero or one group, so the result
|
|
|
|
# is a list of strings, not a list of matches
|
|
|
|
return [scu.normalize_edt_id(m) for m in matches if m]
|
|
|
|
|
|
|
|
|
2023-11-11 18:13:18 +01:00
|
|
|
def formsemestre_retreive_modimpls_from_edt_id(
|
|
|
|
formsemestre: FormSemestre,
|
|
|
|
) -> dict[str, ModuleImpl]:
|
2024-01-03 14:43:26 +01:00
|
|
|
"""Construit un dict donnant le moduleimpl de chaque edt_id (normalisé)"""
|
2023-11-19 22:35:04 +01:00
|
|
|
edt2modimpl = {}
|
|
|
|
for modimpl in formsemestre.modimpls:
|
|
|
|
for edt_id in modimpl.get_edt_ids():
|
|
|
|
if edt_id:
|
|
|
|
edt2modimpl[edt_id] = modimpl
|
2023-11-11 18:13:18 +01:00
|
|
|
return edt2modimpl
|
|
|
|
|
|
|
|
|
|
|
|
def formsemestre_retreive_groups_from_edt_id(
|
|
|
|
formsemestre: FormSemestre,
|
|
|
|
) -> dict[str, GroupDescr]:
|
2024-01-03 14:43:26 +01:00
|
|
|
"""Construit un dict donnant le groupe de chaque edt_id
|
|
|
|
La clé edt_id est sans accents, lowercase.
|
|
|
|
"""
|
2023-11-11 18:13:18 +01:00
|
|
|
edt2group = {}
|
2024-01-03 14:43:26 +01:00
|
|
|
group: GroupDescr
|
2023-11-11 18:13:18 +01:00
|
|
|
for partition in formsemestre.partitions:
|
2024-01-03 14:43:26 +01:00
|
|
|
for group in partition.groups:
|
|
|
|
for edt_id in group.get_edt_ids():
|
|
|
|
edt2group[edt_id] = group
|
2023-11-11 18:13:18 +01:00
|
|
|
return edt2group
|