2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
Model representation of a User
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
|
|
|
|
|
|
|
from sqlalchemy import Column, Integer, String, Sequence
|
|
|
|
from sqlalchemy import DateTime, Enum
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.persistence.db_config import Base
|
2022-12-08 20:54:47 -05:00
|
|
|
import datetime
|
|
|
|
from sqlalchemy.orm import validates
|
|
|
|
import re
|
|
|
|
import enum
|
2023-01-10 12:26:08 -05:00
|
|
|
from sqlalchemy.orm import relationship
|
2022-12-08 20:54:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
class UserRoles(enum.Enum):
|
2023-01-10 20:30:55 -05:00
|
|
|
Admin = 'Admin'
|
2023-01-13 18:42:11 -05:00
|
|
|
Hub_Reader = 'Hub_Reader'
|
2022-12-08 20:54:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
class User(Base):
|
|
|
|
"""A model representation of a city
|
|
|
|
"""
|
|
|
|
__tablename__ = "user"
|
|
|
|
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
|
|
|
|
name = Column(String, nullable=False)
|
|
|
|
email = Column(String, nullable=False, unique=True)
|
|
|
|
password = Column(String, nullable=False)
|
2023-01-10 20:30:55 -05:00
|
|
|
role = Column(Enum(UserRoles), nullable=False, default=UserRoles.Hub_Reader)
|
2023-01-10 12:26:08 -05:00
|
|
|
cities = relationship("City", back_populates="user")
|
2022-12-08 20:54:47 -05:00
|
|
|
created = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
updated = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
|
|
|
|
@validates("email")
|
|
|
|
def validate_email(self, key, address):
|
|
|
|
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
|
|
|
|
if not re.match(pattern, address):
|
|
|
|
raise ValueError("failed simple email validation")
|
|
|
|
return address
|
|
|
|
|
|
|
|
def __init__(self, name, email, password, role):
|
|
|
|
self.name = name
|
|
|
|
self.email = email
|
|
|
|
self.password = password
|
|
|
|
self.role = role
|