hub/persistence/db_setup.py
2022-12-08 20:55:12 -05:00

32 lines
1.2 KiB
Python

from persistence.models import City
from persistence import BaseRepo
from persistence.models import HeatPumpSimulation
from persistence.models import User
from persistence.repositories import UserRepo
from persistence.models import UserRoles
class DBSetup:
def __init__(self, db_name, app_env, dotenv_path):
"""
Creates database tables and a default admin user
:param db_name:
:param app_env:
:param dotenv_path:
"""
repo = BaseRepo(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
City.__table__.create(bind=repo.engine, checkfirst=True)
HeatPumpSimulation.__table__.create(bind=repo.engine, checkfirst=True)
User.__table__.create(bind=repo.engine, checkfirst=True)
self._user_repo = UserRepo(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._create_admin_user(self._user_repo)
def _create_admin_user(self, user_repo):
email = 'admin@hub.com'
password = 'HubAdmin#!98'
print('Creating default admin user...')
user_repo.insert('Administrator', email, password, UserRoles.Admin)
print(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin}')
print('Remember to change the admin default password and email address with the UserFactory')