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

92 lines
3.2 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-06-07 00:21:08 -04:00
import auth
import config_utils
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-06-07 00:21:08 -04:00
@auth.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-06-07 00:21:08 -04:00
@auth.require_level('administrator')
2026-05-27 22:04:04 -04:00
def pending_apply():
2026-06-07 00:21:08 -04:00
pending = config_utils.get_dashboard_pending()
2026-05-26 03:15:37 -04:00
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-06-07 00:21:08 -04:00
config_utils.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-06-07 00:21:08 -04:00
@auth.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-06-03 16:04:51 -04:00
if len(selected_uuids) != 1:
flash('Please select exactly one change to revert.', 'error')
return redirect(f'/{_PAGE}')
behavior = request.form.get('revert_behavior', 'revert_subsequent')
2026-06-07 00:21:08 -04:00
errors, succeeded, failed = config_utils.revert_group_chain(selected_uuids[0])
2026-06-03 16:04:51 -04:00
for msg in errors:
flash(msg, 'error')
2026-05-25 21:31:20 -04:00
if succeeded:
2026-06-03 16:04:51 -04:00
s = 's' if succeeded != 1 else ''
if behavior == 'restore_state':
flash(f'Config restored to selected state ({succeeded} change{s} reverted).', 'success')
else:
flash(f'{succeeded} change{s} reverted.', 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-29 22:53:20 -04:00
@bp.route('/action/actions/history_clear', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('manager')
2026-05-29 22:53:20 -04:00
def history_clear():
2026-05-29 23:10:04 -04:00
selected_uuids = request.form.getlist('selected_uuids')
if not selected_uuids:
flash('No items selected.', 'info')
return redirect(f'/{_PAGE}')
2026-05-29 22:53:20 -04:00
count = 0
2026-06-07 00:21:08 -04:00
conn = config_utils._db()
2026-05-30 14:57:33 -04:00
try:
for uid in selected_uuids:
conn.execute('DELETE FROM changes WHERE group_id=?', (uid,))
result = conn.execute('DELETE FROM groups WHERE uuid=?', (uid,))
count += result.rowcount
conn.commit()
finally:
conn.close()
2026-05-29 22:53:20 -04:00
plural = 's' if count != 1 else ''
2026-05-29 23:10:04 -04:00
flash(f'{count} history record{plural} cleared.', 'success')
2026-05-29 22:53:20 -04:00
return redirect(f'/{_PAGE}')
@bp.route('/action/actions/pending_dismiss', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('manager')
2026-05-29 22:53:20 -04:00
def pending_dismiss():
2026-06-07 00:21:08 -04:00
pending = config_utils.get_dashboard_pending()
2026-05-29 23:10:04 -04:00
dismissible = [(u, t, c, usr) for u, t, c, usr in pending if c != 'fix problems']
if not dismissible:
2026-05-29 22:53:20 -04:00
flash('No pending changes to dismiss.', 'info')
return redirect(f'/{_PAGE}')
2026-05-29 23:10:04 -04:00
keep = [(u, t, c, usr) for u, t, c, usr in pending if c == 'fix problems']
2026-06-07 00:21:08 -04:00
with open(config_utils.DASHBOARD_PENDING, 'w') as f:
2026-05-29 23:10:04 -04:00
for u, t, c, usr in keep:
f.write(f'{u} {t} [{c}] ({usr})\n')
2026-06-03 16:54:33 -04:00
flash('Pending changes dismissed.', 'success')
2026-05-29 22:53:20 -04:00
return redirect(f'/{_PAGE}')