Add admin action to clear watchdog logs
Some checks are pending
CI - Tests et Syntax / lint-and-test (push) Waiting to run

This commit is contained in:
root 2026-08-14 18:02:32 +00:00
parent 4b62583937
commit 469cef48aa
3 changed files with 44 additions and 2 deletions

View file

@ -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'))

View file

@ -4,7 +4,17 @@
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-3">
<h2 class="h5 mb-0"><i class="bi bi-journal-text"></i> Logs des watchdogs</h2>
<div class="d-flex gap-2">
<a href="{{ url_for('logs.api_recent', limit=100) }}" class="btn btn-sm btn-outline-secondary" target="_blank"><i class="bi bi-download"></i> API JSON</a>
{% if current_user.is_admin() %}
<form method="POST" action="{{ url_for('logs.clear_watchdog_logs') }}" onsubmit="return confirm('Vider définitivement tous les journaux des watchdogs ?');">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-sm btn-outline-danger">
<i class="bi bi-trash3"></i> Vider les logs
</button>
</form>
{% endif %}
</div>
</div>
<div class="card border-0 shadow-sm mb-3">

View file

@ -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