from __future__ import annotations import json from flask_login import current_user from ..extensions import db from ..models import AuditLog def log_action(action: str, target_type: str = '', target_id: str = '', **details) -> None: user_id = current_user.id if getattr(current_user, 'is_authenticated', False) else None entry = AuditLog( user_id=user_id, action=action, target_type=target_type, target_id=str(target_id or ''), details=json.dumps(details, ensure_ascii=False) if details else '', ) db.session.add(entry)