Browse Source

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

Jeremy Stretch 8 years ago
parent
commit
ededd3f464
3 changed files with 22 additions and 2 deletions
  1. 14 0
      netbox/ipam/filters.py
  2. 6 1
      netbox/ipam/forms.py
  3. 2 1
      netbox/utilities/forms.py

+ 14 - 0
netbox/ipam/filters.py

@@ -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

+ 6 - 1
netbox/ipam/forms.py

@@ -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}))

+ 2 - 1
netbox/utilities/forms.py

@@ -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):