Skip to content

Commit afc866e

Browse files
committed
#7665: Refactored add_requested_prefixes(); removed button icons
1 parent b6d93b7 commit afc866e

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

netbox/ipam/utils.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@
44
from .models import Prefix, VLAN
55

66

7-
def add_requested_prefixes(parent, prefix_list, request):
8-
"""
9-
Return a list of requested prefixes using show_available, show_assigned filters.
10-
If avalible prefixes are requested, create fake Prefix objects for all unallocated space within a prefix
7+
def add_requested_prefixes(parent, prefix_list, show_available=True, show_assigned=True):
118
"""
9+
Return a list of requested prefixes using show_available, show_assigned filters. If available prefixes are
10+
requested, create fake Prefix objects for all unallocated space within a prefix.
1211
12+
:param parent: Parent Prefix instance
13+
:param prefix_list: Child prefixes list
14+
:param show_available: Include available prefixes.
15+
:param show_assigned: Show assigned prefixes.
16+
"""
1317
child_prefixes = []
1418

1519
# Add available prefixes to the table if requested
16-
if prefix_list and request.GET.get('show_available', 'true') == 'true':
20+
if prefix_list and show_available:
1721

1822
# Find all unallocated space, add fake Prefix objects to child_prefixes.
1923
available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list])
2024
available_prefixes = [Prefix(prefix=p, status=None) for p in available_prefixes.iter_cidrs()]
2125
child_prefixes = child_prefixes + available_prefixes
2226

2327
# Add assigned prefixes to the table if requested
24-
if prefix_list and request.GET.get('show_assigned', 'true') == 'true':
28+
if prefix_list and show_assigned:
2529
child_prefixes = child_prefixes + list(prefix_list)
2630

2731
# Sort child prefixes after additions

netbox/ipam/views.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ def get_extra_context(self, request, instance):
285285
)
286286

287287
# Return List of requested Prefixes
288-
child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request)
288+
show_available = bool(request.GET.get('show_available', 'true') == 'true')
289+
show_assigned = bool(request.GET.get('show_assigned', 'true') == 'true')
290+
child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, show_available, show_assigned)
289291

290292
prefix_table = tables.PrefixTable(child_prefixes, exclude=('utilization',))
291293
if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'):
@@ -303,8 +305,8 @@ def get_extra_context(self, request, instance):
303305
'prefix_table': prefix_table,
304306
'permissions': permissions,
305307
'bulk_querystring': f'within={instance.prefix}',
306-
'show_available': request.GET.get('show_available', 'true') == 'true',
307-
'show_assigned': request.GET.get('show_assigned', 'true') == 'true',
308+
'show_available': show_available,
309+
'show_assigned': show_assigned,
308310
}
309311

310312

@@ -462,7 +464,9 @@ def get_extra_context(self, request, instance):
462464
)
463465

464466
# Return List of requested Prefixes
465-
child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, request)
467+
show_available = bool(request.GET.get('show_available', 'true') == 'true')
468+
show_assigned = bool(request.GET.get('show_assigned', 'true') == 'true')
469+
child_prefixes = add_requested_prefixes(instance.prefix, prefix_list, show_available, show_assigned)
466470

467471
table = tables.PrefixTable(child_prefixes, user=request.user, exclude=('utilization',))
468472
if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'):
@@ -483,8 +487,8 @@ def get_extra_context(self, request, instance):
483487
'bulk_querystring': bulk_querystring,
484488
'active_tab': 'prefixes',
485489
'first_available_prefix': instance.get_first_available_prefix(),
486-
'show_available': request.GET.get('show_available', 'true') == 'true',
487-
'show_assigned': request.GET.get('show_assigned', 'true') == 'true',
490+
'show_available': show_available,
491+
'show_assigned': show_assigned,
488492
}
489493

490494

netbox/templates/ipam/inc/toggle_available.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
{% if show_assigned or show_available is not None %}
44
<div class="btn-group" role="group">
5-
<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 %}">
6-
<i class="mdi mdi-eye-outline"></i> Show Assigned
5+
<a href="{{ request.path }}{% querystring request show_assigned='true' show_available='false' %}" class="btn btn-sm {% if show_assigned and not show_available %}btn-primary active{% else %}btn-outline-primary{% endif %}">
6+
Show Assigned
77
</a>
8-
<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 %}">
9-
<i class="mdi mdi-eye"></i> Show Available
8+
<a href="{{ request.path }}{% querystring request show_assigned='false' show_available='true' %}" class="btn btn-sm {% if show_available and not show_assigned %}btn-primary active{% else %}btn-outline-primary{% endif %}">
9+
Show Available
1010
</a>
11-
<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 %}">
12-
<i class="mdi mdi-eye-plus"></i> Show All
11+
<a href="{{ request.path }}{% querystring request show_assigned='true' show_available='true' %}" class="btn btn-sm {% if show_available and show_assigned %}btn-primary active{% else %}btn-outline-primary{% endif %}">
12+
Show All
1313
</a>
1414
</div>
1515
{% endif %}

0 commit comments

Comments
 (0)