ScoDoc/app/scodoc/sco_modalites.py

81 lines
2.8 KiB
Python
Raw Normal View History

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
#
##############################################################################
"""Modalites des semestres
La "modalite" est utilisee pour organiser les listes de semestres sur la page d'accueil.
2023-12-31 23:04:06 +01:00
Elle n'est pas utilisée pour les parcours, ni pour rien d'autre
2020-09-26 16:19:37 +02:00
(c'est donc un attribut "cosmétique").
"""
import collections
2024-09-04 21:01:03 +02:00
from app.models import FormSemestre, FormationModalite
2020-09-26 16:19:37 +02:00
2024-09-04 21:01:03 +02:00
def _list_formsemestres_modalites(
formsemestres: list[FormSemestre],
) -> list[FormationModalite]:
2021-01-01 18:40:47 +01:00
"""Liste ordonnée des modalités présentes dans ces formsemestres"""
2020-09-26 16:19:37 +02:00
modalites = {}
for formsemestre in formsemestres:
if formsemestre.modalite not in modalites:
2024-09-04 21:01:03 +02:00
m = FormationModalite.query.filter_by(
modalite=formsemestre.modalite
).first()
if m is not None:
modalites[m.modalite] = m
return sorted(modalites.values(), key=lambda m: m.numero)
2020-09-26 16:19:37 +02:00
def group_formsemestres_by_modalite(
formsemestres: list[FormSemestre],
) -> dict[str, list[FormSemestre]]:
"""Given the list of formsemestre, group them by modalite,
2020-09-26 16:19:37 +02:00
sorted in each one by semestre id and date
"""
sems_by_mod = collections.defaultdict(list)
2024-09-04 21:01:03 +02:00
modalites = _list_formsemestres_modalites(formsemestres)
sems_by_mod = {
2024-09-04 21:01:03 +02:00
modalite.modalite: [
formsemestre
for formsemestre in formsemestres
2024-09-04 21:01:03 +02:00
if formsemestre.modalite == modalite.modalite
]
for modalite in modalites
}
2020-09-26 16:19:37 +02:00
# tri dans chaque modalité par indice de semestre et date debut
for modalite in modalites:
2024-09-04 21:01:03 +02:00
sems_by_mod[modalite.modalite].sort(
key=lambda x: (
x.semestre_id if x.semestre_id > 0 else -1000 * x.semestre_id,
x.date_debut,
)
)
2020-09-26 16:19:37 +02:00
return sems_by_mod, modalites