operation_log.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Text, JSON
  2. from sqlalchemy.orm import relationship
  3. from datetime import datetime
  4. from app.core.database import Base
  5. class OperationLog(Base):
  6. __tablename__ = "operation_logs"
  7. id = Column(Integer, primary_key=True, index=True)
  8. app_id = Column(Integer, ForeignKey("applications.id"), nullable=True)
  9. operator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
  10. # Action Type: MANUAL_ADD, DELETE, UPDATE, IMPORT
  11. action_type = Column(String(50), nullable=False, index=True)
  12. # Target info (optional, for single record ops)
  13. target_user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
  14. target_mobile = Column(String(20), nullable=True) # Snapshot for search
  15. ip_address = Column(String(50), nullable=True)
  16. # Details (JSON for structured data like import logs, or diffs)
  17. details = Column(JSON, nullable=True)
  18. created_at = Column(DateTime, default=datetime.now, nullable=False)
  19. # Relationships
  20. application = relationship("Application")
  21. operator = relationship("User", foreign_keys=[operator_id])
  22. target_user = relationship("User", foreign_keys=[target_user_id])
  23. @property
  24. def operator_mobile(self):
  25. return self.operator.mobile if self.operator else None