Skip to content

Commit

Permalink
Fix #298 - Bugfix in circuit lookup using natural key for A/Z endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jathanism committed Jan 12, 2018
1 parent df043c0 commit 2f3ecd3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions nsot/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,35 @@ def filter_mac_address(self, queryset, value):

class CircuitFilter(ResourceFilter):
"""Filter for Circuit objects."""
endpoint_a = django_filters.MethodFilter()
endpoint_z = django_filters.MethodFilter()

class Meta:
model = models.Circuit
fields = ['endpoint_a', 'endpoint_z', 'name', 'attributes']

# FIXME(jathan): The copy/pasted methods can be ripped out once we upgrade
# filters in support of the V2 API. For now this is quicker and easier.
def filter_endpoint_a(self, queryset, value):
"""Overload to use natural key."""
if isinstance(value, int):
value = str(value)

if value.isdigit():
return queryset.filter(endpoint_a=value)
else:
return queryset.filter(endpoint_a__name_slug=value)

def filter_endpoint_z(self, queryset, value):
"""Overload to use natural key."""
if isinstance(value, int):
value = str(value)

if value.isdigit():
return queryset.filter(endpoint_z=value)
else:
return queryset.filter(endpoint_z__name_slug=value)


class ProtocolTypeFilter(django_filters.rest_framework.FilterSet):
"""Filter for ProtocolType (non-resource) objects."""
Expand Down
12 changes: 12 additions & 0 deletions tests/api_tests/test_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,30 @@ def test_filters(site, client):
# Test filter by endpoint_a
wanted = [cir2]
expected = filter_circuits(circuits, wanted)
# by ID
assert_success(
client.retrieve(cir_uri, endpoint_a=if_a2['id']),
expected
)
# by natural key
assert_success(
client.retrieve(cir_uri, endpoint_a=if_a2['name_slug']),
expected
)

# Test filter by endpoint_z
wanted = [cir1]
expected = filter_circuits(circuits, wanted)
# by ID
assert_success(
client.retrieve(cir_uri, endpoint_z=if_z1['id']),
expected
)
# by natural key
assert_success(
client.retrieve(cir_uri, endpoint_z=if_z1['name_slug']),
expected
)

# Test filter by attributes (cidr=abc246)
wanted = [cir2]
Expand Down

0 comments on commit 2f3ecd3

Please sign in to comment.