1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #253: Added ability to search by prefix to IP address filters

This commit is contained in:
Jeremy Stretch
2016-08-06 16:02:57 -04:00
parent ac2aa7ea89
commit ededd3f464
3 changed files with 22 additions and 2 deletions

View File

@ -191,6 +191,10 @@ class IPAddressFilter(django_filters.FilterSet):
action='search',
label='Search',
)
parent = django_filters.MethodFilter(
action='search_by_parent',
label='Parent prefix',
)
vrf = django_filters.MethodFilter(
action='_vrf',
label='VRF',
@ -238,6 +242,16 @@ class IPAddressFilter(django_filters.FilterSet):
pass
return queryset.filter(qs_filter)
def search_by_parent(self, queryset, value):
value = value.strip()
if not value:
return queryset
try:
query = str(IPNetwork(value).cidr)
return queryset.filter(address__net_contained_or_equal=query)
except AddrFormatError:
return queryset.none()
def _vrf(self, queryset, value):
if str(value) == '':
return queryset

View File

@ -293,7 +293,9 @@ def prefix_role_choices():
class PrefixFilterForm(forms.Form, BootstrapMixin):
parent = forms.CharField(required=False, label='Search Within')
parent = forms.CharField(required=False, label='Search Within', widget=forms.TextInput(attrs={
'placeholder': 'Network',
}))
vrf = forms.MultipleChoiceField(required=False, choices=prefix_vrf_choices, label='VRF',
widget=forms.SelectMultiple(attrs={'size': 6}))
tenant = forms.MultipleChoiceField(required=False, choices=tenant_choices, label='Tenant',
@ -444,6 +446,9 @@ def ipaddress_vrf_choices():
class IPAddressFilterForm(forms.Form, BootstrapMixin):
parent = forms.CharField(required=False, label='Search Within', widget=forms.TextInput(attrs={
'placeholder': 'Prefix',
}))
family = forms.ChoiceField(required=False, choices=ipaddress_family_choices, label='Address Family')
vrf = forms.MultipleChoiceField(required=False, choices=ipaddress_vrf_choices, label='VRF',
widget=forms.SelectMultiple(attrs={'size': 6}))

View File

@ -231,7 +231,8 @@ class BootstrapMixin(forms.BaseForm):
field.widget.attrs['class'] = 'form-control'
if field.required:
field.widget.attrs['required'] = 'required'
field.widget.attrs['placeholder'] = field.label
if 'placeholder' not in field.widget.attrs:
field.widget.attrs['placeholder'] = field.label
class ConfirmationForm(forms.Form, BootstrapMixin):