From 469cef48aa8baf3cac8ec98cfefead536c576a91 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 14 Aug 2026 18:02:32 +0000 Subject: [PATCH] Add admin action to clear watchdog logs --- app_new/logs/routes.py | 14 +++++++++++++- app_new/logs/templates/logs/index.html | 12 +++++++++++- tests/integration/test_watchdog_logs.py | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/integration/test_watchdog_logs.py diff --git a/app_new/logs/routes.py b/app_new/logs/routes.py index c74b92a..86513bd 100644 --- a/app_new/logs/routes.py +++ b/app_new/logs/routes.py @@ -1,8 +1,10 @@ """Logs centralises des watchdogs.""" -from flask import Blueprint, render_template, request, jsonify +from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for from flask_login import login_required +from sqlalchemy import text from app_new.extensions import db from app_new.core.models.maintenance import WatchdogLog +from app_new.core.system_security import system_admin_required logs_bp = Blueprint('logs', __name__, url_prefix='/logs', template_folder='templates') @@ -57,3 +59,13 @@ def api_recent(): 'message': log.message, 'created_at': log.created_at.isoformat() if log.created_at else None } for log in logs]) + + +@logs_bp.route('/clear', methods=['POST']) +@system_admin_required +def clear_watchdog_logs(): + """Vide integralement la table dediee aux journaux des watchdogs.""" + db.session.execute(text('TRUNCATE TABLE watchdog_logs')) + db.session.commit() + flash('Les journaux des watchdogs ont été vidés.', 'success') + return redirect(url_for('logs.index')) diff --git a/app_new/logs/templates/logs/index.html b/app_new/logs/templates/logs/index.html index 7fe5942..e9fcbea 100644 --- a/app_new/logs/templates/logs/index.html +++ b/app_new/logs/templates/logs/index.html @@ -4,7 +4,17 @@ {% block content %}

Logs des watchdogs

- API JSON +
+ API JSON + {% if current_user.is_admin() %} +
+ + +
+ {% endif %} +
diff --git a/tests/integration/test_watchdog_logs.py b/tests/integration/test_watchdog_logs.py new file mode 100644 index 0000000..5219f01 --- /dev/null +++ b/tests/integration/test_watchdog_logs.py @@ -0,0 +1,20 @@ +from app_new.extensions import db +from app_new.core.models.maintenance import WatchdogLog + + +def test_admin_can_clear_watchdog_logs(app, authenticated_client): + with app.app_context(): + db.session.add( + WatchdogLog( + watchdog_name='test_watchdog', + level='info', + message='journal de test', + ) + ) + db.session.commit() + + response = authenticated_client.post('/logs/clear') + assert response.status_code == 302 + + with app.app_context(): + assert WatchdogLog.query.count() == 0