From 6d8f97eea5805664b51591f73135820b85bd3f98 Mon Sep 17 00:00:00 2001 From: Grokzen Date: Wed, 20 Feb 2019 12:32:09 +0100 Subject: [PATCH] Rebuilt feature to be opt-in instead of opt-out so that default/old behaviour is preserved by default. Added settings flag to controll the wanted default behaviour of the show/hide feature. Added docs for the new settingss value. --- docs/configuration/optional-settings.md | 8 +++++ netbox/ipam/views.py | 41 ++++++++++++++++++++++--- netbox/netbox/settings.py | 1 + netbox/templates/ipam/prefix.html | 12 ++++---- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/docs/configuration/optional-settings.md b/docs/configuration/optional-settings.md index 65ac588b6..17b0b8166 100644 --- a/docs/configuration/optional-settings.md +++ b/docs/configuration/optional-settings.md @@ -255,6 +255,14 @@ Enable this option to run the webhook backend. See the docs section on the webho --- +## HIDE_AVAILABLE_PREFIXES + +Default: False + +Enable this option if you want to hide all available child prefixes and child ip addresses that is shown by default if this option is set to False. + +--- + ## Date and Time Formatting You may define custom formatting for date and times. For detailed instructions on writing format strings, please see [the Django documentation](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date). diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 84b9bd7de..c70f72563 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -500,10 +500,23 @@ class PrefixPrefixesView(View): 'site', 'vlan', 'role', ).annotate_depth(limit=0) - # Annotate available prefixes - if request.GET.get('show_available', None): - if child_prefixes: + if not settings.HIDE_AVAILABLE_PREFIXES: + # Show all available unless explicit set not to show + if request.GET.get('show_available', '') == 'off': + show_available = False + else: + # Default to show all available when default configuration is set child_prefixes = add_available_prefixes(prefix.prefix, child_prefixes) + show_available = True + else: + # Default set to always hide available, + # but explicit want to show addresses anyway + if request.GET.get('show_available', '') == 'on': + child_prefixes = add_available_prefixes(prefix.prefix, child_prefixes) + show_available = True + else: + # Setting is set to True then hide them by default + show_available = False prefix_table = tables.PrefixDetailTable(child_prefixes) if request.user.has_perm('ipam.change_prefix') or request.user.has_perm('ipam.delete_prefix'): @@ -529,6 +542,7 @@ class PrefixPrefixesView(View): 'permissions': permissions, 'bulk_querystring': 'vrf_id={}&within={}'.format(prefix.vrf.pk if prefix.vrf else '0', prefix.prefix), 'active_tab': 'prefixes', + 'show_available': show_available, }) @@ -542,8 +556,24 @@ class PrefixIPAddressesView(View): ipaddresses = prefix.get_child_ips().select_related( 'vrf', 'interface__device', 'primary_ip4_for', 'primary_ip6_for' ) - if request.GET.get('show_available', None): - ipaddresses = add_available_ipaddresses(prefix.prefix, ipaddresses, prefix.is_pool) + + if not settings.HIDE_AVAILABLE_PREFIXES: + # Show all available unless explicit set not to show + if request.GET.get('show_available', '') == 'off': + show_available = False + else: + # Default to show all available when default configuration is set + ipaddresses = add_available_ipaddresses(prefix.prefix, ipaddresses, prefix.is_pool) + show_available = True + else: + # Default set to always hide available, + # but explicit want to show addresses anyway + if request.GET.get('show_available', '') == 'on': + ipaddresses = add_available_ipaddresses(prefix.prefix, ipaddresses, prefix.is_pool) + show_available = True + else: + # Setting is set to True then hide them by default + show_available = False ip_table = tables.IPAddressTable(ipaddresses) if request.user.has_perm('ipam.change_ipaddress') or request.user.has_perm('ipam.delete_ipaddress'): @@ -569,6 +599,7 @@ class PrefixIPAddressesView(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': show_available, }) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index a85a5d78e..c1ec92be7 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -74,6 +74,7 @@ SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s') TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a') TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC') WEBHOOKS_ENABLED = getattr(configuration, 'WEBHOOKS_ENABLED', False) +HIDE_AVAILABLE_PREFIXES = getattr(configuration, 'HIDE_AVAILABLE_PREFIXES', False) CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS diff --git a/netbox/templates/ipam/prefix.html b/netbox/templates/ipam/prefix.html index a4d19688e..1d39a95a0 100644 --- a/netbox/templates/ipam/prefix.html +++ b/netbox/templates/ipam/prefix.html @@ -54,22 +54,22 @@ {% include 'inc/created_updated.html' with obj=prefix %}
{% endblock %}