Partial correction in persistence

This commit is contained in:
Guille Gutierrez 2023-05-18 16:40:52 -04:00
parent 5fb361e74c
commit 6e249073c5
4 changed files with 54 additions and 68 deletions

View File

@ -48,9 +48,8 @@ class Application(Repository):
self.session.add(application)
self.session.commit()
except SQLAlchemyError as err:
error_message = f'An error occurred while creating application: {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('An error occurred while creating application %s', err)
raise SQLAlchemyError(err)
def update(self, application_uuid: str, name: str, description: str):
"""
@ -66,9 +65,8 @@ class Application(Repository):
).update({'name': name, 'description': description, 'updated': datetime.datetime.utcnow()})
self.session.commit()
except SQLAlchemyError as err:
error_message = f'Error while updating application {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while updating application %s', err)
raise SQLAlchemyError(err)
def delete(self, application_uuid: str):
"""
@ -81,9 +79,8 @@ class Application(Repository):
self.session.flush()
self.session.commit()
except SQLAlchemyError as err:
error_message = f'Error while deleting application {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while deleting application %s', err)
raise SQLAlchemyError(err)
def get_by_uuid(self, application_uuid: str) -> Model:
"""
@ -91,17 +88,14 @@ class Application(Repository):
:param application_uuid: the application uuid
:return: Application with the provided application_uuid
"""
try:
result_set = self.session.execute(select(Model).where(
Model.application_uuid == application_uuid)
).first()
return result_set[0]
except SQLAlchemyError as err:
error_message = f'Error while fetching application by application_uuid {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while fetching application by application_uuid %s', err)
raise SQLAlchemyError(err)
except IndexError as err:
error_message = f'Error while fetching application, empty result {err}'
logging.error(error_message)
raise IndexError(error_message)
logging.error('Error while fetching application, empty result %s', err)
raise IndexError(err)

View File

@ -64,9 +64,8 @@ class City(Repository):
self.session.flush()
self.session.commit()
except SQLAlchemyError as err:
error_message = f'An error occurred while creating a city: {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('An error occurred while creating a city %s', err)
raise SQLAlchemyError(err)
def update(self, city_id: int, city: CityHub):
"""
@ -80,9 +79,8 @@ class City(Repository):
self.session.query(Model).filter(Model.id == city_id).update({'name': city.name, 'updated': now})
self.session.commit()
except SQLAlchemyError as err:
error_message = f'Error while updating city {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while updating city %s', err)
raise SQLAlchemyError(err)
def delete(self, city_id: int):
"""
@ -95,9 +93,8 @@ class City(Repository):
self.session.query(Model).filter(Model.id == city_id).delete()
self.session.commit()
except SQLAlchemyError as err:
error_message = f'Error while fetching city {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while fetching city %s', err)
raise SQLAlchemyError(err)
def get_by_user_id_application_id_and_name(self, user_id, application_id, city_name) -> Model:
"""
@ -116,9 +113,8 @@ class City(Repository):
result_set = result_set[0]
return result_set
except SQLAlchemyError as err:
error_message = f'Error while fetching city by name {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while fetching city by name %s', err)
raise SQLAlchemyError(err)
def get_by_user_id_and_application_id(self, user_id, application_id) -> [Model]:
"""
@ -133,6 +129,5 @@ class City(Repository):
)
return [r[0] for r in result_set]
except SQLAlchemyError as err:
error_message = f'Error while fetching city by name {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
logging.error('Error while fetching city by name %s', err)
raise SQLAlchemyError(err)

View File

@ -52,12 +52,12 @@ class CityObject(Repository):
logging.error(error_message)
raise SQLAlchemyError(error_message)
def update(self, city_id: int, building: Building) -> Union[Dict, None]:
def update(self, city_id: int, building: Building):
"""
Updates an application
:param city_id: the city id of the city owning the city object
:param building: the city object
:return:
:return: None
"""
try:
object_usage = ''
@ -77,9 +77,9 @@ class CityObject(Repository):
'updated': datetime.datetime.utcnow()})
self.session.commit()
except SQLAlchemyError as err:
logging.error('Error while updating city object: %s', err)
return {'message': 'Error occurred while updating application'}
return None
error_message = f'Error while updating city object {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
def delete(self, city_id: int, name: str):
"""
@ -92,9 +92,11 @@ class CityObject(Repository):
self.session.query(Model).filter(Model.city_id == city_id, Model.name == name).delete()
self.session.commit()
except SQLAlchemyError as err:
logging.error('Error while deleting application: %s', err)
error_message = f'Error while deleting application {err}'
logging.error(error_message)
raise SQLAlchemyError(error_message)
def get_by_name_and_city(self, name, city_id) -> Union[Model, None]:
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
@ -106,8 +108,12 @@ class CityObject(Repository):
_city_object = self.session.execute(select(Model).where(
Model.name == name, Model.city_id == city_id
)).first()
return _city_object[0]
except SQLAlchemyError as err:
logging.error('Error while fetching application by application_uuid: %s', err)
if _city_object is None:
return None
return _city_object[0]
logging.error('Error while fetching city object by name and city: %s', err)
raise SQLAlchemyError(err)
except IndexError as err:
logging.error('Error while fetching city object by name and city, empty result %s', err)
raise IndexError(err)

View File

@ -35,23 +35,19 @@ class SimulationResults(Repository):
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]:
def insert(self, name: str, values: str, city_id=None, city_object_id=None):
"""
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
:return: None
"""
if city_id is not None:
city = self._get_city(city_id)
if city is None:
return {'message': 'City does not exists'}
_ = self._get_city(city_id)
else:
city_object = self._get_city_object(city_object_id)
if city_object is None:
return {'message': 'City object does not exists'}
_ = self._get_city_object(city_object_id)
try:
simulation_result = Model(name=name,
values=values,
@ -60,20 +56,18 @@ class SimulationResults(Repository):
self.session.add(simulation_result)
self.session.flush()
self.session.commit()
return simulation_result
except SQLAlchemyError as err:
error_message = f'An error occurred while creating city_object: {err}'
logging.error(error_message)
return {'message': error_message}
logging.error('An error occurred while creating city_object %s', err)
SQLAlchemyError(err)
def update(self, name: str, values: str, city_id=None, city_object_id=None) -> Union[Dict, None]:
def update(self, name: str, values: str, city_id=None, city_object_id=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
:return: None
"""
try:
if city_id is not None:
@ -91,21 +85,18 @@ class SimulationResults(Repository):
})
self.session.commit()
else:
return {'message': 'Missing either city_id or city_object_id'}
raise NotImplementedError('Missing either city_id or city_object_id')
except SQLAlchemyError as err:
error_message = f'Error while updating city object: {err}'
logging.error(error_message)
return {'message': error_message}
return None
logging.error('Error while updating city object %s', err)
SQLAlchemyError(err)
def delete(self, name: str, city_id=None, city_object_id=None) -> Union[Dict, None]:
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 owning these simulation results
:return: [Dict, None]
:return: None
"""
try:
if city_id is not None:
@ -115,12 +106,12 @@ class SimulationResults(Repository):
self.session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).delete()
self.session.commit()
else:
return {'message': 'Missing either city_id or city_object_id'}
raise NotImplementedError('Missing either city_id or city_object_id')
except SQLAlchemyError as err:
logging.error('Error while deleting application: %s', err)
return None
def _get_city(self, city_id) -> [City, None]:
def _get_city(self, city_id) -> City:
"""
Fetch a city object based city id
:param city_id: a city identifier
@ -130,7 +121,7 @@ class SimulationResults(Repository):
return self.session.execute(select(City).where(City.id == city_id)).first()
except SQLAlchemyError as err:
logging.error('Error while fetching city by city_id: %s', err)
return None
raise SQLAlchemyError(err)
def _get_city_object(self, city_object_id) -> [CityObject]:
"""
@ -142,7 +133,7 @@ class SimulationResults(Repository):
return self.session.execute(select(CityObject).where(CityObject.id == city_object_id)).first()
except SQLAlchemyError as err:
logging.error('Error while fetching city by city_id: %s', err)
return None
SQLAlchemyError(err)
def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, result_names=None):
"""