1
0
forked from ScoDoc/ScoDoc
ScoDoc/app/scodoc/sco_abs_billets.py

162 lines
5.9 KiB
Python
Raw Normal View History

# -*- 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.
#
# 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
#
##############################################################################
"""Fonctions sur les billets d'absences
"""
from flask import g, url_for
2023-04-03 17:40:45 +02:00
from flask_sqlalchemy.query import Query
from app.models.absences import BilletAbsence
from app.models.etudiants import Identite
from app.scodoc.gen_tables import GenTable
from app.scodoc import sco_preferences
from app.scodoc import sco_utils as scu
2023-04-03 17:40:45 +02:00
def query_billets_etud(etudid: int = None, etat: bool = None) -> Query:
2022-09-11 09:35:47 +02:00
"""Billets d'absences pour un étudiant, ou tous si etudid is None.
Si etat, filtre par état.
Si dans un département et que la gestion des billets n'a pas été activée
dans ses préférences, table toujours vide.
"""
if g.scodoc_dept is not None and not sco_preferences.get_preference(
"handle_billets_abs"
):
return []
2022-09-11 09:35:47 +02:00
billets = BilletAbsence.query
if etudid is not None:
billets = billets.filter_by(etudid=etudid)
if etat is not None:
2022-09-11 09:35:47 +02:00
billets = billets.filter_by(etat=False)
if g.scodoc_dept is not None:
# jointure avec departement de l'étudiant
billets = billets.join(BilletAbsence.etudiant).filter_by(
dept_id=g.scodoc_dept_id
)
return billets
def table_billets_etud(
etudid: int = None, etat: bool = None, with_links=True
) -> GenTable:
"""Page avec table billets."""
etud = Identite.get_etud(etudid) if etudid is not None else None
2022-09-08 09:46:16 +02:00
billets = query_billets_etud(etudid, etat)
return table_billets(billets, etud=etud, with_links=with_links)
def table_billets(
billets: list[BilletAbsence], etud: Identite = None, title="", with_links=True
) -> GenTable:
"""Construit une table de billets d'absences"""
rows = []
for billet in billets:
billet_dict = billet.to_dict()
rows.append(billet_dict)
2022-10-09 14:25:49 +02:00
billet_dict["_nomprenom_order"] = (
billet.etudiant.sort_key if billet.etudiant else ""
)
billet_dict["_abs_begin_str_order"] = (
billet.abs_begin.isoformat() if billet.abs_begin else ""
)
billet_dict["_abs_begin_str_order"] = (
billet.abs_begin.isoformat() if billet.abs_end else ""
)
if billet_dict["abs_begin"].hour < 12:
m = " matin"
else:
m = " après-midi"
billet_dict["abs_begin_str"] = billet.abs_begin.strftime(scu.DATE_FMT) + m
if billet.abs_end.hour < 12:
m = " matin"
else:
m = " après-midi"
billet_dict["abs_end_str"] = billet.abs_end.strftime(scu.DATE_FMT) + m
if billet.etat == 0:
if billet.justified:
billet_dict["etat_str"] = "à traiter"
else:
billet_dict["etat_str"] = "à justifier"
if with_links:
if etud:
billet_dict["_etat_str_target"] = url_for(
"absences.process_billet_absence_form",
billet_id=billet_dict["billet_id"],
scodoc_dept=billet.etudiant.departement.acronym,
etudid=etud.id,
)
else:
billet_dict["_etat_str_target"] = url_for(
"absences.process_billet_absence_form",
billet_id=billet_dict["billet_id"],
scodoc_dept=g.scodoc_dept,
)
billet_dict["_billet_id_target"] = billet_dict["_etat_str_target"]
else:
billet_dict["etat_str"] = "ok"
if not etud:
# ajoute info etudiant
2022-09-22 14:58:46 +02:00
if not billet.etudiant:
billet_dict["nomprenom"] = "???" # should not occur
else:
2022-09-22 14:58:46 +02:00
billet_dict["nomprenom"] = billet.etudiant.nomprenom
2022-10-09 14:25:49 +02:00
billet_dict["_nomprenom_order"] = billet.etudiant.sort_key
2024-02-23 19:03:02 +01:00
billet_dict["_nomprenom_td_attrs"] = (
f'id="{billet.etudiant.id}" class="etudinfo"'
)
if with_links:
billet_dict["_nomprenom_target"] = url_for(
"scolar.fiche_etud",
scodoc_dept=g.scodoc_dept,
etudid=billet_dict["etudid"],
)
if etud and not title:
title = f"Billets d'absence déclarés par {etud.nomprenom}"
columns_ids = ["billet_id"]
if not etud:
columns_ids += ["nomprenom"]
columns_ids += ["abs_begin_str", "abs_end_str", "description", "etat_str"]
tab = GenTable(
titles={
"billet_id": "Numéro",
"abs_begin_str": "Début",
"abs_end_str": "Fin",
"description": "Raison de l'absence",
"etat_str": "Etat",
},
columns_ids=columns_ids,
page_title=title,
html_title=f"<h2>{title}</h2>",
preferences=sco_preferences.SemPreferences(),
rows=rows,
html_sortable=True,
2022-09-22 14:58:46 +02:00
html_class="table_leftalign",
)
return tab