Skip to content

Commit

Permalink
Hack support CI-fields.
Browse files Browse the repository at this point in the history
  - Requires manual lookup declarations.
  - See #489 (comment)
  • Loading branch information
terjekv committed Jun 27, 2023
1 parent 6c94008 commit 6df5a61
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
21 changes: 21 additions & 0 deletions hostpolicy/api/v1/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ def test_create_with_date(self):
post_data = {"name": "orange", "description": "Round and orange", "create_date": "2018-07-07"}
self.assert_post('/api/v1/hostpolicy/atoms/', post_data)

def test_get_atoms_200_ok(self):
"""Getting an existing entry should return 200"""
self.assert_get('/api/v1/hostpolicy/atoms/testatom1')
ret1 = self.assert_get('/api/v1/hostpolicy/atoms/?name=testatom1')
self.assertEqual(ret1.json()['count'], 1)
self.assertEqual(ret1.json()['results'][0]['name'], 'testatom1')

ret2 = self.assert_get('/api/v1/hostpolicy/atoms/')
self.assertEqual(ret2.json()['count'], 2)

def test_get_atoms_filtered_200_ok(self):
"""Test filtering on atoms"""
ret2 = self.assert_get('/api/v1/hostpolicy/atoms/?name=testatom2')
self.assertEqual(ret2.json()['count'], 1)

ret2 = self.assert_get('/api/v1/hostpolicy/atoms/?name__contains=testatom2')
self.assertEqual(ret2.json()['count'], 1)

ret2 = self.assert_get('/api/v1/hostpolicy/atoms/?name__regex=.*testatom2.*')
self.assertEqual(ret2.json()['count'], 1)


class HostPolicyAdminRights(MregAPITestCase):
"""Test that all of the API for HostPolicy are available for the admin group
Expand Down
24 changes: 18 additions & 6 deletions hostpolicy/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import status
from rest_framework.response import Response

from django_filters import rest_framework as filters
from django_filters import rest_framework as rest_filters

from hostpolicy.models import HostPolicyAtom, HostPolicyRole
from hostpolicy.api.permissions import IsSuperOrHostPolicyAdminOrReadOnly
Expand All @@ -20,17 +20,29 @@
from . import serializers


class HostPolicyAtomFilterSet(filters.FilterSet):
# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the HostPolicyAtom model, this applies to the field "name"
class HostPolicyAtomFilterSet(rest_filters.FilterSet):
class Meta:
model = HostPolicyAtom
fields = '__all__'
fields = {
'name': ['exact', 'regex', 'contains'],
}


class HostPolicyRoleFilterSet(filters.FilterSet):
# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the HostPolicyRole model, this applies to the field "name"
class HostPolicyRoleFilterSet(rest_filters.FilterSet):
class Meta:
model = HostPolicyRole
fields = '__all__'

fields = {
'name': ['exact', 'regex', 'contains'],
'atoms__name': ['exact', 'regex', 'contains'],
'hosts__name': ['exact', 'regex', 'contains'],
'labels__name': ['exact', 'regex', 'contains'],
}

class HostPolicyAtomLogMixin(HistoryLog):

Expand Down
37 changes: 34 additions & 3 deletions mreg/api/v1/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Meta:
fields = "__all__"


# Not that due to the email field being a CIEmailField, filtering on it
# with lookups (email__contains=..., email__regex=..., etc) won't work.
# This field is inherited from BaseZone.
class ForwardZoneFilterSet(filters.FilterSet):
class Meta:
model = ForwardZone
Expand Down Expand Up @@ -79,10 +82,19 @@ class Meta:
fields = "__all__"


# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the HostGroup model, this applies to the field "name"
class HostGroupFilterSet(filters.FilterSet):
class Meta:
model = HostGroup
fields = "__all__"
fields = {
"name": ["exact", "regex", "contains"],
"description": ["exact", "regex", "contains"],
"owners__name": ["exact", "regex", "contains"],
"parent__name": ["exact", "regex", "contains"],
"hosts__name": ["exact", "regex", "contains"],
}


class IpaddressFilterSet(filters.FilterSet):
Expand All @@ -91,10 +103,16 @@ class Meta:
fields = "__all__"


# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the Label model, this applies to the field "name"
class LabelFilterSet(filters.FilterSet):
class Meta:
model = Label
fields = "__all__"
fields = {
"name": ["exact", "regex", "contains"],
"description": ["exact", "regex", "contains"],
}


class LocFilterSet(filters.FilterSet):
Expand Down Expand Up @@ -148,6 +166,9 @@ class Meta:
model = PtrOverride
fields = "__all__"

# Not that due to the email field being a CIEmailField, filtering on it
# with lookups (email__contains=..., email__regex=..., etc) won't work.
# This field is inherited from BaseZone.

class ReverseZoneFilterSet(filters.FilterSet):
network = CIDRFieldExactFilter(field_name="network")
Expand All @@ -162,11 +183,21 @@ class Meta:
model = ReverseZoneDelegation
fields = "__all__"

# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the Srv model, this applies to the field "name"

class SrvFilterSet(filters.FilterSet):
class Meta:
model = Srv
fields = "__all__"
fields = {
"name": ["exact", "contains", "regex"],
"priority": ["exact", "lt", "gt"],
"weight": ["exact", "lt", "gt"],
"port": ["exact", "lt", "gt"],
"ttl": ["exact", "lt", "gt"],
"host__name": ["exact", "contains", "regex"],
}


class SshfpFilterSet(filters.FilterSet):
Expand Down

0 comments on commit 6df5a61

Please sign in to comment.