init db with test data

This commit is contained in:
2025-08-28 06:38:35 +02:00
parent 55f2be4870
commit 89bb69dce5
2 changed files with 66 additions and 0 deletions

63
initdb.py Normal file
View File

@@ -0,0 +1,63 @@
from types import resolve_bases
import models
from db import engine
from sqlalchemy import MetaData
from passlib.hash import argon2
def write_db(db, model):
db.add(model)
db.commit()
def clean_db():
models.Base.metadata.drop_all(bind=engine)
def init_test_db(db):
# clean up everything before starting
# models.Attendee.__table__.drop(engine)
# models.Event.__table__.drop(engine)
# models.Team.__table__.drop(engine)
# models.Contract.__table__.drop(engine)
# models.Customer.__table__.drop(engine)
# models.Collaborator.__table__.drop(engine)
# models.Credentials.__table__.drop(engine)
# create teams
teams = ["commercial", "management", "support"]
for item in teams:
team = models.Team(name=item)
write_db(db, team)
# create a commercial
com1 = models.Collaborator(name="com1",
email="com1@truc.fr",
phone=1092837465,
team_id=1,
)
write_db(db, com1)
commercial_password = models.Credentials(
collaborator_id=com1.id,
password_hash=argon2.hash("testtest"))
write_db(db, commercial_password)
# create a manager
man = models.Collaborator(name="admin",
email="pp",
phone=2,
team_id=2,
)
write_db(db, man)
man_password = models.Credentials(
collaborator_id = man.id,
password_hash = argon2.hash("testtest"))
write_db(db, man_password)
# create an attendee
attendee = models.Attendee(name="Guest")
write_db(db, attendee)
# data = [com1, commercial_password, attendee]

View File

@@ -4,11 +4,13 @@ from db import engine, session
from controllers import App
from views import View
from authentication import PasswordTools
from initdb import init_test_db
import sentry_sdk
import config
def main():
sentry_sdk.init(
dsn=config.sentry_url,
@@ -37,4 +39,5 @@ def main():
if __name__ == "__main__":
models.Base.metadata.create_all(bind=engine)
init_test_db(session)
main()