1
0
forked from ScoDoc/ScoDoc

API: group create

This commit is contained in:
Emmanuel Viennet 2022-07-20 22:03:29 +02:00
parent 4cb2db61bc
commit 48990f5012
5 changed files with 62 additions and 28 deletions

View File

@ -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()

View File

@ -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]

View File

@ -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)

View File

@ -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]

View File

@ -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 !")