from flask import Blueprint, request, redirect, flash from auth import require_level from config_utils import (load_core, save_core, verify_core_hash, queued_msg, flush_pending_to_queue, get_dashboard_pending, _is_locked, _format_timing, _seconds_until_next_run) import validation as validate bp = Blueprint('action_general', __name__) _VIEW = '/view/view_general' @bp.route('/action/general_cardpendingchanges_save', methods=['POST']) @require_level('administrator') def general_cardpendingchanges_save(): if not verify_core_hash(request.form.get('config_hash', '')): flash('Configuration was modified by another session. Please refresh and try again.', 'error') return redirect(_VIEW) core = load_core() core.setdefault('network_interfaces', {})['apply_on_save'] = 'apply_on_save' in request.form save_core(core) flash(queued_msg('core apply'), 'success') return redirect(_VIEW) @bp.route('/action/general_cardpendingchanges_applyselected', methods=['POST']) @require_level('administrator') def general_cardpendingchanges_applyselected(): items = get_dashboard_pending() if not items: flash('No pending changes to apply.', 'info') return redirect(_VIEW) flush_pending_to_queue() if _is_locked(): msg = 'Changes queued. They are being applied now.' else: timing = _format_timing(_seconds_until_next_run()) if timing: msg = f'Changes queued. They will be applied {timing}.' else: msg = 'Changes queued. The processing service is not running.' flash(msg, 'success') return redirect(_VIEW) @bp.route('/action/general_cardpendingchanges_deleteselected', methods=['POST']) @require_level('administrator') def general_cardpendingchanges_deleteselected(): flash('Not yet implemented.', 'info') return redirect(_VIEW)