From fe6790738fbf2d686aa73fe430f09d4054b6d1b5 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 9 Mar 2022 18:03:18 +0100 Subject: [PATCH] =?UTF-8?q?Base=20test:=20compl=C3=A8te=20=C3=A9tudiants?= =?UTF-8?q?=20et=20groupe=20par=20defaut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/groups.py | 6 +++ scodoc.py | 4 +- .../fakedatabase/create_test_api_database.py | 45 ++++++++++++------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/app/models/groups.py b/app/models/groups.py index 976d465b..f6452cf7 100644 --- a/app/models/groups.py +++ b/app/models/groups.py @@ -63,6 +63,12 @@ class GroupDescr(db.Model): # "A", "C2", ... (NULL for 'all'): group_name = db.Column(db.String(GROUPNAME_STR_LEN)) + etuds = db.relationship( + "Identite", + secondary="group_membership", + lazy="dynamic", + ) + def __repr__(self): return ( f"""<{self.__class__.__name__} {self.id} "{self.group_name or '(tous)'}">""" diff --git a/scodoc.py b/scodoc.py index 9eea0f1c..c728bed9 100755 --- a/scodoc.py +++ b/scodoc.py @@ -14,6 +14,7 @@ import click import flask from flask.cli import with_appcontext from flask.templating import render_template +from flask_login import login_user, logout_user, current_user import psycopg2 import sqlalchemy @@ -45,7 +46,6 @@ cli.register(app) def make_shell_context(): from app.scodoc import notesdb as ndb from app.scodoc import sco_utils as scu - from flask_login import login_user, logout_user, current_user import app as mapp # le package app import numpy as np import pandas as pd @@ -504,6 +504,8 @@ def init_test_database(): ctx = app.test_request_context() ctx.push() + admin = User.query.filter_by(user_name="admin").first() + login_user(admin) create_test_api_database.init_test_database() diff --git a/tools/fakedatabase/create_test_api_database.py b/tools/fakedatabase/create_test_api_database.py index bfd346fa..cd5a7e7e 100644 --- a/tools/fakedatabase/create_test_api_database.py +++ b/tools/fakedatabase/create_test_api_database.py @@ -30,7 +30,12 @@ from flask_login import login_user from app import auth from app import models from app import db -from app.scodoc import sco_formations +from app.scodoc import ( + sco_formations, + sco_formsemestre, + sco_formsemestre_inscriptions, + sco_groups, +) from tools.fakeportal.gen_nomprenoms import nomprenom # La formation à utiliser: @@ -69,19 +74,26 @@ def create_user(dept): return user -def create_fake_etud(): +def create_fake_etud(dept): """Créé un faux étudiant et l'insère dans la base""" civilite = random.choice(("M", "F", "X")) nom, prenom = nomprenom(civilite) - etud = models.Identite(civilite=civilite, nom=nom, prenom=prenom) + etud = models.Identite(civilite=civilite, nom=nom, prenom=prenom, dept_id=dept.id) db.session.add(etud) db.session.commit() + adresse = models.Adresse( + etudid=etud.id, email=f"{etud.prenom}.{etud.nom}@example.com" + ) + db.session.add(adresse) + admission = models.Admission(etudid=etud.id) + db.session.add(admission) + db.session.commit() return etud -def create_etuds(nb=16): +def create_etuds(dept, nb=16): "create nb etuds" - return [create_fake_etud() for _ in range(nb)] + return [create_fake_etud(dept) for _ in range(nb)] def create_formsemestre(formation, user, semestre_idx=1): @@ -104,30 +116,29 @@ def create_formsemestre(formation, user, semestre_idx=1): ) db.session.add(modimpl) db.session.commit() + partition_id = sco_groups.partition_create( + formsemestre.id, default=True, redirect=False + ) + _group_id = sco_groups.create_group(partition_id, default=True) return formsemestre def inscrit_etudiants(etuds, formsemestre): """Inscrit les etudiants aux semestres et à tous ses modules""" for etud in etuds: - ins = models.FormSemestreInscription( - etudid=etud.id, formsemestre_id=formsemestre.id, etat="I" + sco_formsemestre_inscriptions.do_formsemestre_inscription_with_modules( + formsemestre.id, + etud.id, + group_ids=[], + etat="I", + method="init db test", ) - db.session.add(ins) - for modimpl in formsemestre.modimpls: - insmod = models.ModuleImplInscription( - etudid=etud.id, moduleimpl_id=modimpl.id - ) - db.session.add(insmod) - db.session.commit() def init_test_database(): dept = init_departement("TAPI") user = create_user(dept) - login_user(user) - - etuds = create_etuds() + etuds = create_etuds(dept) formation = import_formation() formsemestre = create_formsemestre(formation, user) inscrit_etudiants(etuds, formsemestre)