2022-07-19 22:17:10 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
|
|
|
# Copyright (c) 1999 - 2022 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_
|
2022-04-22 16:22:10 +02:00
|
|
|
|
2022-07-19 22:17:10 +02:00
|
|
|
from app import models
|
2022-08-07 19:56:25 +02:00
|
|
|
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
|
2022-05-16 15:45:11 +02:00
|
|
|
|
|
|
|
|
2022-07-19 22:17:10 +02:00
|
|
|
def get_etud(etudid=None, nip=None, ine=None) -> models.Identite:
|
2022-04-22 16:22:10 +02:00
|
|
|
"""
|
2022-07-19 22:17:10 +02:00
|
|
|
L'instance d'étudiant la plus récente en fonction de l'etudid,
|
|
|
|
ou du code nip ou code ine.
|
2022-04-22 16:22:10 +02:00
|
|
|
|
|
|
|
etudid : None ou un int etudid
|
|
|
|
nip : None ou un int code_nip
|
|
|
|
ine : None ou un int code_ine
|
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
Return None si étudiant inexistant.
|
2022-04-22 16:22:10 +02:00
|
|
|
"""
|
2022-07-27 16:03:14 +02:00
|
|
|
allowed_depts = current_user.get_depts_with_permission(Permission.ScoView)
|
2022-07-26 09:00:48 +02:00
|
|
|
|
2022-05-16 15:45:11 +02:00
|
|
|
if etudid is not None:
|
2022-07-26 09:00:48 +02:00
|
|
|
etud: Identite = Identite.query.get(etudid)
|
|
|
|
if (None in allowed_depts) or etud.departement.acronym in allowed_depts:
|
|
|
|
return etud
|
|
|
|
return None # accès interdit => pas d'étudiant
|
2022-06-01 15:53:25 +02:00
|
|
|
|
2022-07-19 22:17:10 +02:00
|
|
|
if 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:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(
|
2022-07-19 22:17:10 +02:00
|
|
|
404,
|
|
|
|
message="parametre manquant",
|
|
|
|
)
|
2022-07-26 09:00:48 +02:00
|
|
|
if None not in allowed_depts:
|
|
|
|
# restreint aux départements autorisés:
|
|
|
|
etuds = etuds.join(Departement).filter(
|
|
|
|
or_(Departement.acronym == acronym for acronym in allowed_depts)
|
|
|
|
)
|
2022-07-19 22:17:10 +02:00
|
|
|
return query.join(Admission).order_by(desc(Admission.annee)).first()
|