Skip to content

Commit

Permalink
Addresses netbox-community#395: Show child prefixes from all VRFs if …
Browse files Browse the repository at this point in the history
…the parent prefix is in the global table
  • Loading branch information
jeremystretch committed Aug 2, 2016
1 parent 7d17baa commit 8ab7aa3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion netbox/ipam/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
13 changes: 10 additions & 3 deletions netbox/ipam/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 8ab7aa3

Please sign in to comment.