From c434b5593f76d400c3f7bf45b5cd9e971fbde90a Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Tue, 10 Jan 2023 20:30:55 -0500 Subject: [PATCH] fixed issued with role enum --- persistence/db_setup.py | 4 ++-- persistence/models/user.py | 6 +++--- persistence/repositories/city_repo.py | 2 +- persistence/repositories/user_repo.py | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/persistence/db_setup.py b/persistence/db_setup.py index bac44596..e182ed35 100644 --- a/persistence/db_setup.py +++ b/persistence/db_setup.py @@ -31,6 +31,6 @@ class DBSetup: if type(user) is dict: logger.info(user) else: - print(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin}') - logger.info(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin}') + print(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin.value}') + logger.info(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin.value}') print('Remember to change the admin default password and email address with the UserFactory') diff --git a/persistence/models/user.py b/persistence/models/user.py index 6529aa5b..e3eb0f95 100644 --- a/persistence/models/user.py +++ b/persistence/models/user.py @@ -16,8 +16,8 @@ from sqlalchemy.orm import relationship class UserRoles(enum.Enum): - Admin = 'ADMIN' - HubReader = 'HUB_READER' + Admin = 'Admin' + Hub_Reader = 'HUB_READER' class User(Base): @@ -28,7 +28,7 @@ class User(Base): name = Column(String, nullable=False) email = Column(String, nullable=False, unique=True) password = Column(String, nullable=False) - role = Column(Enum(UserRoles), nullable=False, default=UserRoles.HubReader) + role = Column(Enum(UserRoles), nullable=False, default=UserRoles.Hub_Reader) cities = relationship("City", back_populates="user") created = Column(DateTime, default=datetime.datetime.utcnow) updated = Column(DateTime, default=datetime.datetime.utcnow) diff --git a/persistence/repositories/city_repo.py b/persistence/repositories/city_repo.py index 8baaab68..5bea8099 100644 --- a/persistence/repositories/city_repo.py +++ b/persistence/repositories/city_repo.py @@ -108,7 +108,7 @@ class CityRepo(BaseRepo): '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 + 'updated': datetime.datetime.utcnow() }) self.session.commit() diff --git a/persistence/repositories/user_repo.py b/persistence/repositories/user_repo.py index da07b0fa..6abda5bf 100644 --- a/persistence/repositories/user_repo.py +++ b/persistence/repositories/user_repo.py @@ -45,7 +45,7 @@ class UserRepo(BaseRepo): else: return {'message': f'user with {email} email already exists'} - def update(self, user_id: int, name: str, email: str, password: str, role: UserRoles): + def update(self, user_id: int, name: str, email: str, password: str, role: UserRoles) -> Union[Dict, None]: """ Updates a user :param user_id: the id of the user to be updated @@ -59,10 +59,11 @@ class UserRepo(BaseRepo): 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, - 'updated': datetime.datetime.utcnow}) + 'updated': datetime.datetime.utcnow()}) self.session.commit() except SQLAlchemyError as err: logger.error(f'Error while updating user: {err}') + return {'err_msg': 'Error occured while updated user'} def get_by_email(self, email: str) -> [User]: """