mirror of
https://github.com/Cccc-owo/CheckInApp.git
synced 2026-06-17 05:56:29 +00:00
style(backend): apply ruff format
This commit is contained in:
@@ -10,21 +10,39 @@ class CheckInRecord(Base):
|
||||
__tablename__ = "check_in_records"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
task_id = Column(Integer, ForeignKey("check_in_tasks.id", ondelete="CASCADE"), nullable=False, index=True, comment="任务 ID")
|
||||
status = Column(String(20), nullable=False, index=True, comment="状态: success/failure/out_of_time/unknown/pending")
|
||||
task_id = Column(
|
||||
Integer,
|
||||
ForeignKey("check_in_tasks.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="任务 ID",
|
||||
)
|
||||
status = Column(
|
||||
String(20),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="状态: success/failure/out_of_time/unknown/pending",
|
||||
)
|
||||
response_text = Column(Text, default="", comment="响应文本")
|
||||
error_message = Column(Text, default="", comment="错误信息")
|
||||
location = Column(Text, default="{}", comment="位置信息 JSON")
|
||||
trigger_type = Column(String(50), default="scheduled", comment="触发类型: scheduled/manual/admin")
|
||||
check_in_time = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), index=True, comment="打卡时间(UTC)")
|
||||
trigger_type = Column(
|
||||
String(50), default="scheduled", comment="触发类型: scheduled/manual/admin"
|
||||
)
|
||||
check_in_time = Column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
index=True,
|
||||
comment="打卡时间(UTC)",
|
||||
)
|
||||
|
||||
# 关联任务
|
||||
task = relationship("CheckInTask", back_populates="check_in_records")
|
||||
|
||||
# 添加复合索引:加速常见查询
|
||||
__table_args__ = (
|
||||
Index('ix_record_task_time', 'task_id', 'check_in_time'), # 获取任务的打卡记录(按时间排序)
|
||||
Index('ix_record_status_time', 'status', 'check_in_time'), # 按状态和时间查询
|
||||
Index("ix_record_task_time", "task_id", "check_in_time"), # 获取任务的打卡记录(按时间排序)
|
||||
Index("ix_record_status_time", "status", "check_in_time"), # 按状态和时间查询
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
@@ -10,11 +10,27 @@ class CheckInTask(Base):
|
||||
__tablename__ = "check_in_tasks"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, comment="用户 ID")
|
||||
payload_config = Column(Text, default="{}", nullable=False, comment="完整的 payload 配置 JSON(从模板生成,包含 ThreadId 和所有字段)")
|
||||
user_id = Column(
|
||||
Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="用户 ID",
|
||||
)
|
||||
payload_config = Column(
|
||||
Text,
|
||||
default="{}",
|
||||
nullable=False,
|
||||
comment="完整的 payload 配置 JSON(从模板生成,包含 ThreadId 和所有字段)",
|
||||
)
|
||||
name = Column(String(100), default="", comment="任务名称(用户自定义)")
|
||||
is_active = Column(Boolean, default=True, comment="是否启用自动打卡(不影响手动打卡)")
|
||||
cron_expression = Column(String(100), default="0 20 * * *", nullable=True, comment="Crontab 表达式(NULL 表示禁用自动打卡,否则按表达式执行)")
|
||||
cron_expression = Column(
|
||||
String(100),
|
||||
default="0 20 * * *",
|
||||
nullable=True,
|
||||
comment="Crontab 表达式(NULL 表示禁用自动打卡,否则按表达式执行)",
|
||||
)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")
|
||||
|
||||
@@ -22,12 +38,14 @@ class CheckInTask(Base):
|
||||
user = relationship("User", back_populates="tasks")
|
||||
|
||||
# 关联打卡记录
|
||||
check_in_records = relationship("CheckInRecord", back_populates="task", cascade="all, delete-orphan")
|
||||
check_in_records = relationship(
|
||||
"CheckInRecord", back_populates="task", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
# 添加索引:加速查询
|
||||
__table_args__ = (
|
||||
Index('ix_task_user_active', 'user_id', 'is_active'),
|
||||
Index('ix_task_cron', 'cron_expression'), # 加速查询启用了定时打卡的任务
|
||||
Index("ix_task_user_active", "user_id", "is_active"),
|
||||
Index("ix_task_cron", "cron_expression"), # 加速查询启用了定时打卡的任务
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
@@ -24,7 +24,7 @@ def receive_load(target, context):
|
||||
"""在从数据库加载对象后,将所有 datetime 字段转换为 timezone-aware (UTC)"""
|
||||
for attr_name in dir(target):
|
||||
# 跳过私有属性和方法
|
||||
if attr_name.startswith('_'):
|
||||
if attr_name.startswith("_"):
|
||||
continue
|
||||
|
||||
try:
|
||||
|
||||
@@ -6,6 +6,7 @@ from backend.models.database import Base
|
||||
|
||||
class TaskTemplate(Base):
|
||||
"""打卡任务模板"""
|
||||
|
||||
__tablename__ = "task_templates"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
@@ -13,7 +14,12 @@ class TaskTemplate(Base):
|
||||
description = Column(Text, nullable=True, comment="模板描述")
|
||||
|
||||
# 父模板 ID(用于继承)
|
||||
parent_id = Column(Integer, ForeignKey("task_templates.id", ondelete="SET NULL"), nullable=True, comment="父模板 ID")
|
||||
parent_id = Column(
|
||||
Integer,
|
||||
ForeignKey("task_templates.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
comment="父模板 ID",
|
||||
)
|
||||
|
||||
# 字段配置(JSON 格式)
|
||||
field_config = Column(Text, nullable=False, comment="字段配置(JSON)")
|
||||
|
||||
@@ -10,21 +10,41 @@ class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
jwt_sub = Column(String(200), unique=True, nullable=True, index=True, comment="QQ 扫码登录的唯一用户标识(注册时为空)")
|
||||
alias = Column(String(50), unique=True, nullable=False, index=True, comment="用户别名(用于登录)")
|
||||
jwt_sub = Column(
|
||||
String(200),
|
||||
unique=True,
|
||||
nullable=True,
|
||||
index=True,
|
||||
comment="QQ 扫码登录的唯一用户标识(注册时为空)",
|
||||
)
|
||||
alias = 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(Boolean, default=False, nullable=False, comment="Token 即将过期提醒是否已发送(过期前30分钟)")
|
||||
token_expired_notified = Column(Boolean, default=False, nullable=False, comment="Token 已过期提醒是否已发送(过期后30分钟内)")
|
||||
token_expiring_notified = Column(
|
||||
Boolean,
|
||||
default=False,
|
||||
nullable=False,
|
||||
comment="Token 即将过期提醒是否已发送(过期前30分钟)",
|
||||
)
|
||||
token_expired_notified = 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="是否已通过管理员审批")
|
||||
|
||||
# 账户锁定相关字段
|
||||
failed_login_attempts = Column(Integer, default=0, nullable=False, comment="连续登录失败次数")
|
||||
locked_until = Column(DateTime(timezone=True), nullable=True, comment="账户锁定到期时间")
|
||||
last_failed_login = Column(DateTime(timezone=True), nullable=True, comment="最后一次登录失败时间")
|
||||
last_failed_login = 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="更新时间")
|
||||
@@ -34,7 +54,7 @@ class User(Base):
|
||||
|
||||
# 添加复合索引:加速审批管理查询
|
||||
__table_args__ = (
|
||||
Index('ix_user_role_approved', 'role', 'is_approved'), # 管理员查询待审批用户
|
||||
Index("ix_user_role_approved", "role", "is_approved"), # 管理员查询待审批用户
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
Reference in New Issue
Block a user