diff --git a/app/__init__.py b/app/__init__.py index 2980b6f..aad3e89 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -72,9 +72,10 @@ def register_error_handlers(app: Flask): app.register_error_handler(exc.SQLAlchemyError, handle_db_exceptions) def register_commands(app: Flask): - from app.extensions import register_create_admin_user_command, register_initialize_db_command + from app.extensions import register_create_admin_user_command, register_initialize_db_command, register_populate_dummy_db_command register_initialize_db_command(app) register_create_admin_user_command(app) + register_populate_dummy_db_command(app) def setup_logger(app: Flask): """ diff --git a/app/commands/commands.py b/app/commands/commands.py index 19d1a50..a0ac28b 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -4,6 +4,7 @@ from flask import Flask from app.extensions import db +from app.extensions import roles_handler # DEPRECATED please use migrations def register_initialize_db_command(app: Flask): @@ -30,15 +31,67 @@ def populate_admin_user(): username=admin_username, password=admin_password, ist_id="ist1"+admin_username, - member_number=0, name=admin_username, - join_date="1970-01-01", - course=admin_username, email=admin_username, + course=admin_username, + member_number=0, + join_date="1970-01-01", exit_date="", description="Admin user created with create-admin command", extra=admin_username, roles=["sysadmin"] ) db.session.add(admin) - db.session.commit() \ No newline at end of file + db.session.commit() + +def register_populate_dummy_db_command(app: Flask): + @app.cli.command('populate-db') + def populate_dummy(): + from app.models import Member, Project, MemberProjects + def get_state(i: int) -> str: + state = ["Active", "Ended", "Paused"] + return state[i % len(state)] + + projects_names = ["wormhole", "time-machine", "rocket", "robotic-arm"] + projects = [] + for i, p in enumerate(projects_names, 0): + project = Project( + name=p, + start_date="1970-01-01", + state=get_state(i), + description=f"Project of a {p}" + ) + projects.append(project) + db.session.add(project) + + def get_course(i: int) -> str: + courses = ["LEIC", "LMAC", "LEEC", "LEFT", "LMAer", "LETI", "LEBiom"] + return courses[i % len(courses)] + + def random_proj(i: int) -> Project: + return projects[i % len(projects)] + + roles = roles_handler.roles.keys() + for i, r in enumerate(roles, 1): + member = Member( + username=r, + password="password", + ist_id="ist1"+str(i), + name=r, + email=f"{r}@hackerschool.dev", + course=get_course(i), + member_number=i, + join_date="1970-01-01", + exit_date="", + description="Description", + extra="", + roles=[r] + ) + assoc = MemberProjects( + entry_date="1970-01-01", + ) + assoc.member = member + random_proj(i).members.append(assoc) + + db.session.add(member) + db.session.commit()