06/05 Optimize app: logs will be saved to db
This commit is contained in:
@@ -470,3 +470,24 @@ def audit_log():
|
||||
logs=pagination.items,
|
||||
actions=actions,
|
||||
action=action)
|
||||
|
||||
|
||||
@settings_bp.route('/audit/purge', methods=['POST'])
|
||||
@login_required
|
||||
def audit_purge():
|
||||
from app.models.audit_log import AuditLog
|
||||
from datetime import timedelta, datetime
|
||||
try:
|
||||
days = int(request.form.get('days', 30))
|
||||
if days not in (7, 30, 90):
|
||||
flash('Invalid purge period.', 'danger')
|
||||
return redirect(url_for('settings.audit_log'))
|
||||
except (ValueError, TypeError):
|
||||
flash('Invalid purge period.', 'danger')
|
||||
return redirect(url_for('settings.audit_log'))
|
||||
|
||||
cutoff = datetime.utcnow() - timedelta(days=days)
|
||||
deleted = AuditLog.query.filter(AuditLog.timestamp < cutoff).delete()
|
||||
db.session.commit()
|
||||
flash(f'Deleted {deleted} audit log entries older than {days} days.', 'success')
|
||||
return redirect(url_for('settings.audit_log'))
|
||||
|
||||
Reference in New Issue
Block a user