Telezab/utilities/web_logger.py

52 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime
from models import WebActionLog
from flask_login import current_user
from utilities.database import db # Убедитесь, что импортируете db
class WebLogger:
def __init__(self, db_session):
self.db = db_session
def log_web_action(self, action, details=None):
"""Сохраняет лог действия пользователя веб-интерфейса."""
if current_user.is_authenticated:
log_entry = WebActionLog(
ldap_user_id=current_user.id,
username=getattr(current_user, 'display_name', None),
action=action,
details=details
)
self.db.session.add(log_entry)
self.db.session.commit()
return True
return False
def get_web_action_logs(self, page, per_page, ldap_user_id_filter=None, action_filter=None):
"""Получает логи действий веб-интерфейса с пагинацией и фильтрацией."""
query = WebActionLog.query.order_by(WebActionLog.timestamp.desc())
if ldap_user_id_filter:
query = query.filter_by(ldap_user_id=ldap_user_id_filter)
if action_filter:
query = query.filter(WebActionLog.action.like(f'%{action_filter}%'))
pagination = query.paginate(page=page, per_page=per_page)
logs = [
{
'id': log.id,
'ldap_user_id': log.ldap_user_id,
'username': log.username,
'timestamp': log.timestamp.isoformat(),
'action': log.action,
'details': log.details
}
for log in pagination.items
]
return {
'logs': logs,
'total': pagination.total,
'pages': pagination.pages,
'current_page': pagination.page,
'per_page': pagination.per_page
}