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

44 lines
1.4 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-25 16:07:21 -04:00
import copy
2026-05-17 03:26:01 -04:00
from flask import Blueprint, request, redirect, flash
from auth import require_level
2026-05-30 14:57:33 -04:00
from config_utils import load_config, record_group, diff_fields, verify_config_hash
2026-05-17 03:26:01 -04:00
import sanitize
2026-05-20 17:10:18 -04:00
import validation as validate
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
bp = Blueprint(_PAGE, __name__)
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/mdns/settings_apply', methods=['POST'])
2026-05-17 03:26:01 -04:00
@require_level('administrator')
2026-05-27 22:04:04 -04:00
def settings_apply():
2026-05-25 16:07:21 -04:00
mdns_enabled = 'mdns_enabled' in request.form
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
if not verify_config_hash(request.form.get('config_hash', '')):
2026-05-17 03:26:01 -04:00
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-25 19:59:42 -04:00
cfg = load_config()
2026-05-25 16:07:21 -04:00
mdns_reflect_vlans = sanitize.filterlist(
request.form.getlist('mdns_reflect_vlans'),
2026-05-25 19:59:42 -04:00
{v.get('name') for v in cfg.get('vlans', [])},
2026-05-25 16:07:21 -04:00
)
2026-05-25 19:59:42 -04:00
before = copy.deepcopy(cfg.get('mdns_reflection', {}))
cfg.setdefault('mdns_reflection', {}).update({
2026-05-17 03:26:01 -04:00
'enabled': mdns_enabled,
'reflect_vlans': mdns_reflect_vlans,
})
2026-05-25 19:59:42 -04:00
errors = validate.validate_config(cfg)
2026-05-20 17:10:18 -04:00
if errors:
for msg in errors:
flash(msg, 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-05-30 14:57:33 -04:00
changes = diff_fields(before, cfg['mdns_reflection'])
flash(record_group(cfg, 'mdns_reflection', None, None, changes, 'core apply'), 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')