|
2 | 2 | from rest_framework import status
|
3 | 3 | from rest_framework.response import Response
|
4 | 4 |
|
5 |
| -from django_filters import rest_framework as rest_filters |
| 5 | +from django_filters import rest_framework as filters |
| 6 | +from rest_framework import filters as rest_filters |
6 | 7 |
|
7 | 8 | from hostpolicy.api.permissions import IsSuperOrHostPolicyAdminOrReadOnly
|
8 | 9 | from hostpolicy.models import HostPolicyAtom, HostPolicyRole
|
|
18 | 19 | from mreg.mixins import LowerCaseLookupMixin
|
19 | 20 | from mreg.models.host import Host
|
20 | 21 |
|
21 |
| -from . import serializers |
22 |
| - |
23 |
| -# For some reason the name field for filtersets for HostPolicyAtom and HostPolicyRole does |
24 |
| -# not support operators (e.g. __contains, __regex) in the same way as other fields. Yes, |
25 |
| -# the name field is a LowerCaseCharField, but the operators work fine in mreg proper. |
26 |
| -# To resolve this issue, we create custom fields for the filtersets that use the name field. |
| 22 | +from mreg.api.v1.filters import STRING_OPERATORS, INT_OPERATORS |
27 | 23 |
|
28 |
| -class HostPolicyAtomFilterSet(rest_filters.FilterSet): |
29 |
| - name__contains = rest_filters.CharFilter(field_name="name", lookup_expr="contains") |
30 |
| - name__regex = rest_filters.CharFilter(field_name="name", lookup_expr="regex") |
| 24 | +from . import serializers |
31 | 25 |
|
| 26 | +# Note that related lookups don't work at the moment, so we need to do them explicitly. |
| 27 | +class HostPolicyAtomFilterSet(filters.FilterSet): |
32 | 28 | class Meta:
|
33 | 29 | model = HostPolicyAtom
|
34 |
| - fields = "__all__" |
| 30 | + fields = { |
| 31 | + "name": STRING_OPERATORS, |
| 32 | + "create_date": INT_OPERATORS, |
| 33 | + "updated_at": INT_OPERATORS, |
| 34 | + "description": STRING_OPERATORS, |
| 35 | + "roles": INT_OPERATORS, |
| 36 | + } |
| 37 | + |
35 | 38 |
|
| 39 | +class HostPolicyRoleFilterSet(filters.FilterSet): |
| 40 | + # This seems to be required due to the many-to-many relationships? |
| 41 | + atoms__name__exact = filters.CharFilter(field_name='atoms__name', lookup_expr='exact') |
| 42 | + atoms__name__contains = filters.CharFilter(field_name='atoms__name', lookup_expr='contains') |
| 43 | + atoms__name__regex = filters.CharFilter(field_name='atoms__name', lookup_expr='regex') |
| 44 | + |
| 45 | + hosts__name__exact = filters.CharFilter(field_name='hosts__name', lookup_expr='exact') |
| 46 | + hosts__name__contains = filters.CharFilter(field_name='hosts__name', lookup_expr='contains') |
| 47 | + hosts__name__regex = filters.CharFilter(field_name='hosts__name', lookup_expr='regex') |
| 48 | + |
| 49 | + labels__name__exact = filters.CharFilter(field_name='labels__name', lookup_expr='exact') |
| 50 | + labels__name__contains = filters.CharFilter(field_name='labels__name', lookup_expr='contains') |
| 51 | + labels__name__regex = filters.CharFilter(field_name='labels__name', lookup_expr='regex') |
36 | 52 |
|
37 |
| -class HostPolicyRoleFilterSet(rest_filters.FilterSet): |
38 |
| - name__contains = rest_filters.CharFilter(field_name="name", lookup_expr="contains") |
39 |
| - name__regex = rest_filters.CharFilter(field_name="name", lookup_expr="regex") |
40 | 53 | class Meta:
|
41 | 54 | model = HostPolicyRole
|
42 |
| - fields = "__all__" |
| 55 | + fields = { |
| 56 | + "name": STRING_OPERATORS, |
| 57 | + "create_date": INT_OPERATORS, |
| 58 | + "updated_at": INT_OPERATORS, |
| 59 | + "hosts": INT_OPERATORS, |
| 60 | + "atoms": INT_OPERATORS, |
| 61 | + "labels": INT_OPERATORS, |
| 62 | + } |
43 | 63 |
|
44 | 64 | class HostPolicyAtomLogMixin(HistoryLog):
|
45 | 65 |
|
46 | 66 | log_resource = 'hostpolicy_atom'
|
47 | 67 | model = HostPolicyAtom
|
| 68 | + filter_backends = ( |
| 69 | + rest_filters.SearchFilter, |
| 70 | + filters.DjangoFilterBackend, |
| 71 | + rest_filters.OrderingFilter, |
| 72 | + ) |
| 73 | + ordering_fields = "__all__" |
| 74 | + |
48 | 75 |
|
49 | 76 |
|
50 | 77 | class HostPolicyRoleLogMixin(HistoryLog):
|
51 | 78 |
|
52 | 79 | log_resource = 'hostpolicy_role'
|
53 | 80 | model = HostPolicyRole
|
| 81 | + filter_backends = ( |
| 82 | + rest_filters.SearchFilter, |
| 83 | + filters.DjangoFilterBackend, |
| 84 | + rest_filters.OrderingFilter, |
| 85 | + ) |
| 86 | + ordering_fields = "__all__" |
54 | 87 |
|
55 | 88 |
|
56 | 89 | class HostPolicyPermissionsListCreateAPIView(M2MPermissions,
|
|
0 commit comments