Base test: complète étudiants et groupe par defaut

This commit is contained in:
Emmanuel Viennet 2022-03-09 18:03:18 +01:00
parent 10230d20ef
commit fe6790738f
3 changed files with 37 additions and 18 deletions

View File

@ -63,6 +63,12 @@ class GroupDescr(db.Model):
# "A", "C2", ... (NULL for 'all'): # "A", "C2", ... (NULL for 'all'):
group_name = db.Column(db.String(GROUPNAME_STR_LEN)) group_name = db.Column(db.String(GROUPNAME_STR_LEN))
etuds = db.relationship(
"Identite",
secondary="group_membership",
lazy="dynamic",
)
def __repr__(self): def __repr__(self):
return ( return (
f"""<{self.__class__.__name__} {self.id} "{self.group_name or '(tous)'}">""" f"""<{self.__class__.__name__} {self.id} "{self.group_name or '(tous)'}">"""

View File

@ -14,6 +14,7 @@ import click
import flask import flask
from flask.cli import with_appcontext from flask.cli import with_appcontext
from flask.templating import render_template from flask.templating import render_template
from flask_login import login_user, logout_user, current_user
import psycopg2 import psycopg2
import sqlalchemy import sqlalchemy
@ -45,7 +46,6 @@ cli.register(app)
def make_shell_context(): def make_shell_context():
from app.scodoc import notesdb as ndb from app.scodoc import notesdb as ndb
from app.scodoc import sco_utils as scu 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 app as mapp # le package app
import numpy as np import numpy as np
import pandas as pd import pandas as pd
@ -504,6 +504,8 @@ def init_test_database():
ctx = app.test_request_context() ctx = app.test_request_context()
ctx.push() ctx.push()
admin = User.query.filter_by(user_name="admin").first()
login_user(admin)
create_test_api_database.init_test_database() create_test_api_database.init_test_database()

View File

@ -30,7 +30,12 @@ from flask_login import login_user
from app import auth from app import auth
from app import models from app import models
from app import db 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 from tools.fakeportal.gen_nomprenoms import nomprenom
# La formation à utiliser: # La formation à utiliser:
@ -69,19 +74,26 @@ def create_user(dept):
return user return user
def create_fake_etud(): def create_fake_etud(dept):
"""Créé un faux étudiant et l'insère dans la base""" """Créé un faux étudiant et l'insère dans la base"""
civilite = random.choice(("M", "F", "X")) civilite = random.choice(("M", "F", "X"))
nom, prenom = nomprenom(civilite) 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.add(etud)
db.session.commit() 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 return etud
def create_etuds(nb=16): def create_etuds(dept, nb=16):
"create nb etuds" "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): 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.add(modimpl)
db.session.commit() 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 return formsemestre
def inscrit_etudiants(etuds, formsemestre): def inscrit_etudiants(etuds, formsemestre):
"""Inscrit les etudiants aux semestres et à tous ses modules""" """Inscrit les etudiants aux semestres et à tous ses modules"""
for etud in etuds: for etud in etuds:
ins = models.FormSemestreInscription( sco_formsemestre_inscriptions.do_formsemestre_inscription_with_modules(
etudid=etud.id, formsemestre_id=formsemestre.id, etat="I" 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(): def init_test_database():
dept = init_departement("TAPI") dept = init_departement("TAPI")
user = create_user(dept) user = create_user(dept)
login_user(user) etuds = create_etuds(dept)
etuds = create_etuds()
formation = import_formation() formation = import_formation()
formsemestre = create_formsemestre(formation, user) formsemestre = create_formsemestre(formation, user)
inscrit_etudiants(etuds, formsemestre) inscrit_etudiants(etuds, formsemestre)