From 8ab7aa34308f50c47b835441ad75f442ede97ae2 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 2 Aug 2016 15:48:12 -0400 Subject: [PATCH] Addresses #395: Show child prefixes from all VRFs if the parent prefix is in the global table --- netbox/ipam/tables.py | 3 ++- netbox/ipam/views.py | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/netbox/ipam/tables.py b/netbox/ipam/tables.py index a7444b6b8fe..8ac21e04cd7 100644 --- a/netbox/ipam/tables.py +++ b/netbox/ipam/tables.py @@ -152,13 +152,14 @@ class Meta(BaseTable.Meta): class PrefixBriefTable(BaseTable): prefix = tables.TemplateColumn(PREFIX_LINK_BRIEF, verbose_name='Prefix') + vrf = tables.LinkColumn('ipam:vrf', args=[Accessor('vrf.pk')], default='Global', verbose_name='VRF') site = tables.LinkColumn('dcim:site', args=[Accessor('site.slug')], verbose_name='Site') status = tables.TemplateColumn(STATUS_LABEL, verbose_name='Status') role = tables.Column(verbose_name='Role') class Meta(BaseTable.Meta): model = Prefix - fields = ('prefix', 'status', 'site', 'role') + fields = ('prefix', 'vrf', 'status', 'site', 'role') orderable = False diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index dbb280c616d..95aa33b1e02 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -2,7 +2,7 @@ from django_tables2 import RequestConfig from django.contrib.auth.mixins import PermissionRequiredMixin -from django.db.models import Count +from django.db.models import Count, Q from django.shortcuts import get_object_or_404, render from dcim.models import Device @@ -281,7 +281,8 @@ def prefix(request, pk): .count() # Parent prefixes table - parent_prefixes = Prefix.objects.filter(vrf=prefix.vrf, prefix__net_contains=str(prefix.prefix))\ + parent_prefixes = Prefix.objects.filter(Q(vrf=prefix.vrf) | Q(vrf__isnull=True))\ + .filter(prefix__net_contains=str(prefix.prefix))\ .select_related('site', 'role').annotate_depth() parent_prefix_table = tables.PrefixBriefTable(parent_prefixes) @@ -291,7 +292,13 @@ def prefix(request, pk): duplicate_prefix_table = tables.PrefixBriefTable(duplicate_prefixes) # Child prefixes table - child_prefixes = Prefix.objects.filter(vrf=prefix.vrf, prefix__net_contained=str(prefix.prefix))\ + if prefix.vrf: + # If the prefix is in a VRF, show child prefixes only within that VRF. + child_prefixes = Prefix.objects.filter(vrf=prefix.vrf) + else: + # If the prefix is in the global table, show child prefixes from all VRFs. + child_prefixes = Prefix.objects.all() + child_prefixes = child_prefixes.filter(prefix__net_contained=str(prefix.prefix))\ .select_related('site', 'role').annotate_depth(limit=0) if child_prefixes: child_prefixes = add_available_prefixes(prefix.prefix, child_prefixes)