from datetime import datetime from sqlalchemy import Boolean, DateTime, Float, Integer, String, JSON from sqlalchemy.orm import Mapped, mapped_column from app.models.base import Base, TimestampMixin class User(Base, TimestampMixin): __tablename__ = "users" id: Mapped[str] = mapped_column(String(32), primary_key=True) username: Mapped[str] = mapped_column(String(64), unique=True, index=True) email: Mapped[str | None] = mapped_column(String(255), unique=True, nullable=True) phone: Mapped[str | None] = mapped_column(String(20), unique=True, nullable=True) # 短信注册用户允许先没有密码,后续通过 /auth/set-password 设置。 hashed_password: Mapped[str | None] = mapped_column(String(128), nullable=True) avatar: Mapped[str | None] = mapped_column(String(512), nullable=True) credits: Mapped[float] = mapped_column(Float, default=0.0) is_active: Mapped[bool] = mapped_column(Boolean, default=True) is_admin: Mapped[bool] = mapped_column(Boolean, default=False) user_type: Mapped[str] = mapped_column(String(16), default="frontend") last_login_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True ) password_set_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True ) allowed_menus: Mapped[list | None] = mapped_column(JSON, nullable=True) @property def must_set_password(self) -> bool: return self.user_type == "frontend" and not self.hashed_password