mirror of
https://github.com/Cccc-owo/CheckInApp.git
synced 2026-06-17 05:56:29 +00:00
refactor(backend): adopt typed SQLAlchemy models
This commit is contained in:
+46
-19
@@ -1,56 +1,83 @@
|
||||
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, Index
|
||||
from sqlalchemy.orm import relationship
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, Index, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
from backend.models.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from backend.models.check_in_task import CheckInTask
|
||||
|
||||
|
||||
class User(Base):
|
||||
"""用户模型 - 账户信息"""
|
||||
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
jwt_sub = Column(
|
||||
id: Mapped[int] = mapped_column(primary_key=True, index=True, autoincrement=True)
|
||||
jwt_sub: Mapped[str | None] = mapped_column(
|
||||
String(200),
|
||||
unique=True,
|
||||
nullable=True,
|
||||
index=True,
|
||||
comment="QQ 扫码登录的唯一用户标识(注册时为空)",
|
||||
)
|
||||
alias = Column(
|
||||
alias: Mapped[str] = mapped_column(
|
||||
String(50), unique=True, nullable=False, index=True, comment="用户别名(用于登录)"
|
||||
)
|
||||
email = Column(String(100), nullable=True, comment="用户邮箱(用于接收通知)")
|
||||
password_hash = Column(String(200), nullable=True, comment="密码哈希(bcrypt加密)")
|
||||
authorization = Column(Text, nullable=True, comment="当前有效的 QQ Token")
|
||||
jwt_exp = Column(String(20), default="0", comment="Token 过期时间戳")
|
||||
token_expiring_notified = Column(
|
||||
email: Mapped[str | None] = mapped_column(
|
||||
String(100), nullable=True, comment="用户邮箱(用于接收通知)"
|
||||
)
|
||||
password_hash: Mapped[str | None] = mapped_column(
|
||||
String(200), nullable=True, comment="密码哈希(bcrypt加密)"
|
||||
)
|
||||
authorization: Mapped[str | None] = mapped_column(
|
||||
Text, nullable=True, comment="当前有效的 QQ Token"
|
||||
)
|
||||
jwt_exp: Mapped[str] = mapped_column(String(20), default="0", comment="Token 过期时间戳")
|
||||
token_expiring_notified: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False,
|
||||
nullable=False,
|
||||
comment="Token 即将过期提醒是否已发送(过期前30分钟)",
|
||||
)
|
||||
token_expired_notified = Column(
|
||||
token_expired_notified: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False,
|
||||
nullable=False,
|
||||
comment="Token 已过期提醒是否已发送(过期后30分钟内)",
|
||||
)
|
||||
role = Column(String(20), default="user", index=True, comment="角色: user/admin")
|
||||
is_approved = Column(Boolean, default=False, index=True, comment="是否已通过管理员审批")
|
||||
role: Mapped[str] = mapped_column(
|
||||
String(20), default="user", index=True, comment="角色: user/admin"
|
||||
)
|
||||
is_approved: Mapped[bool] = mapped_column(
|
||||
Boolean, default=False, index=True, comment="是否已通过管理员审批"
|
||||
)
|
||||
|
||||
# 账户锁定相关字段
|
||||
failed_login_attempts = Column(Integer, default=0, nullable=False, comment="连续登录失败次数")
|
||||
locked_until = Column(DateTime(timezone=True), nullable=True, comment="账户锁定到期时间")
|
||||
last_failed_login = Column(
|
||||
failed_login_attempts: Mapped[int] = mapped_column(
|
||||
default=0, nullable=False, comment="连续登录失败次数"
|
||||
)
|
||||
locked_until: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True, comment="账户锁定到期时间"
|
||||
)
|
||||
last_failed_login: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True, comment="最后一次登录失败时间"
|
||||
)
|
||||
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), comment="创建时间"
|
||||
)
|
||||
updated_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), onupdate=func.now(), comment="更新时间"
|
||||
)
|
||||
|
||||
# 关联打卡任务
|
||||
tasks = relationship("CheckInTask", back_populates="user", cascade="all, delete-orphan")
|
||||
tasks: Mapped[list["CheckInTask"]] = relationship(
|
||||
back_populates="user", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
# 添加复合索引:加速审批管理查询
|
||||
__table_args__ = (
|
||||
|
||||
Reference in New Issue
Block a user