"""Models for authentication.""" from datetime import datetime from pathlib import Path import sqlalchemy as sa from sqlalchemy.engine import URL from sqlmodel import SQLModel, Field, create_engine class User(SQLModel, table=True): """Users.""" username: str = Field(unique=True, primary_key=True) hashed_password: str disabled: bool = Field(default=False) created_at: datetime | None = Field( default=None, sa_type=sa.DateTime(timezone=True), sa_column_kwargs={"server_default": sa.func.now()}, nullable=False, ) class PasswordDB(SQLModel, table=True): """Password database.""" id: int | None = Field(default=None, primary_key=True) encrypted_password: str created_at: datetime | None = Field( default=None, sa_type=sa.DateTime(timezone=True), sa_column_kwargs={"server_default": sa.func.now()}, nullable=False, ) updated_at: datetime | None = Field( default=None, sa_type=sa.DateTime(timezone=True), sa_column_kwargs={"onupdate": sa.func.now(), "server_default": sa.func.now()}, ) def get_engine(filename: Path, echo: bool = False) -> sa.Engine: """Initialize the engine.""" url = URL.create(drivername="sqlite", database=str(filename.absolute())) engine = create_engine(url, echo=echo) return engine