From 17f822302395bdfe1f156ba0a3d92c70c3121cd3 Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 30 Jan 2023 06:49:08 -0500 Subject: [PATCH] Add basic application handling to the persistence --- hub/DEPLOYMENT.md | 1 - hub/persistence/db_setup.py | 9 +++++-- hub/persistence/models/__init__.py | 2 ++ hub/persistence/models/application.py | 29 +++++++++++++++++++++ hub/persistence/models/user_applications.py | 29 +++++++++++++++++++++ hub/unittests/test_db_factory.py | 2 +- 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 hub/persistence/models/application.py create mode 100644 hub/persistence/models/user_applications.py diff --git a/hub/DEPLOYMENT.md b/hub/DEPLOYMENT.md index cd62a903..05299696 100644 --- a/hub/DEPLOYMENT.md +++ b/hub/DEPLOYMENT.md @@ -13,7 +13,6 @@ Update your repositories with Install postgresql -` sudo apt-get install postgresql ` *NB: PostgreSQL DB Server runs on a default port of 5432.* diff --git a/hub/persistence/db_setup.py b/hub/persistence/db_setup.py index 631902d0..5bdcb13c 100644 --- a/hub/persistence/db_setup.py +++ b/hub/persistence/db_setup.py @@ -1,9 +1,12 @@ -from hub.persistence.models import City + from hub.persistence import BaseRepo +from hub.persistence.models import City from hub.persistence.models import HeatPumpSimulation from hub.persistence.models import User -from hub.persistence.repositories import UserRepo from hub.persistence.models import UserRoles +from hub.persistence.models import Application +from hub.persistence.models import UserApplications +from hub.persistence.repositories import UserRepo from hub.hub_logger import logger @@ -19,6 +22,8 @@ class DBSetup: repo = BaseRepo(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) User.__table__.create(bind=repo.engine, checkfirst=True) City.__table__.create(bind=repo.engine, checkfirst=True) + Application.__table__.create(bind=repo.engine, checkfirst=True) + UserApplications.__table__.create(bind=repo.engine, checkfirst=True) 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) diff --git a/hub/persistence/models/__init__.py b/hub/persistence/models/__init__.py index d29ea9e3..89986d04 100644 --- a/hub/persistence/models/__init__.py +++ b/hub/persistence/models/__init__.py @@ -3,3 +3,5 @@ from .heat_pump_simulation import HeatPumpSimulation from .heat_pump_simulation import SimulationTypes from .heat_pump_simulation import HeatPumpTypes from .user import User, UserRoles +from .user_applications import UserApplications +from .application import Application diff --git a/hub/persistence/models/application.py b/hub/persistence/models/application.py new file mode 100644 index 00000000..d62baf8f --- /dev/null +++ b/hub/persistence/models/application.py @@ -0,0 +1,29 @@ +""" +Model representation of an application +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca +""" + +import datetime + +from sqlalchemy import Column, Integer, String, Sequence +from sqlalchemy import DateTime + +from hub.persistence.db_config import Base + + +class Application(Base): + """ + A model representation of an application + """ + __tablename__ = "application" + id = Column(Integer, Sequence('application_id_seq'), primary_key=True) + name = Column(String, nullable=False) + description = Column(String, nullable=False) + created = Column(DateTime, default=datetime.datetime.utcnow) + updated = Column(DateTime, default=datetime.datetime.utcnow) + + def __init__(self, name, description): + self.name = name + self.description = description diff --git a/hub/persistence/models/user_applications.py b/hub/persistence/models/user_applications.py new file mode 100644 index 00000000..d4c856af --- /dev/null +++ b/hub/persistence/models/user_applications.py @@ -0,0 +1,29 @@ +""" +Model representation of the user applications +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca +""" + +import datetime + +from sqlalchemy import Column, Integer, ForeignKey +from sqlalchemy import DateTime + +from hub.persistence.db_config import Base + + +class UserApplications(Base): + """ + A model representation of the user applications + """ + __tablename__ = "user_applications" + user_id = Column(Integer, ForeignKey('user.id')) + application_id = Column(Integer, ForeignKey('application.id')) + + created = Column(DateTime, default=datetime.datetime.utcnow) + updated = Column(DateTime, default=datetime.datetime.utcnow) + + def __init__(self, user_id, application_id): + self.user_id = user_id + self.application_id = application_id diff --git a/hub/unittests/test_db_factory.py b/hub/unittests/test_db_factory.py index 5e54a50f..2a8c0719 100644 --- a/hub/unittests/test_db_factory.py +++ b/hub/unittests/test_db_factory.py @@ -29,7 +29,7 @@ class TestDBFactory(TestCase): :return: None """ # Create test database - repo = BaseRepo(db_name='test_db', app_env='TEST', dotenv_path='../.env') + repo = BaseRepo(db_name='test_db', app_env='TEST', dotenv_path='/usr/local/etc/hub/.env') eng = create_engine(f'postgresql://{repo.config.get_db_user()}@/{repo.config.get_db_user()}') try: