system-pri/backend/app/commands/init_db.py

38 lines
1.0 KiB
Python
Raw Normal View History

from flask.cli import with_appcontext
from click import command
from ..dependencies import db
from ..factory import ProjectSupervisorFactory, GroupFactory, StudentFactory
@command('init_db')
@with_appcontext
def init_db() -> None:
"""Fill database with some data"""
db.drop_all()
db.create_all()
num_of_supervisors = 5
projects_supervisors = [ProjectSupervisorFactory() for _ in range(num_of_supervisors)]
db.session.add_all(projects_supervisors)
db.session.commit()
groups = [GroupFactory(project_supervisor=projects_supervisors[i]) for i in range(num_of_supervisors)]
db.session.add_all(groups)
db.session.commit()
num_of_students = num_of_supervisors * 3
students = [StudentFactory(group=groups[i % num_of_supervisors]) for i in range(num_of_students)]
max_count = 10
max_length = len(students)
start_count = 0
while True:
if start_count > max_length:
break
db.session.add_all(students[start_count:max_count])
db.session.commit()
start_count += max_count