linuxrouter/docker/routlin-dash/app/pages/actions/action.py

51 lines
1.7 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 13:49:23 -04:00
from flask import Blueprint, request, redirect, flash, session
2026-05-23 00:27:37 -04:00
from auth import require_level
2026-05-25 21:31:20 -04:00
from config_utils import (flush_pending_to_queue, get_dashboard_pending,
2026-05-26 03:15:37 -04:00
revert_snapshot_to_config, queued_msg)
2026-05-23 00:27:37 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-23 00:27:37 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-23 00:27:37 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/actions/pending_save', methods=['POST'])
2026-05-23 00:27:37 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def pending_save():
2026-05-25 13:49:23 -04:00
session['apply_changes_immediately'] = 'apply_changes_immediately' in request.form
flash('Preference saved.', 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-23 00:27:37 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/actions/pending_apply', methods=['POST'])
2026-05-23 00:27:37 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def pending_apply():
2026-05-26 03:15:37 -04:00
pending = get_dashboard_pending()
if not pending:
2026-05-25 16:38:08 -04:00
flash('No pending changes to apply.', 'info')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 16:38:08 -04:00
flush_pending_to_queue()
2026-05-26 03:15:37 -04:00
if any(cmd != 'fix problems' for _, _, cmd, _ in pending):
flash('Changes queued.', 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 22:44:37 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/actions/history_revert', methods=['POST'])
2026-05-23 00:27:37 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def history_revert():
2026-05-25 14:50:03 -04:00
selected_uuids = request.form.getlist('selected_uuids')
if not selected_uuids:
flash('No items selected.', 'info')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-25 21:31:20 -04:00
succeeded, failed = 0, 0
for uuid in selected_uuids:
2026-05-25 22:44:37 -04:00
msg, ok = revert_snapshot_to_config(uuid)
2026-05-25 21:31:20 -04:00
if ok:
succeeded += 1
else:
flash(msg, 'error')
failed += 1
if succeeded:
plural = 's' if succeeded != 1 else ''
flash(f'{succeeded} change{plural} reverted.', 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')