Development
This commit is contained in:
parent
eed1d295dc
commit
d9f3bd8289
45 changed files with 635 additions and 666 deletions
|
|
@ -1,3 +1,4 @@
|
|||
from pathlib import Path
|
||||
import copy
|
||||
import ipaddress
|
||||
|
||||
|
|
@ -7,10 +8,9 @@ from config_utils import load_config, save_config_with_snapshot, verify_config_h
|
|||
import sanitize
|
||||
import validation as validate
|
||||
|
||||
bp = Blueprint('dhcp', __name__)
|
||||
|
||||
VIEW = '/view/view_dhcp'
|
||||
_PAGE = Path(__file__).parent.name
|
||||
|
||||
bp = Blueprint(_PAGE, __name__)
|
||||
|
||||
def _row_index():
|
||||
try:
|
||||
|
|
@ -66,9 +66,9 @@ def _check_ip_conflicts(ip, vlan):
|
|||
return None
|
||||
|
||||
|
||||
@bp.route('/action/add_dhcp_reservation', methods=['POST'])
|
||||
@bp.route('/action/dhcp/addreservation_add', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def add_dhcp_reservation():
|
||||
def addreservation_add():
|
||||
vlan_name = sanitize.name(request.form.get('vlan_name', ''))
|
||||
description = sanitize.text(request.form.get('description', ''))
|
||||
hostname = validate.domainname(request.form.get('hostname', ''))
|
||||
|
|
@ -77,27 +77,27 @@ def add_dhcp_reservation():
|
|||
radius_client = 'radius_client' in request.form
|
||||
|
||||
if ip is None:
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not vlan_name:
|
||||
flash('The configuration has not been saved because a VLAN is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not mac:
|
||||
flash('The configuration has not been saved because a MAC address is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.get('vlans', [])
|
||||
vlan = next((v for v in vlans if v.get('name') == vlan_name), None)
|
||||
if vlan is None:
|
||||
flash(f'The configuration has not been saved because VLAN "{vlan_name}" was not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
conflict = _check_ip_conflicts(ip, vlan)
|
||||
if conflict:
|
||||
flash(f'The configuration has not been saved because {conflict}', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
entry = {
|
||||
'description': description,
|
||||
|
|
@ -112,7 +112,7 @@ def add_dhcp_reservation():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
|
|
@ -120,25 +120,25 @@ def add_dhcp_reservation():
|
|||
before=None, after=entry,
|
||||
description=f'Added DHCP reservation: {hostname or mac} ({ip or "dynamic"})',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/toggle_dhcp_reservation', methods=['POST'])
|
||||
@bp.route('/action/dhcp/reservations_toggle', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def toggle_dhcp_reservation():
|
||||
def reservations_toggle():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.get('vlans', [])
|
||||
vi, ri = _flat_index_to_vlan_res(vlans, idx)
|
||||
if vi is None:
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
res = vlans[vi]['reservations'][ri]
|
||||
old_enabled = res.get('enabled', True)
|
||||
|
|
@ -147,7 +147,7 @@ def toggle_dhcp_reservation():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
vlan_name = vlans[vi]['name']
|
||||
action = 'Enabled' if not old_enabled else 'Disabled'
|
||||
|
|
@ -157,16 +157,16 @@ def toggle_dhcp_reservation():
|
|||
before={'enabled': old_enabled}, after={'enabled': not old_enabled},
|
||||
description=f'{action} DHCP reservation: {res.get("hostname") or res["mac"]}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/edit_dhcp_reservation', methods=['POST'])
|
||||
@bp.route('/action/dhcp/reservations_edit', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def edit_dhcp_reservation():
|
||||
def reservations_edit():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
description = sanitize.text(request.form.get('description', ''))
|
||||
hostname = validate.domainname(request.form.get('hostname', ''))
|
||||
|
|
@ -175,24 +175,24 @@ def edit_dhcp_reservation():
|
|||
radius_client = 'radius_client' in request.form
|
||||
|
||||
if ip is None:
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not mac:
|
||||
flash('The configuration has not been saved because a MAC address is required.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.get('vlans', [])
|
||||
vi, ri = _flat_index_to_vlan_res(vlans, idx)
|
||||
if vi is None:
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
conflict = _check_ip_conflicts(ip, vlans[vi])
|
||||
if conflict:
|
||||
flash(f'The configuration has not been saved because {conflict}', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
res = vlans[vi]['reservations'][ri]
|
||||
before = copy.deepcopy(res)
|
||||
|
|
@ -208,7 +208,7 @@ def edit_dhcp_reservation():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
vlan_name = vlans[vi]['name']
|
||||
flash(save_config_with_snapshot(
|
||||
|
|
@ -217,25 +217,25 @@ def edit_dhcp_reservation():
|
|||
before=before, after=copy.deepcopy(res),
|
||||
description=f'Edited DHCP reservation: {hostname or mac} ({ip or "dynamic"})',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
|
||||
@bp.route('/action/delete_dhcp_reservation', methods=['POST'])
|
||||
@bp.route('/action/dhcp/reservations_delete', methods=['POST'])
|
||||
@require_level('administrator')
|
||||
def delete_dhcp_reservation():
|
||||
def reservations_delete():
|
||||
idx = _row_index()
|
||||
if idx is None:
|
||||
flash('Invalid request.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
if not _hash_ok():
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
cfg = load_config()
|
||||
vlans = cfg.get('vlans', [])
|
||||
vi, ri = _flat_index_to_vlan_res(vlans, idx)
|
||||
if vi is None:
|
||||
flash('Entry not found.', 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
vlan_name = vlans[vi]['name']
|
||||
removed = vlans[vi]['reservations'].pop(ri)
|
||||
|
|
@ -243,7 +243,7 @@ def delete_dhcp_reservation():
|
|||
if errors:
|
||||
for msg in errors:
|
||||
flash(msg, 'error')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
||||
flash(save_config_with_snapshot(
|
||||
cfg,
|
||||
|
|
@ -251,4 +251,4 @@ def delete_dhcp_reservation():
|
|||
before=removed, after=None,
|
||||
description=f'Deleted DHCP reservation: {removed.get("hostname") or removed["mac"]}',
|
||||
), 'success')
|
||||
return redirect(VIEW)
|
||||
return redirect(f'/{_PAGE}')
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
{
|
||||
"id": "view_dhcp",
|
||||
"client_requirement": "client_is_viewer+",
|
||||
"items": [
|
||||
{
|
||||
|
|
@ -97,7 +96,7 @@
|
|||
"row_actions": [
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/edit_dhcp_reservation",
|
||||
"action": "/action/dhcp/reservations_edit",
|
||||
"method": "inline_edit",
|
||||
"text": "Edit",
|
||||
"class": "btn-ghost btn-sm",
|
||||
|
|
@ -134,7 +133,7 @@
|
|||
},
|
||||
{
|
||||
"client_requirement": "client_is_administrator+",
|
||||
"action": "/action/delete_dhcp_reservation",
|
||||
"action": "/action/dhcp/reservations_delete",
|
||||
"method": "post",
|
||||
"text": "Delete",
|
||||
"class": "btn-danger btn-sm"
|
||||
|
|
@ -149,7 +148,7 @@
|
|||
"items": [
|
||||
{
|
||||
"type": "form",
|
||||
"action": "/action/add_dhcp_reservation",
|
||||
"action": "/action/dhcp/addreservation_add",
|
||||
"method": "post",
|
||||
"items": [
|
||||
{
|
||||
|
|
@ -203,7 +202,7 @@
|
|||
"items": [
|
||||
{
|
||||
"type": "button_primary",
|
||||
"action": "/action/add_dhcp_reservation",
|
||||
"action": "/action/dhcp/addreservation_add",
|
||||
"method": "post",
|
||||
"text": "Add"
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue