From 4f4f9fd6ecf90c52d4ba3019f900dad39b8e48ef Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Thu, 3 Nov 2022 15:29:09 -0400 Subject: [PATCH] Included repository for persisting building --- .gitignore | 1 + db_migration.py | 2 +- persistence/Base.py | 17 ++++++++++++++ persistence/__init__.py | 3 +++ db_config.py => persistence/db_config.py | 0 persistence/models/Building.py | 14 ++++++++++-- persistence/models/City.py | 24 +++++++++++++++----- persistence/repositories/BuildingRepo.py | 25 +++++++++++++++++++++ persistence/repositories/CityRepo.py | 28 ++++++++++++++++++++++++ persistence/repositories/__init__.py | 1 + requirements.txt | 4 +++- 11 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 persistence/Base.py rename db_config.py => persistence/db_config.py (100%) create mode 100644 persistence/repositories/BuildingRepo.py create mode 100644 persistence/repositories/CityRepo.py diff --git a/.gitignore b/.gitignore index c1c7f0df..973bfa22 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /data/energy_systems/heat_pumps/*.csv /data/energy_systems/heat_pumps/*.insel .DS_Store +.env diff --git a/db_migration.py b/db_migration.py index 336328c5..459fcd09 100644 --- a/db_migration.py +++ b/db_migration.py @@ -6,7 +6,7 @@ Project Coder Peter Yefi peteryefi@gmail.com """ from sqlalchemy import create_engine -from db_config import BaseConfiguration +from persistence.db_config import BaseConfiguration from persistence.models import Building from persistence.models import City diff --git a/persistence/Base.py b/persistence/Base.py new file mode 100644 index 00000000..2fde7981 --- /dev/null +++ b/persistence/Base.py @@ -0,0 +1,17 @@ +""" +Base repository class to establish db connection +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Peter Yefi peteryefi@gmail.com +""" + +from persistence.db_config import BaseConfiguration +from sqlalchemy import create_engine +from sqlalchemy.orm import Session + + +class BaseRepo: + def __init__(self): + config = BaseConfiguration() + engine = create_engine(config.conn_string()) + self.session = Session(engine) diff --git a/persistence/__init__.py b/persistence/__init__.py index e69de29b..9b8c510a 100644 --- a/persistence/__init__.py +++ b/persistence/__init__.py @@ -0,0 +1,3 @@ +from .Base import BaseRepo +from .repositories.CityRepo import CityRepo +from .repositories.BuildingRepo import BuildingRepo \ No newline at end of file diff --git a/db_config.py b/persistence/db_config.py similarity index 100% rename from db_config.py rename to persistence/db_config.py diff --git a/persistence/models/Building.py b/persistence/models/Building.py index ad973d78..e2c1b145 100644 --- a/persistence/models/Building.py +++ b/persistence/models/Building.py @@ -7,13 +7,23 @@ Project Coder Peter Yefi peteryefi@gmail.com from sqlalchemy import Column, Integer, String, Sequence, Float, ForeignKey from sqlalchemy.dialects.postgresql import JSONB -from db_config import Base +from persistence.db_config import Base class Building(Base): + """A model representation of a building + + Attributes: + city_id A reference to the city which has this building. + name The name of the building. + construction_year The year of construction of the building. + function The function (e.g. residential) of the building. + floor_area The computed area of the floor of the building. + data A JSON object which contain other data (like roof, walls, zones, etc) of the building + """ __tablename__ = "building" id = Column(Integer, Sequence('building_id_seq'), primary_key=True) - city_id = Column(Integer, ForeignKey('models.id'), nullable=False) + city_id = Column(Integer, ForeignKey('city.id'), nullable=False) name = Column(String, nullable=False) construction_year = Column(Integer, nullable=False) function = Column(String, nullable=False) diff --git a/persistence/models/City.py b/persistence/models/City.py index 2e243304..284926a4 100644 --- a/persistence/models/City.py +++ b/persistence/models/City.py @@ -6,16 +6,28 @@ Project Coder Peter Yefi peteryefi@gmail.com """ from sqlalchemy import Column, Integer, String, Sequence, Float -from db_config import Base +from persistence.db_config import Base from sqlalchemy.orm import relationship class City(Base): - __tablename__ = "models" + """A model representation of a city + + Attributes: + name The name of the city. + uid A unique identifier for each city object + location The location of the building. + country_code The country where the city is located. + latitude The GPS latitude location of the city. + longitude The GPS longitude location of the city. + building A relationship for fetching all buildings in the city + """ + __tablename__ = "city" id = Column(Integer, Sequence('city_id_seq'), primary_key=True) + uid = Column(String, nullable=False, unique=True) name = Column(String, nullable=False) - location = Column(String, nullable=False) + time_zone = Column(String, nullable=True) country_code = Column(String, nullable=False) - latitude = Column(Float, nullable=False) - longitude = Column(Float, nullable=False) - building = relationship('Building', backref='models', lazy=True, cascade="all, delete-orphan") + latitude = Column(Float) + longitude = Column(Float) + building = relationship('Building', backref='city', lazy=True, cascade="all, delete-orphan") diff --git a/persistence/repositories/BuildingRepo.py b/persistence/repositories/BuildingRepo.py new file mode 100644 index 00000000..9a0979c1 --- /dev/null +++ b/persistence/repositories/BuildingRepo.py @@ -0,0 +1,25 @@ +""" +Building 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 +""" + +import json +from city_model_structure.building import Building +from persistence.models import City +from persistence import BaseRepo + + +class BuildingRepo(BaseRepo): + + def __init__(self): + super().__init__() + + def insert_building(self, building: [Building], city: City): + pass + + @staticmethod + def _extract_building_data(building: Building): + json_building = json.dumps(building.__dict__) + print(json_building) diff --git a/persistence/repositories/CityRepo.py b/persistence/repositories/CityRepo.py new file mode 100644 index 00000000..d15acef5 --- /dev/null +++ b/persistence/repositories/CityRepo.py @@ -0,0 +1,28 @@ +""" +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.models import City as ModelCity +from persistence import BaseRepo + + +class CityRepo(BaseRepo): + def __init__(self): + super().__init__() + + def insert(self, city: City): + model_city = ModelCity() + model_city.uid = city.uid + model_city.name = city.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 + self.session.add(model_city) + self.session.flush() + self.session.commit() + return model_city diff --git a/persistence/repositories/__init__.py b/persistence/repositories/__init__.py index e69de29b..8b137891 100644 --- a/persistence/repositories/__init__.py +++ b/persistence/repositories/__init__.py @@ -0,0 +1 @@ + diff --git a/requirements.txt b/requirements.txt index 5813e91f..6922c468 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,4 +15,6 @@ ply rhino3dm==7.7.0 scipy PyYAML -pyecore==0.12.2 \ No newline at end of file +pyecore==0.12.2 +python-dotenv +SQLAlchemy \ No newline at end of file