121 lines
4.2 KiB
Python
121 lines
4.2 KiB
Python
"""
|
|
City 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
|
|
"""
|
|
|
|
from city_model_structure.city import City
|
|
from persistence import BaseRepo
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy import select
|
|
from helpers.city_util import CityUtil
|
|
from persistence.models import City
|
|
import pickle
|
|
import requests
|
|
from urllib3.exceptions import HTTPError
|
|
from typing import Union, Dict
|
|
|
|
|
|
class CityRepo(BaseRepo):
|
|
def __init__(self, db_name):
|
|
super().__init__(db_name)
|
|
self._city_util = CityUtil()
|
|
|
|
def insert(self, city: City) -> Union[City, Dict]:
|
|
model_city = City()
|
|
model_city.name = city.name
|
|
model_city.climate_reference_city = city.climate_reference_city
|
|
model_city.srs_name = city.srs_name
|
|
model_city.longitude = city.longitude
|
|
model_city.latitude = city.latitude
|
|
model_city.country_code = city.country_code
|
|
model_city.time_zone = city.time_zone
|
|
model_city.lower_corner = city.lower_corner.tolist()
|
|
model_city.upper_corner = city.upper_corner.tolist()
|
|
model_city.city = pickle.dumps(city)
|
|
|
|
try:
|
|
# Retrieve hub project latest release
|
|
response = requests.get("https://rs-loy-gitlab.concordia.ca/api/v4/projects/2/repository/branches/master",
|
|
headers={"PRIVATE-TOKEN": self.config.hub_token})
|
|
recent_commit = response.json()["commit"]["id"]
|
|
exiting_city = self._get_by_hub_version(recent_commit, city.name)
|
|
|
|
# Do not persist the same city for the same version of Hub
|
|
if exiting_city is None:
|
|
model_city.hub_release = recent_commit
|
|
cities = self.get_by_name(city.name)
|
|
# update version for the same city but different hub versions
|
|
if len(cities) == 0:
|
|
model_city.city_version = 0
|
|
else:
|
|
model_city.city_version = cities[-1].city_version + 1
|
|
|
|
# Persist city
|
|
self.session.add(model_city)
|
|
self.session.flush()
|
|
self.session.commit()
|
|
return model_city
|
|
else:
|
|
return {'message': f'Same version of {city.name} exist'}
|
|
except SQLAlchemyError as err:
|
|
print(f'Error while adding city: {err}')
|
|
except HTTPError as err:
|
|
print(f'Error retrieving Hub latest release: {err}')
|
|
|
|
def get_by_id(self, city_id: int) -> City:
|
|
"""
|
|
Fetch a City based on the id
|
|
:param city_id: the city id
|
|
:return: a city
|
|
"""
|
|
try:
|
|
return self.session.execute(select(City).where(City.id == city_id)).first()[0]
|
|
except SQLAlchemyError as err:
|
|
print(f'Error while fetching city: {err}')
|
|
|
|
def _get_by_hub_version(self, hub_commit: str, city_name: str) -> City:
|
|
"""
|
|
Fetch a City based on the name and hub project recent commit
|
|
:param hub_commit: the latest hub commit
|
|
:param city_name: the name of the city
|
|
:return: a city
|
|
"""
|
|
try:
|
|
return self.session.execute(select(City)
|
|
.where(City.hub_release == hub_commit, City.name == city_name)).first()
|
|
except SQLAlchemyError as err:
|
|
print(f'Error while fetching city: {err}')
|
|
|
|
def update(self, city_id: int, city: City):
|
|
"""
|
|
Updates a city
|
|
:param city_id: the id of the city to be updated
|
|
:param city: the city object
|
|
:return:
|
|
"""
|
|
try:
|
|
self.session.query(City).filter(City.id == city_id) \
|
|
.update({
|
|
'name': city.name, 'srs_name': city.srs_name, 'country_code': city.country_code, 'longitude': city.longitude,
|
|
'latitude': city.latitude, 'time_zone': city.time_zone, 'lower_corner': city.lower_corner.tolist(),
|
|
'upper_corner': city.upper_corner.tolist(), 'climate_reference_city': city.climate_reference_city,
|
|
})
|
|
|
|
self.session.commit()
|
|
except SQLAlchemyError as err:
|
|
print(f'Error while updating city: {err}')
|
|
|
|
def get_by_name(self, city_name: str) -> [City]:
|
|
"""
|
|
Fetch city based on the name
|
|
:param city_name: the name of the building
|
|
:return: [ModelCity] with the provided name
|
|
"""
|
|
try:
|
|
result_set = self.session.execute(select(City).where(City.name == city_name))
|
|
return [building[0] for building in result_set]
|
|
except SQLAlchemyError as err:
|
|
print(f'Error while fetching city by name: {err}')
|