ScoDoc/app/api/tools.py

52 lines
1.7 KiB
Python
Raw Normal View History

##############################################################################
# ScoDoc
2023-01-02 13:16:27 +01:00
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
# See LICENSE
##############################################################################
"""ScoDoc 9 API : outils
"""
2022-07-26 09:00:48 +02:00
from flask_login import current_user
from sqlalchemy import desc, or_
from app import models
from app.scodoc.sco_utils import json_error
2022-07-26 09:00:48 +02:00
from app.models import Departement, Identite, Admission
from app.scodoc.sco_permissions import Permission
def get_etud(etudid=None, nip=None, ine=None) -> models.Identite:
"""
L'instance d'étudiant la plus récente en fonction de l'etudid,
ou du code nip ou code ine.
etudid : None ou un int etudid
nip : None ou un int code_nip
ine : None ou un int code_ine
Return None si étudiant inexistant.
"""
allowed_depts = current_user.get_depts_with_permission(Permission.ScoView)
2022-07-26 09:00:48 +02:00
if etudid is not None:
2022-08-18 15:43:14 +02:00
query: Identite = Identite.query.filter_by(id=etudid)
elif nip is not None:
query = Identite.query.filter_by(code_nip=nip)
elif ine is not None:
query = Identite.query.filter_by(code_ine=ine)
else:
return json_error(
404,
message="parametre manquant",
)
2022-07-26 09:00:48 +02:00
if None not in allowed_depts:
# restreint aux départements autorisés:
2022-08-18 15:43:14 +02:00
query = query.join(Departement).filter(
2022-07-26 09:00:48 +02:00
or_(Departement.acronym == acronym for acronym in allowed_depts)
)
etud = query.join(Admission).order_by(desc(Admission.annee)).first()
# dans de rares cas (bricolages manuels, bugs), l'étudiant n'a pas de données d'admission
if etud is None:
etud = query.first()
return etud