2021-10-28 00:52:23 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-12-21 00:10:51 +01:00
|
|
|
"""Exemple utilisation API ScoDoc 9 avec jeton obtenu par basic authentication
|
2021-10-28 00:52:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
Utilisation: créer les variables d'environnement: (indiquer les valeurs
|
|
|
|
pour le serveur ScoDoc que vous voulez interroger)
|
|
|
|
|
|
|
|
export SCODOC_URL="https://scodoc.xxx.net/"
|
|
|
|
export SCODOC_USER="xxx"
|
|
|
|
export SCODOC_PASSWD="xxx"
|
|
|
|
export CHECK_CERTIFICATE=0 # ou 1 si serveur de production avec certif SSL valide
|
|
|
|
|
|
|
|
(on peut aussi placer ces valeurs dans un fichier .env du répertoire tests/api).
|
|
|
|
|
|
|
|
|
2022-05-03 13:35:17 +02:00
|
|
|
Travail en cours.
|
2021-10-28 00:52:23 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import urllib3
|
|
|
|
from pprint import pprint as pp
|
|
|
|
|
|
|
|
# --- Lecture configuration (variables d'env ou .env)
|
2022-05-11 00:59:51 +02:00
|
|
|
try:
|
|
|
|
BASEDIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
except NameError:
|
|
|
|
BASEDIR = "."
|
|
|
|
|
2021-10-28 00:52:23 +02:00
|
|
|
load_dotenv(os.path.join(BASEDIR, ".env"))
|
2022-05-03 13:35:17 +02:00
|
|
|
CHK_CERT = bool(int(os.environ.get("CHECK_CERTIFICATE", False)))
|
2022-05-16 16:04:33 +02:00
|
|
|
SCODOC_URL = os.environ["SCODOC_URL"] or "http://localhost:5000"
|
2022-05-03 13:35:17 +02:00
|
|
|
API_URL = SCODOC_URL + "/ScoDoc/api"
|
2021-10-28 00:52:23 +02:00
|
|
|
SCODOC_USER = os.environ["SCODOC_USER"]
|
2022-05-11 00:59:51 +02:00
|
|
|
SCODOC_PASSWORD = os.environ["SCODOC_PASSWORD"]
|
2021-10-28 00:52:23 +02:00
|
|
|
print(f"SCODOC_URL={SCODOC_URL}")
|
2022-05-03 13:35:17 +02:00
|
|
|
print(f"API URL={API_URL}")
|
2021-10-28 00:52:23 +02:00
|
|
|
|
|
|
|
# ---
|
2022-05-03 13:35:17 +02:00
|
|
|
if not CHK_CERT:
|
2021-10-28 00:52:23 +02:00
|
|
|
urllib3.disable_warnings()
|
|
|
|
|
|
|
|
|
|
|
|
class ScoError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def GET(path: str, headers={}, errmsg=None):
|
|
|
|
"""Get and returns as JSON"""
|
2022-05-03 13:35:17 +02:00
|
|
|
r = requests.get(API_URL + "/" + path, headers=headers or HEADERS, verify=CHK_CERT)
|
2021-10-28 00:52:23 +02:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise ScoError(errmsg or "erreur !")
|
|
|
|
return r.json() # decode la reponse JSON
|
|
|
|
|
|
|
|
|
|
|
|
def POST(s, path: str, data: dict, errmsg=None):
|
|
|
|
"""Post"""
|
2022-05-03 13:35:17 +02:00
|
|
|
r = s.post(API_URL + "/" + path, data=data, verify=CHK_CERT)
|
2021-10-28 00:52:23 +02:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise ScoError(errmsg or "erreur !")
|
|
|
|
return r.text
|
|
|
|
|
|
|
|
|
|
|
|
# --- Obtention du jeton (token)
|
2022-05-03 13:35:17 +02:00
|
|
|
r = requests.post(API_URL + "/tokens", auth=(SCODOC_USER, SCODOC_PASSWORD))
|
2021-10-28 00:52:23 +02:00
|
|
|
assert r.status_code == 200
|
|
|
|
token = r.json()["token"]
|
|
|
|
HEADERS = {"Authorization": f"Bearer {token}"}
|
|
|
|
|
2022-05-03 13:35:17 +02:00
|
|
|
r = requests.get(API_URL + "/departements", headers=HEADERS, verify=CHK_CERT)
|
2021-10-28 00:52:23 +02:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise ScoError("erreur de connexion: vérifier adresse et identifiants")
|
|
|
|
|
|
|
|
pp(r.json())
|
|
|
|
|
2022-05-03 13:35:17 +02:00
|
|
|
# Liste de tous les étudiants en cours (de tous les depts)
|
|
|
|
r = requests.get(API_URL + "/etudiants/courant", headers=HEADERS, verify=CHK_CERT)
|
2021-12-21 00:10:51 +01:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise ScoError("erreur de connexion: vérifier adresse et identifiants")
|
|
|
|
|
2022-05-03 13:35:17 +02:00
|
|
|
print(f"{len(r.json())} étudiants courants")
|
|
|
|
|
|
|
|
# Bulletin d'un BUT
|
|
|
|
formsemestre_id = 1052 # A adapter
|
|
|
|
etudid = 16400
|
|
|
|
bul = GET(f"/etudiant/etudid/{etudid}/formsemestre/{formsemestre_id}/bulletin")
|
|
|
|
|
|
|
|
# d'un DUT
|
|
|
|
formsemestre_id = 1028 # A adapter
|
|
|
|
etudid = 14721
|
|
|
|
bul_dut = GET(f"/etudiant/etudid/{etudid}/formsemestre/{formsemestre_id}/bulletin")
|
2021-10-28 00:52:23 +02:00
|
|
|
|
2022-05-11 00:59:51 +02:00
|
|
|
|
|
|
|
# Infos sur un étudiant
|
|
|
|
etudid = 3561
|
|
|
|
code_nip = "11303314"
|
|
|
|
etud = GET(f"/etudiant/etudid/{etudid}")
|
|
|
|
print(etud)
|
|
|
|
|
|
|
|
etud = GET(f"/etudiant/nip/{code_nip}")
|
|
|
|
print(etud)
|
|
|
|
|
|
|
|
sems = GET(f"/etudiant/etudid/{etudid}/formsemestres")
|
|
|
|
print("\n".join([s["titre_num"] for s in sems]))
|
|
|
|
|
|
|
|
sems = GET(f"/etudiant/nip/{code_nip}/formsemestres")
|
|
|
|
print("\n".join([s["titre_num"] for s in sems]))
|
|
|
|
|
2022-05-16 16:04:33 +02:00
|
|
|
# Evaluation
|
|
|
|
evals = GET("/evaluations/1")
|
2022-05-11 00:59:51 +02:00
|
|
|
|
2021-10-28 00:52:23 +02:00
|
|
|
# # --- Recupere la liste de tous les semestres:
|
|
|
|
# sems = GET(s, "Notes/formsemestre_list?format=json", "Aucun semestre !")
|
|
|
|
|
|
|
|
# # sems est une liste de semestres (dictionnaires)
|
|
|
|
# for sem in sems:
|
|
|
|
# if sem["etat"]:
|
|
|
|
# break
|
|
|
|
|
|
|
|
# if sem["etat"] == "0":
|
|
|
|
# raise ScoError("Aucun semestre non verrouillé !")
|
|
|
|
|
|
|
|
# # Affiche le semestre trouvé:
|
|
|
|
# pp(sem)
|
|
|
|
|
|
|
|
# # ---- Récupère la description de ce semestre:
|
|
|
|
# # semdescr = GET(s, f"Notes/formsemestre_description?formsemestre_id={sem['formsemestre_id']}&with_evals=0&format=json" )
|
|
|
|
|
|
|
|
# # ---- Liste les modules et prend le premier
|
|
|
|
# mods = GET(s, f"/Notes/moduleimpl_list?formsemestre_id={sem['formsemestre_id']}")
|
|
|
|
# print(f"{len(mods)} modules dans le semestre {sem['titre']}")
|
|
|
|
|
|
|
|
# mod = mods[0]
|
|
|
|
|
|
|
|
# # ---- Etudiants inscrits dans ce module
|
|
|
|
# inscrits = GET(
|
|
|
|
# s, f"Notes/do_moduleimpl_inscription_list?moduleimpl_id={mod['moduleimpl_id']}"
|
|
|
|
# )
|
|
|
|
# print(f"{len(inscrits)} inscrits dans ce module")
|
|
|
|
# # prend le premier inscrit, au hasard:
|
|
|
|
# etudid = inscrits[0]["etudid"]
|
|
|
|
|
|
|
|
# # ---- Création d'une evaluation le dernier jour du semestre
|
|
|
|
# jour = sem["date_fin"]
|
|
|
|
# evaluation_id = POST(
|
|
|
|
# s,
|
|
|
|
# "/Notes/do_evaluation_create",
|
|
|
|
# data={
|
|
|
|
# "moduleimpl_id": mod["moduleimpl_id"],
|
|
|
|
# "coefficient": 1,
|
|
|
|
# "jour": jour, # "5/9/2019",
|
|
|
|
# "heure_debut": "9h00",
|
|
|
|
# "heure_fin": "10h00",
|
|
|
|
# "note_max": 20, # notes sur 20
|
|
|
|
# "description": "essai",
|
|
|
|
# },
|
|
|
|
# errmsg="échec création évaluation",
|
|
|
|
# )
|
|
|
|
|
|
|
|
# print(
|
|
|
|
# f"Evaluation créée dans le module {mod['moduleimpl_id']}, evaluation_id={evaluation_id}"
|
|
|
|
# )
|
|
|
|
# print(
|
|
|
|
# f"Pour vérifier, aller sur: {DEPT_URL}/Notes/moduleimpl_status?moduleimpl_id={mod['moduleimpl_id']}",
|
|
|
|
# )
|