From cfd4448ca5f9b442eef1aa9afa7fea7cac38e47c Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 21 Jul 2022 16:46:07 +0200 Subject: [PATCH] API: /formsemestre//resultats --- app/api/formsemestres.py | 30 ++++++++++++++++++++++++++++-- app/comp/res_common.py | 13 ++++++++++--- app/scodoc/sco_groups.py | 18 ++++++++++++++++++ config.py | 2 ++ tests/api/exemple-api-basic.py | 4 ++++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/app/api/formsemestres.py b/app/api/formsemestres.py index bc43431e8..d7385b28d 100644 --- a/app/api/formsemestres.py +++ b/app/api/formsemestres.py @@ -18,7 +18,7 @@ from app.comp.moy_mod import ModuleImplResults from app.comp.res_compat import NotesTableCompat from app.models import Evaluation, FormSemestre, FormSemestreEtape, ModuleImpl from app.scodoc.sco_bulletins import get_formsemestre_bulletin_etud_json -from app.scodoc.sco_groups import get_etud_groups +from app.scodoc import sco_groups from app.scodoc.sco_permissions import Permission from app.scodoc.sco_utils import ModuleType import app.scodoc.sco_utils as scu @@ -257,8 +257,9 @@ def formsemestre_etudiants(formsemestre_id: int, etat: str): inscriptions = [ins for ins in formsemestre.inscriptions if ins.etat == etat] etuds = [ins.etud.to_dict_short() for ins in inscriptions] # Ajout des groupes de chaque étudiants + # XXX A REVOIR: trop inefficace ! for etud in etuds: - etud["groups"] = get_etud_groups(etud["id"], formsemestre_id) + etud["groups"] = sco_groups.get_etud_groups(etud["id"], formsemestre_id) return jsonify(etuds) @@ -368,3 +369,28 @@ def etat_evals(formsemestre_id: int): result.append(modimpl_dict) return jsonify(result) + + +@bp.route("/formsemestre//resultats", methods=["GET"]) +@token_auth.login_required +@token_permission_required(Permission.APIView) +def formsemestre_resultat(formsemestre_id: int): + """Tableau récapitulatif des résultats + Pour chaque étudiant, son état, ses groupes, ses moyennes d'UE et de modules. + """ + formsemestre = FormSemestre.query.get_or_404(formsemestre_id) + app.set_sco_dept(formsemestre.departement.acronym) + res: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre) + rows, footer_rows, titles, column_ids = res.get_table_recap( + convert_values=False, + include_evaluations=False, + mode_jury=False, + allow_html=False, + ) + # Supprime les champs inutiles (mise en forme) + table = [{k: row[k] for k in row if not k[0] == "_"} for row in rows] + # Ajoute les groupes + etud_groups = sco_groups.get_formsemestre_etuds_groups(formsemestre_id) + for row in table: + row["partitions"] = etud_groups.get(row["etudid"], {}) + return jsonify(table) diff --git a/app/comp/res_common.py b/app/comp/res_common.py index 2eb32ac6e..3315f0032 100644 --- a/app/comp/res_common.py +++ b/app/comp/res_common.py @@ -426,9 +426,16 @@ class ResultatsSemestre(ResultatsCache): # --- TABLEAU RECAP def get_table_recap( - self, convert_values=False, include_evaluations=False, mode_jury=False + self, + convert_values=False, + include_evaluations=False, + mode_jury=False, + allow_html=True, ): - """Result: tuple avec + """Table récap. des résultats. + allow_html: si vri, peut-mettre du HTML dans les valeurs + + Result: tuple avec - rows: liste de dicts { column_id : value } - titles: { column_id : title } - columns_ids: (liste des id de colonnes) @@ -591,7 +598,7 @@ class ResultatsSemestre(ResultatsCache): row, f"bonus_ue_{ue.id}", f"Bonus {ue.acronyme}", - val_fmt_html, + val_fmt_html if allow_html else val_fmt, "col_ue_bonus", idx, ) diff --git a/app/scodoc/sco_groups.py b/app/scodoc/sco_groups.py index e05613343..b1044aea5 100644 --- a/app/scodoc/sco_groups.py +++ b/app/scodoc/sco_groups.py @@ -179,6 +179,24 @@ def get_formsemestre_groups(formsemestre_id, with_default=False): return partitions, partitions_etud_groups +def get_formsemestre_etuds_groups(formsemestre_id: int) -> dict: + """{ etudid : { partition_id : group_id } }""" + infos = ndb.SimpleDictFetch( + """SELECT etudid, p.id AS partition_id, gd.id AS group_id + FROM group_descr gd, group_membership gm, partition p + WHERE gd.partition_id = p.id + AND gm.group_id = gd.id + AND p.formsemestre_id = %(formsemestre_id)s + """, + {"formsemestre_id": formsemestre_id}, + ) + # -> {'etudid': 16483, 'group_id': 5317, 'partition_id': 2264}, + d = collections.defaultdict(lambda: {}) + for i in infos: + d[i["etudid"]][i["partition_id"]] = i["group_id"] + return d + + def get_partition_groups(partition): """List of groups in this partition (list of dicts). Some groups may be empty.""" diff --git a/config.py b/config.py index d78c69a91..57c0fb1fd 100755 --- a/config.py +++ b/config.py @@ -65,6 +65,8 @@ class DevConfig(Config): os.environ.get("SCODOC_DATABASE_URI") or "postgresql:///SCODOC_DEV" ) SECRET_KEY = os.environ.get("DEV_SECRET_KEY") or "bb3faec7d9a34eb68a8e3e710087d87a" + # pour le avoir url_for dans le shell: + # SERVER_NAME = os.environ.get("SCODOC_TEST_SERVER_NAME") or "localhost" class TestConfig(DevConfig): diff --git a/tests/api/exemple-api-basic.py b/tests/api/exemple-api-basic.py index 45a4b98b9..126643bfb 100644 --- a/tests/api/exemple-api-basic.py +++ b/tests/api/exemple-api-basic.py @@ -176,6 +176,10 @@ POST_JSON(f"/partition/{2379}/delete") # Recherche de formsemestres sems = GET(f"/formsemestres/query?etape_apo=V1RT&annee_scolaire=2021") +# Table récap: +pp(GET(f"/formsemestre/1063/resultats")[0]) + +pp(GET(f"/formsemestre/880/resultats")[0]) # # sems est une liste de semestres (dictionnaires) # for sem in sems: