Development
This commit is contained in:
parent
b99ea35f79
commit
5149e5a035
10 changed files with 197 additions and 213 deletions
|
|
@ -1,12 +1,12 @@
|
|||
from flask import Blueprint, request, redirect, flash
|
||||
from auth import require_level
|
||||
import json
|
||||
from config_utils import load_core, save_core
|
||||
import sanitize
|
||||
import validation as validate
|
||||
|
||||
bp = Blueprint('action_apply_ddns_providers', __name__)
|
||||
|
||||
DDNS_FILE = '/configs/ddns.json'
|
||||
VIEW = '/view/view_ddns'
|
||||
|
||||
|
||||
@bp.route('/action/add_ddns_provider', methods=['POST'])
|
||||
|
|
@ -14,24 +14,17 @@ DDNS_FILE = '/configs/ddns.json'
|
|||
def add_ddns_provider():
|
||||
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
|
||||
description = sanitize.description(request.form.get('description', ''))
|
||||
hostnames = sanitize.domainlist(request.form.get('hostnames', '').splitlines())
|
||||
hostnames = sanitize.domainlist(request.form.get('hostnames', '').splitlines())
|
||||
|
||||
if not description:
|
||||
flash('Description is required.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
if not hostnames:
|
||||
flash('At least one hostname is required.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
if not provider_type:
|
||||
flash('Unknown provider type.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE) as f:
|
||||
data = json.load(f)
|
||||
except Exception as ex:
|
||||
flash(f'Could not read config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
|
||||
entry = {
|
||||
'description': description,
|
||||
|
|
@ -45,15 +38,11 @@ def add_ddns_provider():
|
|||
else:
|
||||
entry['api_token'] = request.form.get('api_token', '').strip()
|
||||
|
||||
data.setdefault('providers', []).append(entry)
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
flash(f'DDNS provider "{description}" added.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not save config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
core = load_core()
|
||||
core.setdefault('ddns', {}).setdefault('providers', []).append(entry)
|
||||
save_core(core)
|
||||
flash(f'DDNS provider "{description}" added.', 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/edit_ddns_provider', methods=['POST'])
|
||||
|
|
@ -63,29 +52,22 @@ def edit_ddns_provider():
|
|||
row_index = int(request.form.get('row_index', -1))
|
||||
except (TypeError, ValueError):
|
||||
flash('Invalid row index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
|
||||
provider_type = sanitize.filtervalue(request.form.get('provider', ''), validate.VALID_DDNS_PROVIDERS)
|
||||
description = sanitize.description(request.form.get('description', ''))
|
||||
hostnames_raw = request.form.get('hostnames', '')
|
||||
hostnames = [h.strip() for h in request.form.get('hostnames', '').splitlines() if h.strip()]
|
||||
enabled = request.form.get('enabled') == 'on'
|
||||
hostnames = [h.strip() for h in hostnames_raw.splitlines() if h.strip()]
|
||||
|
||||
if not provider_type:
|
||||
flash('Unknown provider type.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE) as f:
|
||||
data = json.load(f)
|
||||
except Exception as ex:
|
||||
flash(f'Could not read config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
providers = data.get('providers', [])
|
||||
core = load_core()
|
||||
providers = core.setdefault('ddns', {}).setdefault('providers', [])
|
||||
if row_index < 0 or row_index >= len(providers):
|
||||
flash('Invalid provider index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
|
||||
entry = {
|
||||
'description': description,
|
||||
|
|
@ -100,15 +82,9 @@ def edit_ddns_provider():
|
|||
entry['api_token'] = request.form.get('api_token', '').strip()
|
||||
|
||||
providers[row_index] = entry
|
||||
data['providers'] = providers
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
flash('DDNS provider updated.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not save config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
save_core(core)
|
||||
flash('DDNS provider updated.', 'success')
|
||||
return redirect(VIEW)
|
||||
|
||||
|
||||
@bp.route('/action/delete_ddns_provider', methods=['POST'])
|
||||
|
|
@ -118,27 +94,15 @@ def delete_ddns_provider():
|
|||
row_index = int(request.form.get('row_index', -1))
|
||||
except (TypeError, ValueError):
|
||||
flash('Invalid row index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE) as f:
|
||||
data = json.load(f)
|
||||
except Exception as ex:
|
||||
flash(f'Could not read config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
|
||||
providers = data.get('providers', [])
|
||||
core = load_core()
|
||||
providers = core.setdefault('ddns', {}).setdefault('providers', [])
|
||||
if row_index < 0 or row_index >= len(providers):
|
||||
flash('Invalid provider index.', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
return redirect(VIEW)
|
||||
|
||||
del providers[row_index]
|
||||
data['providers'] = providers
|
||||
|
||||
try:
|
||||
with open(DDNS_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
flash('DDNS provider deleted.', 'success')
|
||||
except Exception as ex:
|
||||
flash(f'Could not save config: {ex}', 'error')
|
||||
return redirect('/view/view_ddns')
|
||||
save_core(core)
|
||||
flash('DDNS provider deleted.', 'success')
|
||||
return redirect(VIEW)
|
||||
|
|
|
|||
|
|
@ -176,5 +176,5 @@ def dnsblocklists_cardblocklistrefresh_save():
|
|||
@bp.route('/action/dnsblocklists_cardblocklistrefresh_refresh', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def dnsblocklists_cardblocklistrefresh_refresh():
|
||||
flash(queued_msg('core update-blocklists'), 'success')
|
||||
flash(queued_msg('core update-blocklists', action_label='Blocklist refresh queued'), 'success')
|
||||
return redirect(VIEW)
|
||||
|
|
|
|||
|
|
@ -118,5 +118,5 @@ def networkinterfaces_cardinterfaceconfiguration_apply():
|
|||
flash('No changes detected.', 'info')
|
||||
return redirect(_VIEW)
|
||||
|
||||
flash(queued_msg(), 'success')
|
||||
flash(queued_msg(action_label='Changes queued'), 'success')
|
||||
return redirect(_VIEW)
|
||||
|
|
|
|||
|
|
@ -232,29 +232,30 @@ def queue_command(cmd, description=''):
|
|||
return _queue_command(cmd, description)
|
||||
|
||||
|
||||
def queued_msg(cmd=None, description=''):
|
||||
def queued_msg(cmd=None, description='', action_label='Configuration saved'):
|
||||
"""Queue cmd if given, then return a timing message.
|
||||
Without cmd, just returns timing (for commands already queued by the caller)."""
|
||||
Without cmd, just returns timing (for commands already queued by the caller).
|
||||
action_label replaces the 'Configuration saved' prefix for non-save actions."""
|
||||
entry_ts = None
|
||||
if cmd is not None:
|
||||
_entry_uuid, entry_ts = queue_command(cmd, description)
|
||||
if not _apply_on_save():
|
||||
return 'Configuration saved. Click Apply Now on the Configuration Changes card to apply.'
|
||||
return f'{action_label}. Click Apply Now on the Configuration Changes card to apply.'
|
||||
if _is_locked():
|
||||
mtime = _lock_mtime()
|
||||
if entry_ts is not None and mtime and entry_ts < mtime:
|
||||
return 'Configuration saved. Changes are being applied now.'
|
||||
return 'Configuration saved. Changes will be applied on the next run.'
|
||||
return f'{action_label}. Changes are being applied now.'
|
||||
return f'{action_label}. Changes will be applied on the next run.'
|
||||
timing = _format_timing(_seconds_until_next_run())
|
||||
if timing:
|
||||
return f'Configuration saved. Changes will be applied {timing}.'
|
||||
return f'{action_label}. Changes will be applied {timing}.'
|
||||
if cmd is None:
|
||||
return 'Changes queued. The processing service is not running.'
|
||||
return f'{action_label}. The processing service is not running.'
|
||||
parts = cmd.split()
|
||||
cli_cmd = f'sudo python3 {parts[0]}.py --{parts[1]}' if len(parts) == 2 else cmd
|
||||
install_cmd = f'sudo python3 install.py'
|
||||
from markupsafe import Markup
|
||||
return Markup(f'Configuration saved. The command processing service is not installed. '
|
||||
return Markup(f'{action_label}. The command processing service is not installed. '
|
||||
f'Run <strong>{install_cmd}</strong> to enable it, '
|
||||
f'or <strong>{cli_cmd}</strong> to apply manually.')
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ def _load_json(path):
|
|||
return {}
|
||||
|
||||
def _load_core(): return _load_json(f'{CONFIGS_DIR}/core.json')
|
||||
def _load_ddns(): return _load_json(f'{CONFIGS_DIR}/ddns.json')
|
||||
def _load_ddns(): return _load_core().get('ddns', {})
|
||||
def _load_accounts(): return _load_json(f'{DATA_DIR}/authorized_accounts.json')
|
||||
|
||||
def _load_css():
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@
|
|||
{ "type": "nav_item", "label": "General", "map_to": "view_general", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "Network Interfaces", "map_to": "view_network_interfaces", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "Upstream DNS", "map_to": "view_upstream_dns", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "DNS Blocklists", "map_to": "view_dns_blocklists", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "VLANs", "map_to": "view_vlans", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "Inter-VLAN Exceptions", "map_to": "view_inter_vlan", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "DNS Blocklists", "map_to": "view_dns_blocklists", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "Port Forwarding", "map_to": "view_port_forwarding", "client_requirement": "client_is_administrator+" },
|
||||
{ "type": "nav_item", "label": "DHCP", "map_to": "view_dhcp" },
|
||||
{ "type": "nav_item", "label": "Host Overrides", "map_to": "view_host_overrides", "client_requirement": "client_is_administrator+" },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue