Added user id reference to cities. Included updated timestamp

This commit is contained in:
Peter Yefi 2023-01-10 12:26:08 -05:00
parent 3dc74371f4
commit 6383a47aa0
4 changed files with 16 additions and 6 deletions

View File

@ -5,10 +5,11 @@ Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
from sqlalchemy import Column, Integer, String, Sequence
from sqlalchemy import Column, Integer, String, Sequence, ForeignKey
from sqlalchemy import DateTime, PickleType, Float
from persistence.db_config import Base
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship
import datetime
@ -29,7 +30,10 @@ class City(Base):
upper_corner = Column(JSONB, nullable=False)
hub_release = Column(String, nullable=False)
city_version = Column(Integer, nullable=False)
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship("User", back_populates="city")
created = Column(DateTime, default=datetime.datetime.utcnow)
updated = Column(DateTime, default=datetime.datetime.utcnow)
def __init__(self, city, name, srs_name, country_code, l_corner, u_corner):
self.city = city

View File

@ -12,6 +12,7 @@ import datetime
from sqlalchemy.orm import validates
import re
import enum
from sqlalchemy.orm import relationship
class UserRoles(enum.Enum):
@ -28,6 +29,7 @@ class User(Base):
email = Column(String, nullable=False, unique=True)
password = Column(String, nullable=False)
role = Column(Enum(UserRoles), nullable=False, default=UserRoles.HubReader)
cities = relationship("City", back_populates="user")
created = Column(DateTime, default=datetime.datetime.utcnow)
updated = Column(DateTime, default=datetime.datetime.utcnow)

View File

@ -15,6 +15,7 @@ import requests
from urllib3.exceptions import HTTPError
from typing import Union, Dict
from hub_logger import logger
import datetime
class CityRepo(BaseRepo):
@ -104,10 +105,11 @@ class CityRepo(BaseRepo):
try:
self.session.query(DBCity).filter(DBCity.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,
})
'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,
'updated': datetime.datetime.utcnow
})
self.session.commit()
except SQLAlchemyError as err:

View File

@ -13,6 +13,7 @@ from persistence.models import UserRoles
from helpers.auth import Auth
from typing import Union, Dict
from hub_logger import logger
import datetime
class UserRepo(BaseRepo):
@ -57,7 +58,8 @@ class UserRepo(BaseRepo):
try:
if Auth.validate_password(password):
self.session.query(User).filter(User.id == user_id) \
.update({'name': name, 'email': email, 'password': Auth.hash_password(password), 'role': role})
.update({'name': name, 'email': email, 'password': Auth.hash_password(password), 'role': role,
'updated': datetime.datetime.utcnow})
self.session.commit()
except SQLAlchemyError as err:
logger.error(f'Error while updating user: {err}')