Preliminary version of persistence ready for testing
This commit is contained in:
parent
672b9874f2
commit
30bd7678fc
|
@ -31,6 +31,13 @@ class Application(Repository):
|
|||
return cls._instance
|
||||
|
||||
def insert(self, name: str, description: str, application_uuid: str) -> Union[Model, Dict]:
|
||||
"""
|
||||
Inserts a new application
|
||||
:param name: Application name
|
||||
:param description: Application description
|
||||
:param application_uuid: Unique identifier for the application
|
||||
:return: application and dictionary
|
||||
"""
|
||||
application = self.get_by_uuid(application_uuid)
|
||||
if application is None:
|
||||
try:
|
||||
|
@ -59,7 +66,19 @@ class Application(Repository):
|
|||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while updating application: {err}')
|
||||
return {'err_msg': 'Error occurred while updating application'}
|
||||
return {'message': 'Error occurred while updating application'}
|
||||
|
||||
def delete(self, application_uuid: str):
|
||||
"""
|
||||
Deletes an application with the application_uuid
|
||||
:param application_uuid: The application uuid
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.application_uuid == application_uuid).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while deleting application: {err}')
|
||||
|
||||
def get_by_uuid(self, application_uuid: str) -> [Model]:
|
||||
"""
|
||||
|
@ -73,15 +92,3 @@ class Application(Repository):
|
|||
).first()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching application by application_uuid: {err}')
|
||||
|
||||
def delete_application(self, application_uuid: str):
|
||||
"""
|
||||
Deletes an application with the application_uuid
|
||||
:param application_uuid: The application uuid
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.application_uuid == application_uuid).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while deleting application: {err}')
|
||||
|
|
|
@ -35,7 +35,7 @@ class City(Repository):
|
|||
|
||||
def insert(self, city: CityHub, application_id, user_id: int) -> Union[Model, Dict]:
|
||||
"""
|
||||
Insert a city
|
||||
Inserts a city
|
||||
:param city: The complete city instance
|
||||
:param application_id: Application id owning the instance
|
||||
:param user_id: User id owning the instance
|
||||
|
@ -58,6 +58,32 @@ class City(Repository):
|
|||
except SQLAlchemyError as err:
|
||||
logger.error(f'An error occurred while creating city: {err}')
|
||||
|
||||
def update(self, city_id: int, city: CityHub):
|
||||
"""
|
||||
Updates a city name (other updates makes no sense)
|
||||
:param city_id: the id of the city to be updated
|
||||
:param city: the city object
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
now = datetime.datetime.utcnow()
|
||||
self.session.query(Model).filter(Model.id == city_id).update({'name': city.name,'updated': now})
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while updating city: {err}')
|
||||
|
||||
def delete(self, city_id: int):
|
||||
"""
|
||||
Deletes a City with the id
|
||||
:param city_id: the city id
|
||||
:return: a city
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.id == city_id).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching city: {err}')
|
||||
|
||||
def get_by_id(self, city_id: int) -> Model:
|
||||
"""
|
||||
Fetch a City based on the id
|
||||
|
@ -106,29 +132,3 @@ class City(Repository):
|
|||
return [building[0] for building in result_set]
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching city by name: {err}')
|
||||
|
||||
def update(self, city_id: int, city: CityHub):
|
||||
"""
|
||||
Updates a city name (other updates makes no sense)
|
||||
:param city_id: the id of the city to be updated
|
||||
:param city: the city object
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
now = datetime.datetime.utcnow()
|
||||
self.session.query(Model).filter(Model.id == city_id).update({'name': city.name,'updated': now})
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while updating city: {err}')
|
||||
|
||||
def delete_city(self, city_id: int):
|
||||
"""
|
||||
Deletes a City with the id
|
||||
:param city_id: the city id
|
||||
:return: a city
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.id == city_id).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching city: {err}')
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
"""
|
||||
City Object repository with database CRUD operations
|
||||
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||
Copyright © 2022 Concordia CERC group
|
||||
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from typing import Union, Dict
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from hub.hub_logger import logger
|
||||
from hub.persistence import Repository
|
||||
from hub.persistence.models import CityObject as Model
|
||||
from hub.city_model_structure.building import Building
|
||||
|
||||
|
||||
class CityObject(Repository):
|
||||
_instance = None
|
||||
|
||||
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
|
||||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(CityObject, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def insert(self, city_id:int, building: Building) -> Union[Model, Dict]:
|
||||
"""
|
||||
Inserts a new city object
|
||||
:param city_id: city id for the city owning this city object
|
||||
:param building: the city object (only building for now) to be inserted
|
||||
return CityObject model and dictionary
|
||||
"""
|
||||
city_object = self.get_by_name_and_city(building.name, city_id)
|
||||
if city_object is None:
|
||||
try:
|
||||
object_usage = ''
|
||||
for internal_zone in building.internal_zones:
|
||||
for usage in internal_zone.usages:
|
||||
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
|
||||
object_usage = object_usage.rstrip()
|
||||
city_object = Model(city_id=city_id,
|
||||
name=building.name,
|
||||
alias=building.alias,
|
||||
object_type=building.type,
|
||||
year_of_construction=building.year_of_construction,
|
||||
function=building.function,
|
||||
usage=object_usage,
|
||||
volume=building.volume,
|
||||
area=building.floor_area)
|
||||
self.session.add(city_object)
|
||||
self.session.flush()
|
||||
self.session.commit()
|
||||
return city_object
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'An error occurred while creating city_object: {err}')
|
||||
else:
|
||||
return {'message': f'A city_object named {building.name} already exists in that city'}
|
||||
|
||||
def update(self,city_id: int, building: Building) -> Union[Dict, None]:
|
||||
"""
|
||||
Updates an application
|
||||
:param city_id: the city id of the city owning the city object
|
||||
:param building: the city object
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
object_usage = ''
|
||||
for internal_zone in building.internal_zones:
|
||||
for usage in internal_zone.usages:
|
||||
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
|
||||
object_usage = object_usage.rstrip()
|
||||
self.session.query(Model).filter(Model.name == building.name and Model.city_id == city_id).update(
|
||||
{'name': building.name,
|
||||
'alias': building.alias,
|
||||
'object_type': building.type,
|
||||
'year_of_construction': building.year_of_construction,
|
||||
'function': building.function,
|
||||
'usage': object_usage,
|
||||
'volume': building.volume,
|
||||
'area': building.floor_area,
|
||||
'updated': datetime.datetime.utcnow()})
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while updating city object: {err}')
|
||||
return {'message': 'Error occurred while updating application'}
|
||||
|
||||
def delete(self, city_id: int, name: str):
|
||||
"""
|
||||
Deletes an application with the application_uuid
|
||||
:param city_id: The id for the city owning the city object
|
||||
:param name: The city object name
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.city_id == city_id and Model.name == name).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while deleting application: {err}')
|
||||
|
||||
def get_by_name_and_city(self, name, city_id) -> [Model]:
|
||||
"""
|
||||
Fetch a city object based on name and city id
|
||||
:param name: city object name
|
||||
:param city_id: a city identifier
|
||||
:return: [CityObject] with the provided name belonging to the city with id city_id
|
||||
"""
|
||||
try:
|
||||
return self.session.execute(select(Model).where(
|
||||
Model.name == name and Model.city_id == city_id
|
||||
)).first()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching application by application_uuid: {err}')
|
137
hub/persistence/repositories/simulation_results.py
Normal file
137
hub/persistence/repositories/simulation_results.py
Normal file
|
@ -0,0 +1,137 @@
|
|||
"""
|
||||
Simulation results repository with database CRUD operations
|
||||
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||
Copyright © 2022 Concordia CERC group
|
||||
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from typing import Union, Dict
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from hub.hub_logger import logger
|
||||
from hub.persistence import Repository
|
||||
from hub.persistence.models import SimulationResults as Model
|
||||
from hub.persistence.models import City
|
||||
from hub.persistence.models import CityObject
|
||||
|
||||
from hub.city_model_structure.building import Building
|
||||
|
||||
|
||||
class SimulationResults(Repository):
|
||||
_instance = None
|
||||
|
||||
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
|
||||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(SimulationResults, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def insert(self, name: str, values: str, city_id=None, city_object_id=None) -> Union[Model, Dict]:
|
||||
"""
|
||||
Inserts simulations results linked either with a city as a whole or with a city object
|
||||
:param name: results name
|
||||
:param values: the simulation results in json format
|
||||
:param city_id: optional city id
|
||||
:param city_object_id: optional city object id
|
||||
:return SimulationResults or Dictionary
|
||||
"""
|
||||
if city_id is not None:
|
||||
city = self.get_city(city_id)
|
||||
if city is None:
|
||||
return {'message': f'City does not exists'}
|
||||
else:
|
||||
city_object = self.get_city_object(city_object_id)
|
||||
if city_object is None:
|
||||
return {'message': f'City object does not exists'}
|
||||
try:
|
||||
simulation_result = Model(name=name,
|
||||
values=values,
|
||||
city_id=city_id,
|
||||
city_object_id=city_object_id)
|
||||
self.session.add(simulation_result)
|
||||
self.session.flush()
|
||||
self.session.commit()
|
||||
return simulation_result
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'An error occurred while creating city_object: {err}')
|
||||
|
||||
def update(self, name: str, values: str, city_id=None, city_object_id=None) -> Union[Dict, None]:
|
||||
"""
|
||||
Updates simulation results for a city or a city object
|
||||
:param name: The simulation results tool and workflow name
|
||||
:param values: the simulation results in json format
|
||||
:param city_id: optional city id
|
||||
:param city_object_id: optional city object id
|
||||
:return: None or dictionary
|
||||
"""
|
||||
try:
|
||||
if city_id is not None:
|
||||
self.session.query(Model).filter(Model.name == name and Model.city_id == city_id).update(
|
||||
{
|
||||
'values': values,
|
||||
'updated': datetime.datetime.utcnow()
|
||||
})
|
||||
self.session.commit()
|
||||
elif city_object_id is not None:
|
||||
self.session.query(Model).filter(Model.name == name and Model.city_object_id == city_object_id).update(
|
||||
{
|
||||
'values': values,
|
||||
'updated': datetime.datetime.utcnow()
|
||||
})
|
||||
self.session.commit()
|
||||
else:
|
||||
return {'message': 'Missing either city_id or city_object_id'}
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while updating city object: {err}')
|
||||
return {'message': 'Error occurred while updating application'}
|
||||
|
||||
def delete(self, name: str, city_id=None, city_object_id=None):
|
||||
"""
|
||||
Deletes an application with the application_uuid
|
||||
:param name: The simulation results tool and workflow name
|
||||
:param city_id: The id for the city owning the simulation results
|
||||
:param city_object_id: the id for the city_object ownning these simulation results
|
||||
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
if city_id is not None:
|
||||
self.session.query(Model).filter(Model.name == name and Model.city_id == city_id).delete()
|
||||
self.session.commit()
|
||||
elif city_object_id is not None:
|
||||
self.session.query(Model).filter(Model.name == name and Model.city_object_id == city_object_id).delete()
|
||||
self.session.commit()
|
||||
else:
|
||||
return {'message': 'Missing either city_id or city_object_id'}
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while deleting application: {err}')
|
||||
|
||||
def get_city(self, city_id) -> [City]:
|
||||
"""
|
||||
Fetch a city object based city id
|
||||
:param city_id: a city identifier
|
||||
:return: [City] with the provided city_id
|
||||
"""
|
||||
try:
|
||||
return self.session.execute(select(City).where(City.id == city_id)).first()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching city by city_id: {err}')
|
||||
|
||||
def get_city_object(self, city_object_id) -> [CityObject]:
|
||||
"""
|
||||
Fetch a city object based city id
|
||||
:param city_object_id: a city object identifier
|
||||
:return: [CityObject] with the provided city_object_id
|
||||
"""
|
||||
try:
|
||||
return self.session.execute(select(CityObject).where(CityObject.id == city_object_id)).first()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching city by city_id: {err}')
|
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
City repository with database CRUD operations
|
||||
User repository with database CRUD operations
|
||||
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||
Copyright © 2022 Concordia CERC group
|
||||
Project Coder Peter Yefi peteryefi@gmail.com
|
||||
|
@ -31,6 +31,14 @@ class User(Repository):
|
|||
return cls._instance
|
||||
|
||||
def insert(self, name: str, password: str, role: UserRoles, application_id: int) -> Union[Model, Dict]:
|
||||
"""
|
||||
Inserts a new user
|
||||
:param name: user name
|
||||
:param password: user password
|
||||
:param role: user rol [Admin or Hub_Reader]
|
||||
:param application_id: user application id
|
||||
:return: [User, Dictionary]
|
||||
"""
|
||||
user = self.get_by_name_and_application(name, application_id)
|
||||
if user is None:
|
||||
try:
|
||||
|
@ -65,6 +73,18 @@ class User(Repository):
|
|||
logger.error(f'Error while updating user: {err}')
|
||||
return {'err_msg': 'Error occurred while updated user'}
|
||||
|
||||
def delete(self, user_id: int):
|
||||
"""
|
||||
Deletes a user with the id
|
||||
:param user_id: the user id
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.id == user_id).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching user: {err}')
|
||||
|
||||
def get_by_name_and_application(self, name: str, application_id: int) -> [Model]:
|
||||
"""
|
||||
Fetch user based on the email address
|
||||
|
@ -79,18 +99,6 @@ class User(Repository):
|
|||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching user by name and application: {err}')
|
||||
|
||||
def delete_user(self, user_id: int):
|
||||
"""
|
||||
Deletes a user with the id
|
||||
:param user_id: the user id
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
self.session.query(Model).filter(Model.id == user_id).delete()
|
||||
self.session.commit()
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching user: {err}')
|
||||
|
||||
def get_user_by_name_application_and_password(self, name: str, password: str, application_id: int) -> [Model]:
|
||||
"""
|
||||
Fetch user based on the email and password
|
||||
|
|
Loading…
Reference in New Issue
Block a user