From 3d553cf7de30f71f8f81672d9adb3af2ed8977a9 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 8 Nov 2021 16:49:15 +1100 Subject: [PATCH 1/3] Adds search capability to "owner" API - Currently a bit of a hack, but it works... --- InvenTree/users/api.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/InvenTree/users/api.py b/InvenTree/users/api.py index 240d6aabc0..0bb51aab27 100644 --- a/InvenTree/users/api.py +++ b/InvenTree/users/api.py @@ -26,6 +26,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): """ From ca0619a482126c894114f59d96736eaf0cd30e67 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 8 Nov 2021 16:53:15 +1100 Subject: [PATCH 2/3] Adds API filtering for the "user" list --- InvenTree/users/api.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/InvenTree/users/api.py b/InvenTree/users/api.py index 0bb51aab27..6b6b4afcfc 100644 --- a/InvenTree/users/api.py +++ b/InvenTree/users/api.py @@ -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): @@ -127,6 +128,18 @@ 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. """ From f1eaeef949080205afa812644eac46d27b78b2fa Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 8 Nov 2021 16:53:43 +1100 Subject: [PATCH 3/3] PEP fixes --- InvenTree/users/api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/users/api.py b/InvenTree/users/api.py index 6b6b4afcfc..222f284add 100644 --- a/InvenTree/users/api.py +++ b/InvenTree/users/api.py @@ -140,7 +140,6 @@ class UserList(generics.ListAPIView): ] - class GetAuthToken(APIView): """ Return authentication token for an authenticated user. """