mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #2273 from SchrodingersGat/owner-filter
Adds search capability to "owner" API
This commit is contained in:
commit
8fee117870
@ -7,15 +7,16 @@ from django.core.exceptions import ObjectDoesNotExist
|
|||||||
|
|
||||||
from django.conf.urls import url, include
|
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.views import APIView
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
from .serializers import UserSerializer, OwnerSerializer
|
from users.models import RuleSet, Owner, check_user_role
|
||||||
|
from users.serializers import UserSerializer, OwnerSerializer
|
||||||
from .models import RuleSet, Owner, check_user_role
|
|
||||||
|
|
||||||
|
|
||||||
class OwnerList(generics.ListAPIView):
|
class OwnerList(generics.ListAPIView):
|
||||||
@ -26,6 +27,37 @@ class OwnerList(generics.ListAPIView):
|
|||||||
queryset = Owner.objects.all()
|
queryset = Owner.objects.all()
|
||||||
serializer_class = OwnerSerializer
|
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):
|
class OwnerDetail(generics.RetrieveAPIView):
|
||||||
"""
|
"""
|
||||||
@ -96,6 +128,17 @@ class UserList(generics.ListAPIView):
|
|||||||
serializer_class = UserSerializer
|
serializer_class = UserSerializer
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
|
|
||||||
|
filter_backends = [
|
||||||
|
DjangoFilterBackend,
|
||||||
|
filters.SearchFilter,
|
||||||
|
]
|
||||||
|
|
||||||
|
search_fields = [
|
||||||
|
'first_name',
|
||||||
|
'last_name',
|
||||||
|
'username',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class GetAuthToken(APIView):
|
class GetAuthToken(APIView):
|
||||||
""" Return authentication token for an authenticated user. """
|
""" Return authentication token for an authenticated user. """
|
||||||
|
Loading…
Reference in New Issue
Block a user