Explorar o código

fix(vpn): Implement quick search for TunnelTerminationFilterSet (#23331)

TunnelTerminationFilterSet never overrode search(), so the q filter
fell through to the NetBoxModelFilterSet placeholder, which returns the
queryset unchanged, and every termination matched any query in both
the REST API and the UI list view. Match on the parent tunnel's name,
as L2VPNTerminationFilterSet does for its L2VPN.

Fixes #23322
Jeremy Stretch hai 5 días
pai
achega
e2bef46689
Modificáronse 2 ficheiros con 11 adicións e 0 borrados
  1. 5 0
      netbox/vpn/filtersets.py
  2. 6 0
      netbox/vpn/tests/test_filtersets.py

+ 5 - 0
netbox/vpn/filtersets.py

@@ -138,6 +138,11 @@ class TunnelTerminationFilterSet(NetBoxModelFilterSet):
         model = TunnelTermination
         fields = ('id', 'termination_id')
 
+    def search(self, queryset, name, value):
+        if not value.strip():
+            return queryset
+        return queryset.filter(tunnel__name__icontains=value)
+
 
 @register_filterset
 class IKEProposalFilterSet(PrimaryModelFilterSet):

+ 6 - 0
netbox/vpn/tests/test_filtersets.py

@@ -256,6 +256,12 @@ class TunnelTerminationTestCase(TestCase, ChangeLoggedFilterSetTestMixin):
         )
         TunnelTermination.objects.bulk_create(tunnel_terminations)
 
+    def test_q(self):
+        params = {'q': 'Tunnel 1'}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+        params = {'q': 'foobar'}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 0)
+
     def test_tunnel(self):
         tunnels = Tunnel.objects.all()[:2]
         params = {'tunnel_id': [tunnels[0].pk, tunnels[1].pk]}