From 37bc17d3a28ca1149cd2bded2213f877daadce99 Mon Sep 17 00:00:00 2001 From: Saria Hajjar Date: Thu, 2 Jan 2020 09:16:18 +0000 Subject: [PATCH] Fixes #2365: Toggle for showing available prefixes/ip addresses --- docs/release-notes/version-2.6.md | 1 + netbox/ipam/views.py | 29 ++++++++++++++++++++++++---- netbox/templates/ipam/aggregate.html | 8 ++++++++ netbox/templates/ipam/prefix.html | 8 ++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/docs/release-notes/version-2.6.md b/docs/release-notes/version-2.6.md index 548492438..f916f5083 100644 --- a/docs/release-notes/version-2.6.md +++ b/docs/release-notes/version-2.6.md @@ -2,6 +2,7 @@ ## Enhancements +* [#2365](https://github.com/netbox-community/netbox/issues/2365) - Toggle for showing available prefixes/ip addresses * [#3705](https://github.com/netbox-community/netbox/issues/3705) - Provide request context when executing custom scripts * [#3762](https://github.com/netbox-community/netbox/issues/3762) - Add date/time picker widgets * [#3788](https://github.com/netbox-community/netbox/issues/3788) - Enable partial search for inventory items diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 2cc1a0ea8..24d8efa82 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -333,7 +333,14 @@ class AggregateView(PermissionRequiredMixin, View): ).annotate_depth( limit=0 ) - child_prefixes = add_available_prefixes(aggregate.prefix, child_prefixes) + + # Update the ipam_show_available cookie if request specifies it + if request.GET.get('show_available') is not None: + request.session['ipam_show_available'] = request.GET.get('show_available') == 'true' + + # Add available prefixes to the table if the cookie requested it + if request.session.get('ipam_show_available'): + child_prefixes = add_available_prefixes(aggregate.prefix, child_prefixes) prefix_table = tables.PrefixDetailTable(child_prefixes) if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'): @@ -356,6 +363,7 @@ class AggregateView(PermissionRequiredMixin, View): 'aggregate': aggregate, 'prefix_table': prefix_table, 'permissions': permissions, + 'show_available': request.session.get('ipam_show_available'), }) @@ -511,8 +519,12 @@ class PrefixPrefixesView(PermissionRequiredMixin, View): 'site', 'vlan', 'role', ).annotate_depth(limit=0) - # Annotate available prefixes - if child_prefixes: + # Update the ipam_show_available cookie if request specifies it + if request.GET.get('show_available') is not None: + request.session['ipam_show_available'] = request.GET.get('show_available') == 'true' + + # Add available prefixes to the table if the cookie requested it + if child_prefixes and request.session.get('ipam_show_available'): child_prefixes = add_available_prefixes(prefix.prefix, child_prefixes) prefix_table = tables.PrefixDetailTable(child_prefixes) @@ -539,6 +551,7 @@ class PrefixPrefixesView(PermissionRequiredMixin, View): 'permissions': permissions, 'bulk_querystring': 'vrf_id={}&within={}'.format(prefix.vrf.pk if prefix.vrf else '0', prefix.prefix), 'active_tab': 'prefixes', + 'show_available': request.session.get('ipam_show_available'), }) @@ -553,7 +566,14 @@ class PrefixIPAddressesView(PermissionRequiredMixin, View): ipaddresses = prefix.get_child_ips().prefetch_related( 'vrf', 'interface__device', 'primary_ip4_for', 'primary_ip6_for' ) - ipaddresses = add_available_ipaddresses(prefix.prefix, ipaddresses, prefix.is_pool) + + # Update the ipam_show_available cookie if request specifies it + if request.GET.get('show_available') is not None: + request.session['ipam_show_available'] = request.GET.get('show_available') == 'true' + + # Add available IP addresses to the table if the cookie requested it + if request.session.get('ipam_show_available'): + ipaddresses = add_available_ipaddresses(prefix.prefix, ipaddresses, prefix.is_pool) ip_table = tables.IPAddressTable(ipaddresses) if request.user.has_perm('ipam.change_ipaddress') or request.user.has_perm('ipam.delete_ipaddress'): @@ -579,6 +599,7 @@ class PrefixIPAddressesView(PermissionRequiredMixin, View): 'permissions': permissions, 'bulk_querystring': 'vrf_id={}&parent={}'.format(prefix.vrf.pk if prefix.vrf else '0', prefix.prefix), 'active_tab': 'ip-addresses', + 'show_available': request.session.get('ipam_show_available'), }) diff --git a/netbox/templates/ipam/aggregate.html b/netbox/templates/ipam/aggregate.html index daa7b5107..ae79e9b66 100644 --- a/netbox/templates/ipam/aggregate.html +++ b/netbox/templates/ipam/aggregate.html @@ -40,6 +40,14 @@

{% block title %}{{ aggregate }}{% endblock %}

{% include 'inc/created_updated.html' with obj=aggregate %} + {% if show_available is not None %} +
+ +
+ {% endif %}
{% custom_links aggregate %}
diff --git a/netbox/templates/ipam/prefix.html b/netbox/templates/ipam/prefix.html index 9ea34804e..43b723553 100644 --- a/netbox/templates/ipam/prefix.html +++ b/netbox/templates/ipam/prefix.html @@ -53,6 +53,14 @@

{% block title %}{{ prefix }}{% endblock %}

{% include 'inc/created_updated.html' with obj=prefix %} + {% if show_available is not None %} +
+ +
+ {% endif %}
{% custom_links prefix %}