26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
"""Append-only audit trail for admin write actions."""
|
|
from datetime import datetime
|
|
from app.utils.time import utcnow
|
|
from sqlalchemy import JSON
|
|
from app.extensions import db
|
|
|
|
|
|
class AuditLog(db.Model):
|
|
__tablename__ = "audit_logs"
|
|
|
|
id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"),
|
|
primary_key=True, autoincrement=True)
|
|
actor_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"),
|
|
db.ForeignKey("users.id"), nullable=True, index=True)
|
|
action = db.Column(db.String(60), nullable=False, index=True)
|
|
target_type = db.Column(db.String(40), nullable=False)
|
|
target_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"),
|
|
nullable=True, index=True)
|
|
meta = db.Column(JSON, nullable=True)
|
|
created_at = db.Column(db.DateTime, default=utcnow, nullable=False)
|
|
|
|
actor = db.relationship("User", backref=db.backref("audit_logs", lazy="dynamic"))
|
|
|
|
def __repr__(self):
|
|
return f"<AuditLog {self.action} {self.target_type}:{self.target_id}>"
|