history.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. 历史记录工具函数(客户端后端仅保留会话/配置/nonce 相关)
  3. """
  4. from datetime import datetime
  5. from app.utils.timezone import beijing_now_naive
  6. from sqlalchemy.orm import Session as DBSession
  7. from app.models import (
  8. Session as SessionModel,
  9. SessionHis,
  10. SteamAccount,
  11. SteamAccountHis,
  12. SystemConfig,
  13. SystemConfigHis,
  14. APINonce,
  15. APINonceHis,
  16. )
  17. def create_session_history(
  18. db: DBSession,
  19. session: SessionModel,
  20. operation: str,
  21. expires_at_before: datetime = None,
  22. ):
  23. """创建会话历史记录
  24. Args:
  25. db: 数据库会话
  26. session: 会话对象
  27. operation: 操作类型 (INSERT/UPDATE/DELETE/RENEW)
  28. expires_at_before: RENEW操作时,记录续期前的过期时间
  29. """
  30. his_record = SessionHis(
  31. id=session.id,
  32. session_id=session.session_id,
  33. user_id=session.user_id,
  34. machine_code=session.machine_code,
  35. is_active=session.is_active,
  36. expires_at=session.expires_at,
  37. created_at=session.created_at,
  38. updated_at=session.updated_at,
  39. his_created_at=beijing_now_naive(),
  40. his_operation=operation,
  41. )
  42. db.add(his_record)
  43. def create_steam_account_history(db: DBSession, steam_account: SteamAccount, operation: str):
  44. """创建Steam账号历史记录(先写 his 再改/删时调用)"""
  45. his_record = SteamAccountHis(
  46. id=steam_account.id,
  47. user_id=steam_account.user_id,
  48. steam_username=steam_account.steam_username,
  49. steam_id=steam_account.steam_id,
  50. is_active=steam_account.is_active,
  51. is_deleted=steam_account.is_deleted,
  52. created_at=steam_account.created_at,
  53. updated_at=steam_account.updated_at,
  54. his_created_at=beijing_now_naive(),
  55. his_operation=operation,
  56. )
  57. db.add(his_record)
  58. def create_system_config_history(db: DBSession, system_config: SystemConfig, operation: str):
  59. """创建系统配置历史记录"""
  60. his_record = SystemConfigHis(
  61. id=system_config.id,
  62. config_key=system_config.config_key,
  63. config_value=system_config.config_value,
  64. config_type=system_config.config_type,
  65. app_name=system_config.app_name,
  66. description=system_config.description,
  67. is_system=system_config.is_system,
  68. is_editable=system_config.is_editable,
  69. is_deleted=system_config.is_deleted,
  70. created_at=system_config.created_at,
  71. updated_at=system_config.updated_at,
  72. his_created_at=beijing_now_naive(),
  73. his_operation=operation,
  74. )
  75. db.add(his_record)
  76. def create_api_nonce_history(db: DBSession, api_nonce: APINonce, operation: str):
  77. """创建API Nonce历史记录"""
  78. his_record = APINonceHis(
  79. id=api_nonce.id,
  80. nonce=api_nonce.nonce,
  81. endpoint=api_nonce.endpoint,
  82. session_id=api_nonce.session_id,
  83. created_at=api_nonce.created_at,
  84. expires_at=api_nonce.expires_at,
  85. his_created_at=beijing_now_naive(),
  86. his_operation=operation,
  87. )
  88. db.add(his_record)