from pathlib import Path import copy from flask import Blueprint, request, redirect, flash from auth import require_level from config_utils import load_config, record_group, diff_fields, verify_config_hash import sanitize import mod_validation as validate _PAGE = Path(__file__).parent.name bp = Blueprint(_PAGE, __name__) @bp.route('/action/mdns/settings_apply', methods=['POST']) @require_level('administrator') def settings_apply(): mdns_enabled = 'mdns_enabled' in request.form if not verify_config_hash(request.form.get('config_hash', '')): flash('Configuration was modified by another session. Please refresh and try again.', 'error') return redirect(f'/{_PAGE}') cfg = load_config() mdns_reflect_vlans = sanitize.filterlist( request.form.getlist('mdns_reflect_vlans'), {v.get('name') for v in cfg.get('vlans', [])}, ) before = copy.deepcopy(cfg.get('mdns_reflection', {})) cfg.setdefault('mdns_reflection', {}).update({ 'enabled': mdns_enabled, 'reflect_vlans': mdns_reflect_vlans, }) errors = validate.validate_config(cfg) if errors: for msg in errors: flash(msg, 'error') return redirect(f'/{_PAGE}') changes = diff_fields(before, cfg['mdns_reflection']) flash(record_group(cfg, 'mdns_reflection', None, None, changes, 'core apply'), 'success') return redirect(f'/{_PAGE}')