From 953b6796e9c3fc6e2bf9f8dc3b387ac66b6cbaed Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 27 Nov 2023 07:00:39 +0100 Subject: [PATCH] correct persistence json read --- hub/persistence/db_control.py | 7 ++-- hub/persistence/repositories/city_object.py | 41 +++++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/hub/persistence/db_control.py b/hub/persistence/db_control.py index 4ac5f95a..e060559b 100644 --- a/hub/persistence/db_control.py +++ b/hub/persistence/db_control.py @@ -76,10 +76,9 @@ class DBControl: """ cities = self._city.get_by_user_id_application_id_and_scenario(user_id, application_id, scenario) c = [c[0].id for c in cities] - for city in cities: - result = self.building_info(name, city[0].id) - if result is not None: - return result + result = self._city_object.building_in_cities_info(name, c) + if result is not None: + return result return None def building_info(self, name, city_id) -> CityObject: diff --git a/hub/persistence/repositories/city_object.py b/hub/persistence/repositories/city_object.py index 3fd5c62c..bba01ef5 100644 --- a/hub/persistence/repositories/city_object.py +++ b/hub/persistence/repositories/city_object.py @@ -6,14 +6,15 @@ Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca """ import datetime import logging +from typing import Union -from sqlalchemy import select, or_ +from sqlalchemy import select from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session from hub.city_model_structure.building import Building -from hub.persistence.repository import Repository from hub.persistence.models import CityObject as Model +from hub.persistence.repository import Repository class CityObject(Repository): @@ -100,7 +101,41 @@ class CityObject(Repository): logging.error('Error while deleting application %s', err) raise SQLAlchemyError from err - def get_by_name_or_alias_and_city(self, name, city_id) -> Model: + def building_in_cities_info(self, name, cities): + """ + Fetch a city object based on name and city id + :param name: city object name + :param cities: city identifiers + :return: [CityObject] with the provided name or alias belonging to the city with id city_id + """ + try: + # search by name first + with Session(self.engine) as session: + city_object = session.execute(select(Model).where( + Model.name == name, Model.city_id.in_(cities)) + ).first() + if city_object is not None: + return city_object[0] + # name not found, so search by alias instead + city_objects = session.execute( + select(Model).where(Model.aliases.contains(name), Model.city_id.in_(cities)) + ).all() + for city_object in city_objects: + aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',') + for alias in aliases: + if alias == name: + # force the name as the alias + city_object[0].name = name + return city_object[0] + return None + except SQLAlchemyError as err: + logging.error('Error while fetching city object by name and city: %s', err) + raise SQLAlchemyError from err + except IndexError as err: + logging.error('Error while fetching city object by name and city, empty result %s', err) + raise IndexError from err + + def get_by_name_or_alias_and_city(self, name, city_id) -> Union[Model, None]: """ Fetch a city object based on name and city id :param name: city object name