Merge pull request #2273 from SchrodingersGat/owner-filter

Adds search capability to "owner" API
This commit is contained in:
Oliver 2021-11-08 17:11:20 +11:00 committed by GitHub
commit 8fee117870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,15 +7,16 @@ from django.core.exceptions import ObjectDoesNotExist
from django.conf.urls import url, include
from rest_framework import generics, permissions
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters, generics, permissions
from rest_framework.views import APIView
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework import status
from .serializers import UserSerializer, OwnerSerializer
from .models import RuleSet, Owner, check_user_role
from users.models import RuleSet, Owner, check_user_role
from users.serializers import UserSerializer, OwnerSerializer
class OwnerList(generics.ListAPIView):
@ -26,6 +27,37 @@ class OwnerList(generics.ListAPIView):
queryset = Owner.objects.all()
serializer_class = OwnerSerializer
def filter_queryset(self, queryset):
"""
Implement text search for the "owner" model.
Note that an "owner" can be either a group, or a user,
so we cannot do a direct text search.
A "hack" here is to post-process the queryset and simply
remove any values which do not match.
It is not necessarily "efficient" to do it this way,
but until we determine a better way, this is what we have...
"""
search_term = str(self.request.query_params.get('search', '')).lower()
queryset = super().filter_queryset(queryset)
if not search_term:
return queryset
results = []
# Extract search term f
for result in queryset.all():
if search_term in result.name().lower():
results.append(result)
return results
class OwnerDetail(generics.RetrieveAPIView):
"""
@ -96,6 +128,17 @@ class UserList(generics.ListAPIView):
serializer_class = UserSerializer
permission_classes = (permissions.IsAuthenticated,)
filter_backends = [
DjangoFilterBackend,
filters.SearchFilter,
]
search_fields = [
'first_name',
'last_name',
'username',
]
class GetAuthToken(APIView):
""" Return authentication token for an authenticated user. """