mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 12:12:53 -06:00
Merge pull request #7826 from WillIrvine/develop
Add filter for optionally including assigned prefixes
This commit is contained in:
commit
5d6158dd64
@ -4,20 +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 = []
|
||||
|
||||
# Concatenate and sort complete list of children
|
||||
prefix_list = list(prefix_list) + available_prefixes
|
||||
prefix_list.sort(key=lambda p: p.prefix)
|
||||
# Add available prefixes to the table if requested
|
||||
if prefix_list and request.GET.get('show_available', 'true') == 'true':
|
||||
|
||||
return prefix_list
|
||||
# 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):
|
||||
|
@ -15,7 +15,7 @@ from . import filtersets, forms, tables
|
||||
from .constants import *
|
||||
from .models import *
|
||||
from .models import ASN
|
||||
from .utils import add_available_ipaddresses, add_available_prefixes, add_available_vlans
|
||||
from .utils import add_available_ipaddresses, add_requested_prefixes, add_available_vlans
|
||||
|
||||
|
||||
#
|
||||
@ -275,8 +275,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 = 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'
|
||||
@ -284,9 +284,8 @@ class AggregateView(generic.ObjectView):
|
||||
'prefix'
|
||||
)
|
||||
|
||||
# Add available prefixes to the table if requested
|
||||
if request.GET.get('show_available', 'true') == 'true':
|
||||
child_prefixes = add_available_prefixes(instance.prefix, child_prefixes)
|
||||
# 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'):
|
||||
@ -305,6 +304,7 @@ class AggregateView(generic.ObjectView):
|
||||
'permissions': permissions,
|
||||
'bulk_querystring': f'within={instance.prefix}',
|
||||
'show_available': request.GET.get('show_available', 'true') == 'true',
|
||||
'show_assigned': request.GET.get('show_assigned', 'true') == 'true',
|
||||
}
|
||||
|
||||
|
||||
@ -456,14 +456,13 @@ class PrefixPrefixesView(generic.ObjectView):
|
||||
template_name = 'ipam/prefix/prefixes.html'
|
||||
|
||||
def get_extra_context(self, request, instance):
|
||||
# Child prefixes table
|
||||
child_prefixes = 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',
|
||||
)
|
||||
|
||||
# Add available prefixes to the table if requested
|
||||
if child_prefixes and request.GET.get('show_available', 'true') == 'true':
|
||||
child_prefixes = add_available_prefixes(instance.prefix, child_prefixes)
|
||||
# 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'):
|
||||
@ -485,6 +484,7 @@ class PrefixPrefixesView(generic.ObjectView):
|
||||
'active_tab': 'prefixes',
|
||||
'first_available_prefix': instance.get_first_available_prefix(),
|
||||
'show_available': request.GET.get('show_available', 'true') == 'true',
|
||||
'show_assigned': request.GET.get('show_assigned', 'true') == 'true',
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,12 +1,15 @@
|
||||
{% load helpers %}
|
||||
|
||||
{% if show_available is not None %}
|
||||
{% if show_assigned or show_available is not None %}
|
||||
<div class="btn-group" role="group">
|
||||
<a href="{{ request.path }}{% querystring request show_available='true' %}" class="btn btn-sm btn-outline-primary{% if show_available %} active disabled{% endif %}">
|
||||
<a href="{{ request.path }}{% querystring request show_assigned='true' show_available='false' %}" class="btn btn-sm btn-outline-primary{% if show_assigned and not show_available %} active disabled{% endif %}">
|
||||
<i class="mdi mdi-eye-outline"></i> Show Assigned
|
||||
</a>
|
||||
<a href="{{ request.path }}{% querystring request show_assigned='false' show_available='true' %}" class="btn btn-sm btn-outline-primary{% if show_available and not show_assigned %} active disabled{% endif %}">
|
||||
<i class="mdi mdi-eye"></i> Show Available
|
||||
</a>
|
||||
<a href="{{ request.path }}{% querystring request show_available='false' %}" class="btn btn-sm btn-outline-primary{% if not show_available %} active disabled{% endif %}">
|
||||
<i class="mdi mdi-eye-off"></i> Hide Available
|
||||
<a href="{{ request.path }}{% querystring request show_assigned='true' show_available='true' %}" class="btn btn-sm btn-outline-primary{% if show_available and show_assigned %} active disabled{% endif %}">
|
||||
<i class="mdi mdi-eye-plus"></i> Show All
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
Loading…
Reference in New Issue
Block a user