Skip to content

Commit 41c97d8

Browse files
committed
fix 3525
1 parent feb04f0 commit 41c97d8

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

netbox/ipam/fields.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def to_python(self, value):
3232
def get_prep_value(self, value):
3333
if not value:
3434
return None
35+
if isinstance(value, list):
36+
return [str(self.to_python(v)).split('/')[0] for v in value]
3537
return str(self.to_python(value))
3638

3739
def form_class(self):
@@ -90,5 +92,6 @@ def db_type(self, connection):
9092
IPAddressField.register_lookup(lookups.NetContains)
9193
IPAddressField.register_lookup(lookups.NetContainsOrEquals)
9294
IPAddressField.register_lookup(lookups.NetHost)
95+
IPAddressField.register_lookup(lookups.NetHostIn)
9396
IPAddressField.register_lookup(lookups.NetHostContained)
9497
IPAddressField.register_lookup(lookups.NetMaskLength)

netbox/ipam/filters.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dcim.models import Device, Interface, Region, Site
88
from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet
99
from tenancy.filtersets import TenancyFilterSet
10-
from utilities.filters import NameSlugSearchFilterSet, NumericInFilter, TagFilter, TreeNodeMultipleChoiceFilter
10+
from utilities.filters import NameSlugSearchFilterSet, NumericInFilter, TagFilter, TreeNodeMultipleChoiceFilter, MultiValueCharFilter
1111
from virtualization.models import VirtualMachine
1212
from .constants import *
1313
from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
@@ -271,7 +271,7 @@ class IPAddressFilter(TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilt
271271
method='search_by_parent',
272272
label='Parent prefix',
273273
)
274-
address = django_filters.CharFilter(
274+
address = MultiValueCharFilter(
275275
method='filter_address',
276276
label='Address',
277277
)
@@ -358,13 +358,11 @@ def search_by_parent(self, queryset, name, value):
358358
return queryset.none()
359359

360360
def filter_address(self, queryset, name, value):
361-
if not value.strip():
362-
return queryset
363361
try:
364-
# Match address and subnet mask
365-
if '/' in value:
366-
return queryset.filter(address=value)
367-
return queryset.filter(address__net_host=value)
362+
return queryset.filter(
363+
Q(address__in=value) |
364+
Q(address__net_host_in=value)
365+
)
368366
except ValidationError:
369367
return queryset.none()
370368

netbox/ipam/lookups.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ def as_sql(self, qn, connection):
100100
return 'HOST(%s) = %s' % (lhs, rhs), params
101101

102102

103+
class NetHostIn(Lookup):
104+
lookup_name = 'net_host_in'
105+
106+
def as_sql(self, qn, connection):
107+
lhs, lhs_params = self.process_lhs(qn, connection)
108+
rhs, rhs_params = self.process_rhs(qn, connection)
109+
in_elements = ['HOST(%s) IN (' % lhs]
110+
params = []
111+
for offset in range(0, len(rhs_params[0])):
112+
if offset > 0:
113+
in_elements.append(', ')
114+
params.extend(lhs_params)
115+
sqls_params = rhs_params[0][offset]
116+
in_elements.append(rhs)
117+
params.append(sqls_params)
118+
in_elements.append(')')
119+
return ''.join(in_elements), params
120+
121+
103122
class NetHostContained(Lookup):
104123
"""
105124
Check for the host portion of an IP address without regard to its mask. This allows us to find e.g. 192.0.2.1/24

0 commit comments

Comments
 (0)