2023-01-30 06:49:08 -05:00
|
|
|
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.persistence import BaseRepo
|
2023-01-30 06:49:08 -05:00
|
|
|
from hub.persistence.models import City
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.persistence.models import HeatPumpSimulation
|
|
|
|
from hub.persistence.models import User
|
|
|
|
from hub.persistence.models import UserRoles
|
2023-01-30 06:49:08 -05:00
|
|
|
from hub.persistence.models import Application
|
|
|
|
from hub.persistence.models import UserApplications
|
|
|
|
from hub.persistence.repositories import UserRepo
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.hub_logger import logger
|
2022-12-07 19:06:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DBSetup:
|
|
|
|
|
|
|
|
def __init__(self, db_name, app_env, dotenv_path):
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
Creates database tables and a default admin user
|
|
|
|
:param db_name:
|
|
|
|
:param app_env:
|
|
|
|
:param dotenv_path:
|
|
|
|
"""
|
2022-12-07 19:06:17 -05:00
|
|
|
repo = BaseRepo(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
|
2023-01-10 12:52:20 -05:00
|
|
|
User.__table__.create(bind=repo.engine, checkfirst=True)
|
2022-12-07 19:06:17 -05:00
|
|
|
City.__table__.create(bind=repo.engine, checkfirst=True)
|
2023-01-30 06:49:08 -05:00
|
|
|
Application.__table__.create(bind=repo.engine, checkfirst=True)
|
|
|
|
UserApplications.__table__.create(bind=repo.engine, checkfirst=True)
|
2022-12-08 20:54:47 -05:00
|
|
|
HeatPumpSimulation.__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)
|
|
|
|
|
2023-01-26 07:41:56 -05:00
|
|
|
@staticmethod
|
|
|
|
def _create_admin_user(user_repo):
|
2022-12-08 20:55:12 -05:00
|
|
|
email = 'admin@hub.com'
|
2022-12-08 20:54:47 -05:00
|
|
|
password = 'HubAdmin#!98'
|
|
|
|
print('Creating default admin user...')
|
2022-12-14 17:49:29 -05:00
|
|
|
user = user_repo.insert('Administrator', email, password, UserRoles.Admin)
|
|
|
|
if type(user) is dict:
|
|
|
|
logger.info(user)
|
|
|
|
else:
|
2023-01-10 20:30:55 -05:00
|
|
|
print(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin.value}')
|
|
|
|
logger.info(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin.value}')
|
2022-12-14 17:49:29 -05:00
|
|
|
print('Remember to change the admin default password and email address with the UserFactory')
|