41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
"""This module defines the SQL data model for users."""
|
|
|
|
import enum
|
|
|
|
from sqlalchemy import Column, Integer, String, DateTime
|
|
from sqlalchemy.sql.functions import now
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from todo.database.engine import Base
|
|
|
|
|
|
class User(Base):
|
|
"""Model for the users table."""
|
|
|
|
__tablename__ = "users"
|
|
|
|
id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
|
|
email = Column('email', String, unique=True, nullable=False, index=True)
|
|
password = Column('password', String, nullable=False)
|
|
created = Column('created', DateTime(timezone=True), nullable=False, server_default=now())
|
|
updated = Column('updated', DateTime(timezone=True), nullable=False, server_default=now(), onupdate=now())
|
|
first_name = Column('first_name', String, nullable=False)
|
|
last_name = Column('last_name', String, nullable=False, index=True)
|
|
|
|
todo_items = relationship("TodoItem", back_populates="user", uselist=True, cascade="all, delete")
|
|
|
|
|
|
class SortableUserField(enum.Enum):
|
|
"""Defines which fields user lists can be sorted on."""
|
|
|
|
id = 'id'
|
|
email = 'email'
|
|
created = 'created'
|
|
updated = 'updated'
|
|
first_name = 'first_name'
|
|
last_name = 'last_name'
|
|
|
|
@property
|
|
def field(self) -> Column:
|
|
return getattr(User, self.value)
|