Add Activity log Service and model
This commit is contained in:
59
app/routes/activity_routes.py
Normal file
59
app/routes/activity_routes.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from flask import Blueprint, render_template, request, send_file, abort
|
||||
import os
|
||||
|
||||
from app.utils.helpers import login_required
|
||||
from app.utils.file_utils import get_logs_folder , ALLOWED_LOG_FILE
|
||||
from app.services.activity_service import ActivityService
|
||||
|
||||
activity_bp = Blueprint("activity", __name__, url_prefix="/activity")
|
||||
|
||||
# call activity_log page
|
||||
@activity_bp.route("/")
|
||||
@login_required
|
||||
def activity():
|
||||
file_name = request.args.get("file", "app.log")
|
||||
search = request.args.get("search", "")
|
||||
level = request.args.get("level", "").upper()
|
||||
user = request.args.get("user", "")
|
||||
from_date = request.args.get("from_date", "")
|
||||
to_date = request.args.get("to_date", "")
|
||||
|
||||
logs = ActivityService.read_logs(
|
||||
file_name=file_name,
|
||||
search=search,
|
||||
level=level,
|
||||
user=user,
|
||||
from_date=from_date,
|
||||
to_date=to_date
|
||||
)
|
||||
|
||||
return render_template(
|
||||
"activity/activity_log.html",
|
||||
logs=logs,
|
||||
file_name=file_name,
|
||||
search=search,
|
||||
level=level,
|
||||
user=user,
|
||||
from_date=from_date,
|
||||
to_date=to_date
|
||||
)
|
||||
|
||||
# Download activity_log files
|
||||
@activity_bp.route("/logs")
|
||||
@login_required
|
||||
def download_log():
|
||||
|
||||
filename = request.args.get("file", "app.log")
|
||||
if filename not in ALLOWED_LOG_FILE:
|
||||
abort(400, "Invalid log file.")
|
||||
|
||||
file_path = os.path.join(get_logs_folder(), filename)
|
||||
if not os.path.isfile(file_path):
|
||||
abort(404, "Log file not found.")
|
||||
|
||||
return send_file(
|
||||
file_path,
|
||||
as_attachment=True,
|
||||
download_name=filename,
|
||||
mimetype="text/plain"
|
||||
)
|
||||
Reference in New Issue
Block a user