From dcfd332cbfb2817b91ec744933291a718b3ebbdd Mon Sep 17 00:00:00 2001 From: Will Irvine Date: Wed, 1 Dec 2021 19:24:44 +1300 Subject: [PATCH] Moved filtering logic to utils, adjusted show buttons --- netbox/ipam/utils.py | 26 +++++++++--- netbox/ipam/views.py | 40 +++++-------------- .../templates/ipam/inc/toggle_available.html | 26 +++++------- 3 files changed, 38 insertions(+), 54 deletions(-) diff --git a/netbox/ipam/utils.py b/netbox/ipam/utils.py index c457dc068..3ec910ede 100644 --- a/netbox/ipam/utils.py +++ b/netbox/ipam/utils.py @@ -4,16 +4,30 @@ from .constants import * from .models import Prefix, VLAN -def add_available_prefixes(parent, prefix_list): +def add_requested_prefixes(parent, prefix_list, request): """ - Create fake Prefix objects for all unallocated space within a prefix. + Return a list of requested prefixes using show_available, show_assigned filters. + If avalible prefixes are requested, create fake Prefix objects for all unallocated space within a prefix """ - # Find all unallocated space - available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list]) - available_prefixes = [Prefix(prefix=p, status=None) for p in available_prefixes.iter_cidrs()] + child_prefixes = [] - return available_prefixes + # Add available prefixes to the table if requested + if prefix_list and request.GET.get('show_available', 'true') == 'true': + + # Find all unallocated space, add fake Prefix objects to child_prefixes. + available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list]) + available_prefixes = [Prefix(prefix=p, status=None) for p in available_prefixes.iter_cidrs()] + child_prefixes = child_prefixes + available_prefixes + + # Add assigned prefixes to the table if requested + if prefix_list and request.GET.get('show_assigned', 'true') == 'true': + child_prefixes = child_prefixes + list(prefix_list) + + # Sort child prefixes after additions + child_prefixes.sort(key=lambda p: p.prefix) + + return child_prefixes def add_available_ipaddresses(prefix, ipaddress_list, is_pool=False): diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index fc64eb73b..337c5cf8f 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -11,7 +11,7 @@ from virtualization.models import VirtualMachine, VMInterface from . import filtersets, forms, tables from .constants import * from .models import * -from .utils import add_available_ipaddresses, add_available_prefixes, add_available_vlans +from .utils import add_available_ipaddresses, add_requested_prefixes, add_available_vlans # @@ -212,8 +212,8 @@ class AggregateView(generic.ObjectView): queryset = Aggregate.objects.all() def get_extra_context(self, request, instance): - # Find all child prefixes contained by this aggregate - child_prefixes_assigned = Prefix.objects.restrict(request.user, 'view').filter( + # Find all child prefixes contained in this aggregate + prefix_list = Prefix.objects.restrict(request.user, 'view').filter( prefix__net_contained_or_equal=str(instance.prefix) ).prefetch_related( 'site', 'role' @@ -221,19 +221,8 @@ class AggregateView(generic.ObjectView): 'prefix' ) - # List to append filtered prefixes to - child_prefixes = [] - - # Add available prefixes to the table if requested - if child_prefixes_assigned and request.GET.get('show_available', 'true') == 'true': - child_prefixes = child_prefixes + add_available_prefixes(instance.prefix, child_prefixes_assigned) - - # Add assigned prefixes to the table if requested - if child_prefixes_assigned and request.GET.get('show_assigned', 'true') == 'true': - child_prefixes = child_prefixes + list(child_prefixes_assigned) - - # Sort child prefixes after additions - child_prefixes.sort(key=lambda p: p.prefix) + # Return List of requested Prefixes + child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request) prefix_table = tables.PrefixTable(child_prefixes, exclude=('utilization',)) if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'): @@ -404,24 +393,13 @@ class PrefixPrefixesView(generic.ObjectView): template_name = 'ipam/prefix/prefixes.html' def get_extra_context(self, request, instance): - # Initial pull of currently assigned child prefixes - child_prefixes_assigned = instance.get_child_prefixes().restrict(request.user, 'view').prefetch_related( + # Find all child prefixes contained in this prefix + prefix_list = instance.get_child_prefixes().restrict(request.user, 'view').prefetch_related( 'site', 'vlan', 'role', ) - # List to append filtered prefixes to - child_prefixes = [] - - # Add available prefixes to the table if requested - if child_prefixes_assigned and request.GET.get('show_available', 'true') == 'true': - child_prefixes = child_prefixes + add_available_prefixes(instance.prefix, child_prefixes_assigned) - - # Add assigned prefixes to the table if requested - if child_prefixes_assigned and request.GET.get('show_assigned', 'true') == 'true': - child_prefixes = child_prefixes + list(child_prefixes_assigned) - - # Sort child prefixes after additions - child_prefixes.sort(key=lambda p: p.prefix) + # Return List of requested Prefixes + child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request) table = tables.PrefixTable(child_prefixes, user=request.user, exclude=('utilization',)) if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'): diff --git a/netbox/templates/ipam/inc/toggle_available.html b/netbox/templates/ipam/inc/toggle_available.html index f0d286128..15ea19c98 100644 --- a/netbox/templates/ipam/inc/toggle_available.html +++ b/netbox/templates/ipam/inc/toggle_available.html @@ -1,23 +1,15 @@ {% load helpers %} -{% if show_assigned is not None %} +{% if show_assigned or show_available is not None %}
- - Show Assigned + + Show Assigned - - Hide Assigned + + Show Avaliable + + + Show All
-{% endif %} - -{% if show_available is not None %} -
- - Show Available - - - Hide Available - -
-{% endif %} +{% endif %} \ No newline at end of file