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 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 sqlalchemy import DateTime, PickleType, Float
from persistence.db_config import Base from persistence.db_config import Base
from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship
import datetime import datetime
@ -29,7 +30,10 @@ class City(Base):
upper_corner = Column(JSONB, nullable=False) upper_corner = Column(JSONB, nullable=False)
hub_release = Column(String, nullable=False) hub_release = Column(String, nullable=False)
city_version = Column(Integer, 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) 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): def __init__(self, city, name, srs_name, country_code, l_corner, u_corner):
self.city = city self.city = city

View File

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

View File

@ -15,6 +15,7 @@ import requests
from urllib3.exceptions import HTTPError from urllib3.exceptions import HTTPError
from typing import Union, Dict from typing import Union, Dict
from hub_logger import logger from hub_logger import logger
import datetime
class CityRepo(BaseRepo): class CityRepo(BaseRepo):
@ -107,6 +108,7 @@ class CityRepo(BaseRepo):
'name': city.name, 'srs_name': city.srs_name, 'country_code': city.country_code, 'longitude': city.longitude, '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(), '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, 'upper_corner': city.upper_corner.tolist(), 'climate_reference_city': city.climate_reference_city,
'updated': datetime.datetime.utcnow
}) })
self.session.commit() self.session.commit()

View File

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