Corrections in persistence, distribution_systems_electrical_consumption property seems to be bugged
This commit is contained in:
parent
fe76d2fe3d
commit
31a4893ed8
@ -743,8 +743,10 @@ class Building(CityObject):
|
|||||||
|
|
||||||
for key, item in self._distribution_systems_electrical_consumption.items():
|
for key, item in self._distribution_systems_electrical_consumption.items():
|
||||||
for i in range(0, len(item)):
|
for i in range(0, len(item)):
|
||||||
self._distribution_systems_electrical_consumption[key][i] += _peak_load * _consumption_fix_flow \
|
self._distribution_systems_electrical_consumption[key][i] += (
|
||||||
* _working_hours[key] * cte.WATTS_HOUR_TO_JULES
|
_peak_load * _consumption_fix_flow * _working_hours[key] * cte.WATTS_HOUR_TO_JULES
|
||||||
|
)
|
||||||
|
|
||||||
return self._distribution_systems_electrical_consumption
|
return self._distribution_systems_electrical_consumption
|
||||||
|
|
||||||
def _calculate_consumption(self, consumption_type, demand):
|
def _calculate_consumption(self, consumption_type, demand):
|
||||||
|
@ -114,10 +114,7 @@ class DBControl:
|
|||||||
result_names = []
|
result_names = []
|
||||||
results = {}
|
results = {}
|
||||||
for scenario in request_values['scenarios']:
|
for scenario in request_values['scenarios']:
|
||||||
print('scenario', scenario, results)
|
|
||||||
for scenario_name in scenario.keys():
|
for scenario_name in scenario.keys():
|
||||||
print('scenario name', scenario_name)
|
|
||||||
|
|
||||||
result_sets = self._city.get_by_user_id_application_id_and_scenario(
|
result_sets = self._city.get_by_user_id_application_id_and_scenario(
|
||||||
user_id,
|
user_id,
|
||||||
application_id,
|
application_id,
|
||||||
@ -143,7 +140,6 @@ class DBControl:
|
|||||||
values = json.loads(value.values)
|
values = json.loads(value.values)
|
||||||
values["building"] = building_name
|
values["building"] = building_name
|
||||||
results[scenario_name].append(values)
|
results[scenario_name].append(values)
|
||||||
print(scenario, results)
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):
|
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):
|
||||||
|
@ -7,9 +7,10 @@ Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
|
||||||
from sqlalchemy import Column, Integer, String, Sequence
|
from sqlalchemy import Column, Integer, String, Sequence
|
||||||
from sqlalchemy import DateTime
|
from sqlalchemy import DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
|
||||||
from hub.persistence.configuration import Models
|
from hub.persistence.configuration import Models
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ class City(Models):
|
|||||||
pickle_path = Column(String, nullable=False)
|
pickle_path = Column(String, nullable=False)
|
||||||
name = Column(String, nullable=False)
|
name = Column(String, nullable=False)
|
||||||
scenario = Column(String, nullable=False)
|
scenario = Column(String, nullable=False)
|
||||||
application_id = Column(Integer, ForeignKey('application.id'), nullable=False)
|
application_id = Column(Integer, ForeignKey('application.id', ondelete='CASCADE'), nullable=False)
|
||||||
user_id = Column(Integer, ForeignKey('user.id'), nullable=True)
|
user_id = Column(Integer, ForeignKey('user.id', ondelete='CASCADE'), nullable=True)
|
||||||
hub_release = Column(String, nullable=False)
|
hub_release = Column(String, nullable=False)
|
||||||
created = Column(DateTime, default=datetime.datetime.utcnow)
|
created = Column(DateTime, default=datetime.datetime.utcnow)
|
||||||
updated = Column(DateTime, default=datetime.datetime.utcnow)
|
updated = Column(DateTime, default=datetime.datetime.utcnow)
|
||||||
|
@ -21,7 +21,7 @@ class CityObject(Models):
|
|||||||
"""
|
"""
|
||||||
__tablename__ = 'city_object'
|
__tablename__ = 'city_object'
|
||||||
id = Column(Integer, Sequence('city_object_id_seq'), primary_key=True)
|
id = Column(Integer, Sequence('city_object_id_seq'), primary_key=True)
|
||||||
city_id = Column(Integer, ForeignKey('city.id'), nullable=False)
|
city_id = Column(Integer, ForeignKey('city.id', ondelete='CASCADE'), nullable=False)
|
||||||
name = Column(String, nullable=False)
|
name = Column(String, nullable=False)
|
||||||
aliases = Column(String, nullable=True)
|
aliases = Column(String, nullable=True)
|
||||||
type = Column(String, nullable=False)
|
type = Column(String, nullable=False)
|
||||||
|
@ -19,8 +19,8 @@ class SimulationResults(Models):
|
|||||||
"""
|
"""
|
||||||
__tablename__ = 'simulation_results'
|
__tablename__ = 'simulation_results'
|
||||||
id = Column(Integer, Sequence('simulation_results_id_seq'), primary_key=True)
|
id = Column(Integer, Sequence('simulation_results_id_seq'), primary_key=True)
|
||||||
city_id = Column(Integer, ForeignKey('city.id'), nullable=True)
|
city_id = Column(Integer, ForeignKey('city.id', ondelete='CASCADE'), nullable=True)
|
||||||
city_object_id = Column(Integer, ForeignKey('city_object.id'), nullable=True)
|
city_object_id = Column(Integer, ForeignKey('city_object.id', ondelete='CASCADE'), nullable=True)
|
||||||
name = Column(String, nullable=False)
|
name = Column(String, nullable=False)
|
||||||
values = Column(JSONB, nullable=False)
|
values = Column(JSONB, nullable=False)
|
||||||
created = Column(DateTime, default=datetime.datetime.utcnow)
|
created = Column(DateTime, default=datetime.datetime.utcnow)
|
||||||
|
@ -10,6 +10,7 @@ import logging
|
|||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
from hub.persistence.repository import Repository
|
from hub.persistence.repository import Repository
|
||||||
from hub.persistence.models import Application as Model
|
from hub.persistence.models import Application as Model
|
||||||
@ -48,9 +49,10 @@ class Application(Repository):
|
|||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
application = Model(name=name, description=description, application_uuid=application_uuid)
|
application = Model(name=name, description=description, application_uuid=application_uuid)
|
||||||
self.session.add(application)
|
with Session(self.engine) as session:
|
||||||
self.session.commit()
|
session.add(application)
|
||||||
self.session.refresh(application)
|
session.commit()
|
||||||
|
session.refresh(application)
|
||||||
return application.id
|
return application.id
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('An error occurred while creating application %s', err)
|
logging.error('An error occurred while creating application %s', err)
|
||||||
@ -65,10 +67,11 @@ class Application(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(Model).filter(
|
with Session(self.engine) as session:
|
||||||
|
session.query(Model).filter(
|
||||||
Model.application_uuid == application_uuid
|
Model.application_uuid == application_uuid
|
||||||
).update({'name': name, 'description': description, 'updated': datetime.datetime.utcnow()})
|
).update({'name': name, 'description': description, 'updated': datetime.datetime.utcnow()})
|
||||||
self.session.commit()
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while updating application %s', err)
|
logging.error('Error while updating application %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -80,9 +83,10 @@ class Application(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(Model).filter(Model.application_uuid == application_uuid).delete()
|
with Session(self.engine) as session:
|
||||||
self.session.flush()
|
session.query(Model).filter(Model.application_uuid == application_uuid).delete()
|
||||||
self.session.commit()
|
session.flush()
|
||||||
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while deleting application %s', err)
|
logging.error('Error while deleting application %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -94,7 +98,8 @@ class Application(Repository):
|
|||||||
:return: Application with the provided application_uuid
|
:return: Application with the provided application_uuid
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result_set = self.session.execute(select(Model).where(
|
with Session(self.engine) as session:
|
||||||
|
result_set = session.execute(select(Model).where(
|
||||||
Model.application_uuid == application_uuid)
|
Model.application_uuid == application_uuid)
|
||||||
).first()
|
).first()
|
||||||
return result_set[0]
|
return result_set[0]
|
||||||
|
@ -54,17 +54,17 @@ class City(Repository):
|
|||||||
application_id,
|
application_id,
|
||||||
user_id,
|
user_id,
|
||||||
__version__)
|
__version__)
|
||||||
|
with Session(self.engine) as session:
|
||||||
self.session.add(db_city)
|
session.add(db_city)
|
||||||
self.session.flush()
|
session.flush()
|
||||||
self.session.commit()
|
session.commit()
|
||||||
for building in city.buildings:
|
for building in city.buildings:
|
||||||
db_city_object = CityObject(db_city.id,
|
db_city_object = CityObject(db_city.id,
|
||||||
building)
|
building)
|
||||||
self.session.add(db_city_object)
|
session.add(db_city_object)
|
||||||
self.session.flush()
|
session.flush()
|
||||||
self.session.commit()
|
session.commit()
|
||||||
self.session.refresh(db_city)
|
session.refresh(db_city)
|
||||||
return db_city.id
|
return db_city.id
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('An error occurred while creating a city %s', err)
|
logging.error('An error occurred while creating a city %s', err)
|
||||||
@ -79,8 +79,9 @@ class City(Repository):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
now = datetime.datetime.utcnow()
|
now = datetime.datetime.utcnow()
|
||||||
self.session.query(Model).filter(Model.id == city_id).update({'name': city.name, 'updated': now})
|
with Session(self.engine) as session:
|
||||||
self.session.commit()
|
session.query(Model).filter(Model.id == city_id).update({'name': city.name, 'updated': now})
|
||||||
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while updating city %s', err)
|
logging.error('Error while updating city %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -92,9 +93,10 @@ class City(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(CityObject).filter(CityObject.city_id == city_id).delete()
|
with Session(self.engine) as session:
|
||||||
self.session.query(Model).filter(Model.id == city_id).delete()
|
session.query(CityObject).filter(CityObject.city_id == city_id).delete()
|
||||||
self.session.commit()
|
session.query(Model).filter(Model.id == city_id).delete()
|
||||||
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching city %s', err)
|
logging.error('Error while fetching city %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -108,12 +110,11 @@ class City(Repository):
|
|||||||
:return: [ModelCity]
|
:return: [ModelCity]
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result_set = self.session.execute(select(Model).where(Model.user_id == user_id,
|
with Session(self.engine) as session:
|
||||||
|
result_set = session.execute(select(Model).where(Model.user_id == user_id,
|
||||||
Model.application_id == application_id,
|
Model.application_id == application_id,
|
||||||
Model.scenario == scenario
|
Model.scenario == scenario
|
||||||
)).all()
|
)).all()
|
||||||
self.session.close()
|
|
||||||
self.session = Session(self.engine)
|
|
||||||
return result_set
|
return result_set
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching city by name %s', err)
|
logging.error('Error while fetching city by name %s', err)
|
||||||
@ -127,7 +128,8 @@ class City(Repository):
|
|||||||
:return: ModelCity
|
:return: ModelCity
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result_set = self.session.execute(
|
with Session(self.engine) as session:
|
||||||
|
result_set = session.execute(
|
||||||
select(Model).where(Model.user_id == user_id, Model.application_id == application_id)
|
select(Model).where(Model.user_id == user_id, Model.application_id == application_id)
|
||||||
)
|
)
|
||||||
return [r[0] for r in result_set]
|
return [r[0] for r in result_set]
|
||||||
|
@ -46,10 +46,11 @@ class CityObject(Repository):
|
|||||||
try:
|
try:
|
||||||
city_object = Model(city_id=city_id,
|
city_object = Model(city_id=city_id,
|
||||||
building=building)
|
building=building)
|
||||||
self.session.add(city_object)
|
with Session(self.engine) as session:
|
||||||
self.session.flush()
|
session.add(city_object)
|
||||||
self.session.commit()
|
session.flush()
|
||||||
self.session.refresh(city_object)
|
session.commit()
|
||||||
|
session.refresh(city_object)
|
||||||
return city_object.id
|
return city_object.id
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('An error occurred while creating city_object %s', err)
|
logging.error('An error occurred while creating city_object %s', err)
|
||||||
@ -68,7 +69,8 @@ class CityObject(Repository):
|
|||||||
for usage in internal_zone.usages:
|
for usage in internal_zone.usages:
|
||||||
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
|
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
|
||||||
object_usage = object_usage.rstrip()
|
object_usage = object_usage.rstrip()
|
||||||
self.session.query(Model).filter(Model.name == building.name, Model.city_id == city_id).update(
|
with Session(self.engine) as session:
|
||||||
|
session.query(Model).filter(Model.name == building.name, Model.city_id == city_id).update(
|
||||||
{'name': building.name,
|
{'name': building.name,
|
||||||
'alias': building.alias,
|
'alias': building.alias,
|
||||||
'object_type': building.type,
|
'object_type': building.type,
|
||||||
@ -78,7 +80,7 @@ class CityObject(Repository):
|
|||||||
'volume': building.volume,
|
'volume': building.volume,
|
||||||
'area': building.floor_area,
|
'area': building.floor_area,
|
||||||
'updated': datetime.datetime.utcnow()})
|
'updated': datetime.datetime.utcnow()})
|
||||||
self.session.commit()
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while updating city object %s', err)
|
logging.error('Error while updating city object %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -91,8 +93,9 @@ class CityObject(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(Model).filter(Model.city_id == city_id, Model.name == name).delete()
|
with Session(self.engine) as session:
|
||||||
self.session.commit()
|
session.query(Model).filter(Model.city_id == city_id, Model.name == name).delete()
|
||||||
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while deleting application %s', err)
|
logging.error('Error while deleting application %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -106,15 +109,14 @@ class CityObject(Repository):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# search by name first
|
# search by name first
|
||||||
city_object = self.session.execute(select(Model).where(Model.name == name, Model.city_id == city_id)).first()
|
with Session(self.engine) as session:
|
||||||
|
city_object = session.execute(select(Model).where(Model.name == name, Model.city_id == city_id)).first()
|
||||||
if city_object is not None:
|
if city_object is not None:
|
||||||
return city_object[0]
|
return city_object[0]
|
||||||
# name not found, so search by alias instead
|
# name not found, so search by alias instead
|
||||||
city_objects = self.session.execute(
|
city_objects = session.execute(
|
||||||
select(Model).where(Model.aliases.contains(name), Model.city_id == city_id)
|
select(Model).where(Model.aliases.contains(name), Model.city_id == city_id)
|
||||||
).all()
|
).all()
|
||||||
self.session.close()
|
|
||||||
self.session = Session(self.engine)
|
|
||||||
for city_object in city_objects:
|
for city_object in city_objects:
|
||||||
aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',')
|
aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',')
|
||||||
for alias in aliases:
|
for alias in aliases:
|
||||||
|
@ -10,6 +10,7 @@ import logging
|
|||||||
from sqlalchemy import or_
|
from sqlalchemy import or_
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from hub.persistence.repository import Repository
|
from hub.persistence.repository import Repository
|
||||||
from hub.persistence.models import City
|
from hub.persistence.models import City
|
||||||
@ -52,10 +53,11 @@ class SimulationResults(Repository):
|
|||||||
values=values,
|
values=values,
|
||||||
city_id=city_id,
|
city_id=city_id,
|
||||||
city_object_id=city_object_id)
|
city_object_id=city_object_id)
|
||||||
self.session.add(simulation_result)
|
with Session(self.engine) as session:
|
||||||
self.session.flush()
|
session.add(simulation_result)
|
||||||
self.session.commit()
|
session.flush()
|
||||||
self.session.refresh(simulation_result)
|
session.commit()
|
||||||
|
session.refresh(simulation_result)
|
||||||
return simulation_result.id
|
return simulation_result.id
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('An error occurred while creating city_object %s', err)
|
logging.error('An error occurred while creating city_object %s', err)
|
||||||
@ -71,20 +73,21 @@ class SimulationResults(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with Session(self.engine) as session:
|
||||||
if city_id is not None:
|
if city_id is not None:
|
||||||
self.session.query(Model).filter(Model.name == name, Model.city_id == city_id).update(
|
session.query(Model).filter(Model.name == name, Model.city_id == city_id).update(
|
||||||
{
|
{
|
||||||
'values': values,
|
'values': values,
|
||||||
'updated': datetime.datetime.utcnow()
|
'updated': datetime.datetime.utcnow()
|
||||||
})
|
})
|
||||||
self.session.commit()
|
session.commit()
|
||||||
elif city_object_id is not None:
|
elif city_object_id is not None:
|
||||||
self.session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).update(
|
session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).update(
|
||||||
{
|
{
|
||||||
'values': values,
|
'values': values,
|
||||||
'updated': datetime.datetime.utcnow()
|
'updated': datetime.datetime.utcnow()
|
||||||
})
|
})
|
||||||
self.session.commit()
|
session.commit()
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError('Missing either city_id or city_object_id')
|
raise NotImplementedError('Missing either city_id or city_object_id')
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
@ -100,12 +103,13 @@ class SimulationResults(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
with Session(self.engine) as session:
|
||||||
if city_id is not None:
|
if city_id is not None:
|
||||||
self.session.query(Model).filter(Model.name == name, Model.city_id == city_id).delete()
|
session.query(Model).filter(Model.name == name, Model.city_id == city_id).delete()
|
||||||
self.session.commit()
|
session.commit()
|
||||||
elif city_object_id is not None:
|
elif city_object_id is not None:
|
||||||
self.session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).delete()
|
session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).delete()
|
||||||
self.session.commit()
|
session.commit()
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError('Missing either city_id or city_object_id')
|
raise NotImplementedError('Missing either city_id or city_object_id')
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
@ -119,7 +123,8 @@ class SimulationResults(Repository):
|
|||||||
:return: [City] with the provided city_id
|
:return: [City] with the provided city_id
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return self.session.execute(select(City).where(City.id == city_id)).first()
|
with Session(self.engine) as session:
|
||||||
|
return session.execute(select(City).where(City.id == city_id)).first()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching city by city_id: %s', err)
|
logging.error('Error while fetching city by city_id: %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -131,7 +136,8 @@ class SimulationResults(Repository):
|
|||||||
:return: [CityObject] with the provided city_object_id
|
:return: [CityObject] with the provided city_object_id
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return self.session.execute(select(CityObject).where(CityObject.id == city_object_id)).first()
|
with Session(self.engine) as session:
|
||||||
|
return session.execute(select(CityObject).where(CityObject.id == city_object_id)).first()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching city by city_id: %s', err)
|
logging.error('Error while fetching city by city_id: %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -145,7 +151,8 @@ class SimulationResults(Repository):
|
|||||||
:return: [SimulationResult]
|
:return: [SimulationResult]
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result_set = self.session.execute(select(Model).where(or_(
|
with Session(self.engine) as session:
|
||||||
|
result_set = session.execute(select(Model).where(or_(
|
||||||
Model.city_id == city_id,
|
Model.city_id == city_id,
|
||||||
Model.city_object_id == city_object_id
|
Model.city_object_id == city_object_id
|
||||||
)))
|
)))
|
||||||
|
@ -9,6 +9,7 @@ import logging
|
|||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from hub.helpers.auth import Auth
|
from hub.helpers.auth import Auth
|
||||||
from hub.persistence.repository import Repository
|
from hub.persistence.repository import Repository
|
||||||
@ -49,10 +50,11 @@ class User(Repository):
|
|||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
user = Model(name=name, password=Auth.hash_password(password), role=role, application_id=application_id)
|
user = Model(name=name, password=Auth.hash_password(password), role=role, application_id=application_id)
|
||||||
self.session.add(user)
|
with Session(self.engine) as session:
|
||||||
self.session.flush()
|
session.add(user)
|
||||||
self.session.commit()
|
session.flush()
|
||||||
self.session.refresh(user)
|
session.commit()
|
||||||
|
session.refresh(user)
|
||||||
return user.id
|
return user.id
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('An error occurred while creating user %s', err)
|
logging.error('An error occurred while creating user %s', err)
|
||||||
@ -68,13 +70,14 @@ class User(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(Model).filter(Model.id == user_id).update({
|
with Session(self.engine) as session:
|
||||||
|
session.query(Model).filter(Model.id == user_id).update({
|
||||||
'name': name,
|
'name': name,
|
||||||
'password': Auth.hash_password(password),
|
'password': Auth.hash_password(password),
|
||||||
'role': role,
|
'role': role,
|
||||||
'updated': datetime.datetime.utcnow()
|
'updated': datetime.datetime.utcnow()
|
||||||
})
|
})
|
||||||
self.session.commit()
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while updating user: %s', err)
|
logging.error('Error while updating user: %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -86,8 +89,9 @@ class User(Repository):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(Model).filter(Model.id == user_id).delete()
|
with Session(self.engine) as session:
|
||||||
self.session.commit()
|
session.query(Model).filter(Model.id == user_id).delete()
|
||||||
|
session.commit()
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching user: %s', err)
|
logging.error('Error while fetching user: %s', err)
|
||||||
raise SQLAlchemyError from err
|
raise SQLAlchemyError from err
|
||||||
@ -100,9 +104,11 @@ class User(Repository):
|
|||||||
:return: User matching the search criteria or None
|
:return: User matching the search criteria or None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
user = self.session.execute(
|
with Session(self.engine) as session:
|
||||||
|
user = session.execute(
|
||||||
select(Model).where(Model.name == name, Model.application_id == application_id)
|
select(Model).where(Model.name == name, Model.application_id == application_id)
|
||||||
).first()
|
).first()
|
||||||
|
session.commit()
|
||||||
return user[0]
|
return user[0]
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching user by name and application: %s', err)
|
logging.error('Error while fetching user by name and application: %s', err)
|
||||||
@ -120,7 +126,8 @@ class User(Repository):
|
|||||||
:return: User
|
:return: User
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
user = self.session.execute(
|
with Session(self.engine) as session:
|
||||||
|
user = session.execute(
|
||||||
select(Model).where(Model.name == name, Model.application_id == application_id)
|
select(Model).where(Model.name == name, Model.application_id == application_id)
|
||||||
).first()
|
).first()
|
||||||
if user:
|
if user:
|
||||||
@ -140,7 +147,8 @@ class User(Repository):
|
|||||||
:return: User
|
:return: User
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
application = self.session.execute(
|
with Session(self.engine) as session:
|
||||||
|
application = session.execute(
|
||||||
select(ApplicationModel).where(ApplicationModel.application_uuid == application_uuid)
|
select(ApplicationModel).where(ApplicationModel.application_uuid == application_uuid)
|
||||||
).first()
|
).first()
|
||||||
return self.get_by_name_application_id_and_password(name, password, application[0].id)
|
return self.get_by_name_application_id_and_password(name, password, application[0].id)
|
||||||
|
@ -6,7 +6,6 @@ Project Coder Peter Yefi peteryefi@gmail.com
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
from hub.persistence.configuration import Configuration
|
from hub.persistence.configuration import Configuration
|
||||||
|
|
||||||
|
|
||||||
@ -19,6 +18,5 @@ class Repository:
|
|||||||
try:
|
try:
|
||||||
self.configuration = Configuration(db_name, dotenv_path, app_env)
|
self.configuration = Configuration(db_name, dotenv_path, app_env)
|
||||||
self.engine = create_engine(self.configuration.connection_string)
|
self.engine = create_engine(self.configuration.connection_string)
|
||||||
self.session = Session(self.engine)
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logging.error('Missing value for credentials: %s', err)
|
logging.error('Missing value for credentials: %s', err)
|
||||||
|
@ -103,16 +103,16 @@ class Control:
|
|||||||
app_env='TEST',
|
app_env='TEST',
|
||||||
dotenv_path=dotenv_path)
|
dotenv_path=dotenv_path)
|
||||||
|
|
||||||
self._application_uuid = '60b7fc1b-f389-4254-9ffd-22a4cf32c7a3'
|
self._application_uuid = 'b9e0ce80-1218-410c-8a64-9d9b7026aad8'
|
||||||
self._application_id = 1
|
self._application_id = 1
|
||||||
self._user_id = 1
|
self._user_id = 1
|
||||||
|
|
||||||
self._application_id = self._database.persist_application(
|
self._application_id = self._database.persist_application(
|
||||||
'City_layers',
|
'test',
|
||||||
'City layers test user',
|
'test',
|
||||||
self.application_uuid
|
self.application_uuid
|
||||||
)
|
)
|
||||||
self._user_id = self._database.create_user('city_layers', self._application_id, 'city_layers', UserRoles.Admin)
|
self._user_id = self._database.create_user('test', self._application_id, 'test', UserRoles.Admin)
|
||||||
|
|
||||||
self._pickle_path = Path('tests_data/pickle_path.bz2').resolve()
|
self._pickle_path = Path('tests_data/pickle_path.bz2').resolve()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user