26 lines
1.3 KiB
Python
26 lines
1.3 KiB
Python
from app import db
|
|
from datetime import datetime
|
|
|
|
class AuditLog(db.Model):
|
|
"""
|
|
Bitácora inmutable de auditoría para registro de eventos críticos
|
|
y trazabilidad de operaciones institucionales.
|
|
"""
|
|
__tablename__ = 'audit_logs'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
user_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='SET NULL'), nullable=True)
|
|
user_email = db.Column(db.String(255), nullable=True)
|
|
action = db.Column(db.String(50), nullable=False, index=True) # CREATE, UPDATE, DELETE, LOGIN, LOGOUT
|
|
module = db.Column(db.String(50), nullable=False, index=True) # classrooms, reservations, auth, users
|
|
entity_id = db.Column(db.Integer, nullable=True) # ID del registro afectado
|
|
details = db.Column(db.Text, nullable=True) # Detalles de cambios o parámetros
|
|
ip_address = db.Column(db.String(45), nullable=True) # Soporta IPv4 e IPv6
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False, index=True)
|
|
|
|
# Relación con usuario
|
|
user = db.relationship('User', foreign_keys=[user_id], backref=db.backref('audit_records', lazy='dynamic'))
|
|
|
|
def __repr__(self):
|
|
return f"<AuditLog #{self.id} {self.action} on {self.module} by {self.user_email or 'System'}>"
|