mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-17 04:32:51 -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
|
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
|
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()]
|
|
||||||
|
|
||||||
# Concatenate and sort complete list of children
|
# Add available prefixes to the table if requested
|
||||||
prefix_list = list(prefix_list) + available_prefixes
|
if prefix_list and request.GET.get('show_available', 'true') == 'true':
|
||||||
prefix_list.sort(key=lambda p: p.prefix)
|
|
||||||
|
|
||||||
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):
|
def add_available_ipaddresses(prefix, ipaddress_list, is_pool=False):
|
||||||
|
@ -15,7 +15,7 @@ from . import filtersets, forms, tables
|
|||||||
from .constants import *
|
from .constants import *
|
||||||
from .models import *
|
from .models import *
|
||||||
from .models import ASN
|
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()
|
queryset = Aggregate.objects.all()
|
||||||
|
|
||||||
def get_extra_context(self, request, instance):
|
def get_extra_context(self, request, instance):
|
||||||
# Find all child prefixes contained by this aggregate
|
# Find all child prefixes contained in this aggregate
|
||||||
child_prefixes = Prefix.objects.restrict(request.user, 'view').filter(
|
prefix_list = Prefix.objects.restrict(request.user, 'view').filter(
|
||||||
prefix__net_contained_or_equal=str(instance.prefix)
|
prefix__net_contained_or_equal=str(instance.prefix)
|
||||||
).prefetch_related(
|
).prefetch_related(
|
||||||
'site', 'role'
|
'site', 'role'
|
||||||
@ -284,9 +284,8 @@ class AggregateView(generic.ObjectView):
|
|||||||
'prefix'
|
'prefix'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add available prefixes to the table if requested
|
# Return List of requested Prefixes
|
||||||
if request.GET.get('show_available', 'true') == 'true':
|
child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request)
|
||||||
child_prefixes = add_available_prefixes(instance.prefix, child_prefixes)
|
|
||||||
|
|
||||||
prefix_table = tables.PrefixTable(child_prefixes, exclude=('utilization',))
|
prefix_table = tables.PrefixTable(child_prefixes, exclude=('utilization',))
|
||||||
if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'):
|
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,
|
'permissions': permissions,
|
||||||
'bulk_querystring': f'within={instance.prefix}',
|
'bulk_querystring': f'within={instance.prefix}',
|
||||||
'show_available': request.GET.get('show_available', 'true') == 'true',
|
'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'
|
template_name = 'ipam/prefix/prefixes.html'
|
||||||
|
|
||||||
def get_extra_context(self, request, instance):
|
def get_extra_context(self, request, instance):
|
||||||
# Child prefixes table
|
# Find all child prefixes contained in this prefix
|
||||||
child_prefixes = instance.get_child_prefixes().restrict(request.user, 'view').prefetch_related(
|
prefix_list = instance.get_child_prefixes().restrict(request.user, 'view').prefetch_related(
|
||||||
'site', 'vlan', 'role',
|
'site', 'vlan', 'role',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add available prefixes to the table if requested
|
# Return List of requested Prefixes
|
||||||
if child_prefixes and request.GET.get('show_available', 'true') == 'true':
|
child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request)
|
||||||
child_prefixes = add_available_prefixes(instance.prefix, child_prefixes)
|
|
||||||
|
|
||||||
table = tables.PrefixTable(child_prefixes, user=request.user, exclude=('utilization',))
|
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'):
|
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',
|
'active_tab': 'prefixes',
|
||||||
'first_available_prefix': instance.get_first_available_prefix(),
|
'first_available_prefix': instance.get_first_available_prefix(),
|
||||||
'show_available': request.GET.get('show_available', 'true') == 'true',
|
'show_available': request.GET.get('show_available', 'true') == 'true',
|
||||||
|
'show_assigned': request.GET.get('show_assigned', 'true') == 'true',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
{% load helpers %}
|
{% load helpers %}
|
||||||
|
|
||||||
{% if show_available is not None %}
|
{% if show_assigned or show_available is not None %}
|
||||||
<div class="btn-group" role="group">
|
<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
|
<i class="mdi mdi-eye"></i> Show Available
|
||||||
</a>
|
</a>
|
||||||
<a href="{{ request.path }}{% querystring request show_available='false' %}" class="btn btn-sm btn-outline-primary{% if not show_available %} active disabled{% endif %}">
|
<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-off"></i> Hide Available
|
<i class="mdi mdi-eye-plus"></i> Show All
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
Loading…
Reference in New Issue
Block a user