From 48990f501278bf6d7f463538d4907ce4059f9276 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 20 Jul 2022 22:03:29 +0200 Subject: [PATCH] API: group create --- app/api/partitions.py | 7 ++++++ app/models/groups.py | 1 + app/scodoc/sco_cache.py | 12 +++++----- app/scodoc/sco_cursus.py | 41 +++++++++++++++++----------------- tests/api/exemple-api-basic.py | 29 +++++++++++++++++++++--- 5 files changed, 62 insertions(+), 28 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index d783a7e4..d597c4bd 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -9,6 +9,7 @@ """ from flask import abort, jsonify, request +import app from app import db from app.api import bp from app.api.auth import token_auth, token_permission_required @@ -159,6 +160,7 @@ def group_create(partition_id: int): group = GroupDescr(group_name=group_name, partition_id=partition_id) db.session.add(group) db.session.commit() + app.set_sco_dept(partition.formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(partition.formsemestre_id) return jsonify(group.to_dict(with_partition=True)) @@ -174,6 +176,7 @@ def group_delete(group_id: int): formsemestre_id = group.partition.formsemestre_id db.session.delete(group) db.session.commit() + app.set_sco_dept(group.partition.formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(formsemestre_id) return jsonify({"OK": 1}) @@ -194,6 +197,7 @@ def group_edit(group_id: int): group.group_name = group_name.strip() db.session.add(group) db.session.commit() + app.set_sco_dept(group.partition.formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(group.partition.formsemestre_id) return jsonify(group.to_dict(with_partition=True)) @@ -239,6 +243,7 @@ def partition_create(formsemestre_id: int): partition = Partition(**args) db.session.add(partition) db.session.commit() + app.set_sco_dept(formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(formsemestre_id) return jsonify(partition.to_dict(with_groups=True)) @@ -289,6 +294,7 @@ def partition_edit(partition_id: int): if modified: db.session.add(partition) db.session.commit() + app.set_sco_dept(partition.formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(partition.formsemestre_id) return jsonify(partition.to_dict(with_groups=True)) @@ -311,6 +317,7 @@ def partition_delete(partition_id: int): is_parcours = partition.is_parcours() formsemestre: FormSemestre = partition.formsemestre db.session.delete(partition) + app.set_sco_dept(partition.formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(formsemestre.id) if is_parcours: formsemestre.update_inscriptions_parcours_from_groups() diff --git a/app/models/groups.py b/app/models/groups.py index cd71f269..7ccb1593 100644 --- a/app/models/groups.py +++ b/app/models/groups.py @@ -87,6 +87,7 @@ class Partition(db.Model): """as a dict, with or without groups""" d = dict(self.__dict__) d.pop("_sa_instance_state", None) + d.pop("formsemestre") if with_groups: d["groups"] = [group.to_dict(with_partition=False) for group in self.groups] diff --git a/app/scodoc/sco_cache.py b/app/scodoc/sco_cache.py index 7a20a1a6..28e46cfc 100644 --- a/app/scodoc/sco_cache.py +++ b/app/scodoc/sco_cache.py @@ -131,12 +131,14 @@ class EvaluationCache(ScoDocCache): @classmethod def invalidate_sem(cls, formsemestre_id): "delete evaluations in this formsemestre from cache" - req = """SELECT e.id - FROM notes_formsemestre s, notes_evaluation e, notes_moduleimpl m - WHERE s.id = %(formsemestre_id)s and s.id=m.formsemestre_id and e.moduleimpl_id=m.id; - """ + from app.models.evaluations import Evaluation + from app.models.moduleimpls import ModuleImpl + evaluation_ids = [ - x[0] for x in ndb.SimpleQuery(req, {"formsemestre_id": formsemestre_id}) + e.id + for e in Evaluation.query.join(ModuleImpl).filter_by( + formsemestre_id=formsemestre_id + ) ] cls.delete_many(evaluation_ids) diff --git a/app/scodoc/sco_cursus.py b/app/scodoc/sco_cursus.py index eec02b9f..c492f86b 100644 --- a/app/scodoc/sco_cursus.py +++ b/app/scodoc/sco_cursus.py @@ -1,4 +1,3 @@ -# -*- mode: python -*- # -*- coding: utf-8 -*- ############################################################################## @@ -27,15 +26,15 @@ """Gestion des cursus (jurys suivant la formation) """ +from sqlalchemy.sql import text +from app import db from app.but import cursus_but from app.scodoc import sco_cursus_dut from app.comp.res_compat import NotesTableCompat from app.comp import res_sem from app.models import FormSemestre -from app.scodoc import sco_formsemestre -from app.scodoc import sco_formations import app.scodoc.notesdb as ndb # SituationEtudParcours -> get_situation_etud_cursus @@ -111,24 +110,26 @@ def list_formsemestre_utilisateurs_uecap(formsemestre_id): """Liste des formsemestres pouvant utiliser une UE capitalisee de ce semestre (et qui doivent donc etre sortis du cache si l'on modifie ce semestre): meme code formation, meme semestre_id, date posterieure""" - cnx = ndb.GetDBConnexion() - sem = sco_formsemestre.get_formsemestre(formsemestre_id) - F = sco_formations.formation_list(args={"formation_id": sem["formation_id"]})[0] - cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor) - cursor.execute( - """SELECT sem.id - FROM notes_formsemestre sem, notes_formations F - WHERE sem.formation_id = F.id - and F.formation_code = %(formation_code)s - and sem.semestre_id = %(semestre_id)s - and sem.date_debut >= %(date_debut)s - and sem.id != %(formsemestre_id)s; - """, + formsemestre = FormSemestre.query.get(formsemestre_id) + + cursor = db.session.execute( + text( + """ + SELECT sem.id + FROM notes_formsemestre sem, notes_formations F + WHERE sem.formation_id = F.id + and F.formation_code = :formation_code + and sem.semestre_id = :semestre_id + and sem.date_debut >= :date_debut + and sem.id != :formsemestre_id; + """ + ), { - "formation_code": F["formation_code"], - "semestre_id": sem["semestre_id"], + "formation_code": formsemestre.formation.formation_code, + "semestre_id": formsemestre.semestre_id, "formsemestre_id": formsemestre_id, - "date_debut": ndb.DateDMYtoISO(sem["date_debut"]), + "date_debut": formsemestre.date_debut, }, ) - return [x[0] for x in cursor.fetchall()] + + return [x[0] for x in cursor] diff --git a/tests/api/exemple-api-basic.py b/tests/api/exemple-api-basic.py index 4a04abd2..3b8eac45 100644 --- a/tests/api/exemple-api-basic.py +++ b/tests/api/exemple-api-basic.py @@ -20,6 +20,7 @@ Travail en cours. """ from dotenv import load_dotenv +import json import os import requests import urllib3 @@ -51,7 +52,7 @@ class ScoError(Exception): def GET(path: str, headers={}, errmsg=None): """Get and returns as JSON""" - r = requests.get(API_URL + "/" + path, headers=headers or HEADERS, verify=CHK_CERT) + r = requests.get(API_URL + path, headers=headers or HEADERS, verify=CHK_CERT) if r.status_code != 200: raise ScoError(errmsg or f"erreur status={r.status_code} !") return r.json() # decode la reponse JSON @@ -60,10 +61,26 @@ def GET(path: str, headers={}, errmsg=None): def POST(path: str, data: dict = {}, headers={}, errmsg=None): """Post""" r = requests.post( - API_URL + "/" + path, data=data, headers=headers or HEADERS, verify=CHK_CERT + API_URL + path, + data=data, + headers=headers or HEADERS, + verify=CHK_CERT, ) if r.status_code != 200: - raise ScoError(errmsg or "erreur !") + raise ScoError(errmsg or f"erreur status={r.status_code} !") + return r.json() # decode la reponse JSON + + +def POST_JSON(path: str, data: dict = {}, headers={}, errmsg=None): + """Post""" + r = requests.post( + API_URL + path, + json=data, + headers=headers or HEADERS, + verify=CHK_CERT, + ) + if r.status_code != 200: + raise ScoError(errmsg or f"erreur status={r.status_code} !") return r.json() # decode la reponse JSON @@ -72,6 +89,8 @@ r = requests.post(API_URL + "/tokens", auth=(SCODOC_USER, SCODOC_PASSWORD)) assert r.status_code == 200 token = r.json()["token"] HEADERS = {"Authorization": f"Bearer {token}"} +# HEADERS_JSON = HEADERS.copy() +# HEADERS_JSON["Content-Type"] = "application/json" r = requests.get(API_URL + "/departements", headers=HEADERS, verify=CHK_CERT) if r.status_code != 200: @@ -134,6 +153,10 @@ etudid = 16650 group_id = 5315 POST(f"/group/{group_id}/set_etudiant/{etudid}") + +POST_JSON(f"/partition/{pid}/group/create", data={"group_name": "Omega10"}) + + # # --- Recupere la liste de tous les semestres: # sems = GET(s, "Notes/formsemestre_list?format=json", "Aucun semestre !")