admin.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. 后台管理系统ORM定义
  3. 定义管理员用户、操作日志和登录尝试记录的数据库表结构
  4. Requirements: 13.2, 13.3
  5. """
  6. from sqlalchemy import Column, Integer, BigInteger, String, Text, DateTime, Boolean, ForeignKey, Index
  7. from sqlalchemy.dialects.postgresql import JSON
  8. from sqlalchemy.sql import func
  9. import json
  10. from app.database import Base
  11. class AdminUser(Base):
  12. """
  13. 管理员用户ORM类
  14. """
  15. __tablename__ = "admin_users"
  16. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
  17. username = Column(String(50), unique=True, nullable=False, index=True, comment="用户名")
  18. password_hash = Column(String(255), nullable=False, comment="密码哈希")
  19. nickname = Column(String(100), nullable=False, comment="昵称")
  20. status = Column(String(20), nullable=False, default="active", comment="状态:active/disabled")
  21. created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
  22. updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")
  23. __table_args__ = (
  24. Index('idx_admin_users_status', 'status'),
  25. {'schema': 'aigcspace', 'comment': '管理员用户表'}
  26. )
  27. def __repr__(self):
  28. return f"<AdminUser(id={self.id}, username='{self.username}', nickname='{self.nickname}')>"
  29. class OperationLog(Base):
  30. """
  31. 操作日志ORM类
  32. """
  33. __tablename__ = "operation_log"
  34. id = Column(BigInteger, primary_key=True, autoincrement=True, comment="主键ID")
  35. admin_id = Column(Integer, ForeignKey("aigcspace.admin_users.id"), nullable=False, comment="管理员ID")
  36. operation_type = Column(String(50), nullable=False, comment="操作类型:create/update/delete/login")
  37. module = Column(String(50), nullable=False, comment="操作模块:user/model/config")
  38. target_id = Column(String(100), nullable=True, comment="操作对象ID")
  39. detail = Column(JSON, nullable=True, comment="操作详情(JSON格式)")
  40. ip_address = Column(String(50), nullable=True, comment="IP地址")
  41. created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
  42. __table_args__ = (
  43. Index('idx_operation_log_admin_id', 'admin_id'),
  44. Index('idx_operation_log_operation_type', 'operation_type'),
  45. Index('idx_operation_log_module', 'module'),
  46. Index('idx_operation_log_created_at', 'created_at'),
  47. {'schema': 'aigcspace', 'comment': '管理员操作日志表'}
  48. )
  49. def __repr__(self):
  50. return f"<OperationLog(id={self.id}, admin_id={self.admin_id}, type='{self.operation_type}', module='{self.module}')>"
  51. class AdminLoginAttempt(Base):
  52. """
  53. 管理员登录尝试记录ORM类
  54. """
  55. __tablename__ = "admin_login_attempt"
  56. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
  57. username = Column(String(50), nullable=False, index=True, comment="用户名")
  58. success = Column(Boolean, nullable=False, comment="是否成功")
  59. ip_address = Column(String(50), nullable=True, comment="IP地址")
  60. created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
  61. __table_args__ = (
  62. Index('idx_admin_login_attempt_created_at', 'created_at'),
  63. {'schema': 'aigcspace', 'comment': '管理员登录尝试记录表'}
  64. )
  65. def __repr__(self):
  66. return f"<AdminLoginAttempt(id={self.id}, username='{self.username}', success={self.success})>"