Skip to content

Commit

Permalink
Closes #1871: Enable filtering sites by parent region
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jan 3, 2019
1 parent 209a9f0 commit 0a820d9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ v2.5.3 (FUTURE)
## Enhancements

* [#1630](https://github.com/digitalocean/netbox/issues/1630) - Enable bulk editing of prefix/IP mask length
* [#1871](https://github.com/digitalocean/netbox/issues/1871) - Enable filtering sites by parent region
* [#2693](https://github.com/digitalocean/netbox/issues/2693) - Additional cable colors
* [#2726](https://github.com/digitalocean/netbox/issues/2726) - Include cables in global search

Expand Down
22 changes: 16 additions & 6 deletions netbox/dcim/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ class SiteFilter(CustomFieldFilterSet, django_filters.FilterSet):
choices=SITE_STATUS_CHOICES,
null_value=None
)
region_id = django_filters.ModelMultipleChoiceFilter(
queryset=Region.objects.all(),
region_id = django_filters.NumberFilter(
method='filter_region',
field_name='pk',
label='Region (ID)',
)
region = django_filters.ModelMultipleChoiceFilter(
field_name='region__slug',
queryset=Region.objects.all(),
to_field_name='slug',
region = django_filters.CharFilter(
method='filter_region',
field_name='slug',
label='Region (slug)',
)
tenant_id = django_filters.ModelMultipleChoiceFilter(
Expand Down Expand Up @@ -108,6 +108,16 @@ def search(self, queryset, name, value):
pass
return queryset.filter(qs_filter)

def filter_region(self, queryset, name, value):
try:
region = Region.objects.get(**{name: value})
except ObjectDoesNotExist:
return queryset.none()
return queryset.filter(
Q(region=region) |
Q(region__in=region.get_descendants())
)


class RackGroupFilter(django_filters.FilterSet):
q = django_filters.CharFilter(
Expand Down
3 changes: 2 additions & 1 deletion netbox/dcim/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ class SiteFilterForm(BootstrapMixin, CustomFieldFilterForm):
required=False
)
region = FilterTreeNodeMultipleChoiceField(
queryset=Region.objects.annotate(filter_count=Count('sites')),
queryset=Region.objects.all(),
to_field_name='slug',
required=False,
count_attr='site_count'
)
tenant = FilterChoiceField(
queryset=Tenant.objects.annotate(filter_count=Count('sites')),
Expand Down
7 changes: 7 additions & 0 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ def to_csv(self):
self.parent.name if self.parent else None,
)

@property
def site_count(self):
return Site.objects.filter(
Q(region=self) |
Q(region__in=self.get_descendants())
).count()


#
# Sites
Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def post(self, request):
#

class RegionListView(ObjectListView):
queryset = Region.objects.annotate(site_count=Count('sites'))
queryset = Region.objects.all()
filter = filters.RegionFilter
filter_form = forms.RegionFilterForm
table = tables.RegionTable
Expand Down
8 changes: 5 additions & 3 deletions netbox/utilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,9 @@ def __iter__(self):
class FilterChoiceFieldMixin(object):
iterator = FilterChoiceIterator

def __init__(self, null_label=None, *args, **kwargs):
def __init__(self, null_label=None, count_attr='filter_count', *args, **kwargs):
self.null_label = null_label
self.count_attr = count_attr
if 'required' not in kwargs:
kwargs['required'] = False
if 'widget' not in kwargs:
Expand All @@ -515,8 +516,9 @@ def __init__(self, null_label=None, *args, **kwargs):

def label_from_instance(self, obj):
label = super().label_from_instance(obj)
if hasattr(obj, 'filter_count'):
return '{} ({})'.format(label, obj.filter_count)
obj_count = getattr(obj, self.count_attr, None)
if obj_count is not None:
return '{} ({})'.format(label, obj_count)
return label


Expand Down

0 comments on commit 0a820d9

Please sign in to comment.