Skip to content

Commit

Permalink
Hostpolicy filter "fixes", also bump dependencies.
Browse files Browse the repository at this point in the history
  - We are stuck on drf 3.14.0 due to encode/django-rest-framework#9358 until encode/django-rest-framework#9483 goes into prod, hopefully 3.15.3.
  • Loading branch information
terjekv committed Aug 7, 2024
1 parent 9253c29 commit 6757d93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
52 changes: 37 additions & 15 deletions hostpolicy/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from rest_framework import status
from rest_framework.response import Response

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

from hostpolicy.api.permissions import IsSuperOrHostPolicyAdminOrReadOnly
from hostpolicy.models import HostPolicyAtom, HostPolicyRole
Expand All @@ -18,39 +19,60 @@
from mreg.mixins import LowerCaseLookupMixin
from mreg.models.host import Host

from . import serializers

# For some reason the name field for filtersets for HostPolicyAtom and HostPolicyRole does
# not support operators (e.g. __contains, __regex) in the same way as other fields. Yes,
# the name field is a LowerCaseCharField, but the operators work fine in mreg proper.
# To resolve this issue, we create custom fields for the filtersets that use the name field.
from mreg.api.v1.filters import STRING_OPERATORS, INT_OPERATORS

class HostPolicyAtomFilterSet(rest_filters.FilterSet):
name__contains = rest_filters.CharFilter(field_name="name", lookup_expr="contains")
name__regex = rest_filters.CharFilter(field_name="name", lookup_expr="regex")
from . import serializers

# Note that related lookups don't work at the moment, so we need to do them explicitly.
class HostPolicyAtomFilterSet(filters.FilterSet):
class Meta:
model = HostPolicyAtom
fields = "__all__"
fields = {
"name": STRING_OPERATORS,
"create_date": INT_OPERATORS,
"updated_at": INT_OPERATORS,
"description": STRING_OPERATORS,
"roles": INT_OPERATORS,
}


class HostPolicyRoleFilterSet(rest_filters.FilterSet):
name__contains = rest_filters.CharFilter(field_name="name", lookup_expr="contains")
name__regex = rest_filters.CharFilter(field_name="name", lookup_expr="regex")
class HostPolicyRoleFilterSet(filters.FilterSet):
atoms__name__exact = filters.CharFilter(field_name='atoms__name', lookup_expr='exact')

class Meta:
model = HostPolicyRole
fields = "__all__"
fields = {
"name": STRING_OPERATORS,
"create_date": INT_OPERATORS,
"updated_at": INT_OPERATORS,
"hosts": INT_OPERATORS,
"atoms": INT_OPERATORS,
"labels": INT_OPERATORS,
}

class HostPolicyAtomLogMixin(HistoryLog):

log_resource = 'hostpolicy_atom'
model = HostPolicyAtom
filter_backends = (
rest_filters.SearchFilter,
filters.DjangoFilterBackend,
rest_filters.OrderingFilter,
)
ordering_fields = "__all__"



class HostPolicyRoleLogMixin(HistoryLog):

log_resource = 'hostpolicy_role'
model = HostPolicyRole
filter_backends = (
rest_filters.SearchFilter,
filters.DjangoFilterBackend,
rest_filters.OrderingFilter,
)
ordering_fields = "__all__"


class HostPolicyPermissionsListCreateAPIView(M2MPermissions,
Expand Down
6 changes: 3 additions & 3 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ djangorestframework==3.14.0
django-auth-ldap==4.8.0
django-logging-json==1.15
django-netfields==1.3.2
django-filter==24.2
structlog==24.1.0
django-filter==24.3
structlog==24.4.0
rich==13.7.1
gunicorn==22.0.0
idna==3.7
psycopg2-binary==2.9.9
pika==1.3.2
sentry-sdk==2.3.1
sentry-sdk==2.12.0
tzdata==2024.1
# Testing framwork
tox
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Django==5.0.6
Django==5.0.8
djangorestframework==3.14.0
django-auth-ldap==4.8.0
django-netfields==1.3.2
django-filter==24.2
structlog==24.1.0
django-filter==24.3
structlog==24.4.0
rich==13.7.1
gunicorn==22.0.0
idna==3.7
psycopg2-binary==2.9.9
pika==1.3.2
sentry-sdk==2.3.1
sentry-sdk==2.12.0
tzdata==2024.1
# For OpenAPI schema generation.
uritemplate
Expand Down

0 comments on commit 6757d93

Please sign in to comment.