Skip to content

Commit

Permalink
feat: add command to populate db with dummy users
Browse files Browse the repository at this point in the history
  • Loading branch information
vugonz committed Nov 17, 2024
1 parent 760c9f7 commit f2146fd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
61 changes: 57 additions & 4 deletions app/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
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()

0 comments on commit f2146fd

Please sign in to comment.