Moved filtering logic to utils, adjusted show buttons

This commit is contained in:
Will Irvine 2021-12-01 19:24:44 +13:00
parent 80048bfa2b
commit dcfd332cbf
3 changed files with 38 additions and 54 deletions

View File

@ -4,16 +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()]
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): def add_available_ipaddresses(prefix, ipaddress_list, is_pool=False):

View File

@ -11,7 +11,7 @@ from virtualization.models import VirtualMachine, VMInterface
from . import filtersets, forms, tables from . import filtersets, forms, tables
from .constants import * from .constants import *
from .models 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() 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_assigned = 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'
@ -221,19 +221,8 @@ class AggregateView(generic.ObjectView):
'prefix' 'prefix'
) )
# List to append filtered prefixes to # Return List of requested Prefixes
child_prefixes = [] child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request)
# 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)
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'):
@ -404,24 +393,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):
# Initial pull of currently assigned child prefixes # Find all child prefixes contained in this prefix
child_prefixes_assigned = 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',
) )
# List to append filtered prefixes to # Return List of requested Prefixes
child_prefixes = [] child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request)
# 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)
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'):

View File

@ -1,23 +1,15 @@
{% load helpers %} {% load helpers %}
{% if show_assigned 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_assigned='true' %}" class="btn btn-sm btn-outline-primary{% if show_assigned %} 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"></i> Show Assigned <i class="mdi mdi-eye-outline"></i> Show Assigned
</a> </a>
<a href="{{ request.path }}{% querystring request show_assigned='false' %}" class="btn btn-sm btn-outline-primary{% if not show_assigned %} active disabled{% endif %}"> <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-off"></i> Hide Assigned <i class="mdi mdi-eye"></i> Show Avaliable
</a>
<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> </a>
</div> </div>
{% endif %} {% endif %}
{% if 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 %}">
<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>
</div>
{% endif %}