Adds an API filter class for the ManufacturerPart list endpoint

This commit is contained in:
Oliver 2021-07-28 23:32:49 +10:00
parent 50eb70f538
commit 5744796506

View File

@ -6,6 +6,8 @@ Provides a JSON API for the Company app
from __future__ import unicode_literals from __future__ import unicode_literals
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from django_filters import rest_framework as rest_filters
from rest_framework import filters from rest_framework import filters
from rest_framework import generics from rest_framework import generics
@ -84,6 +86,15 @@ class CompanyDetail(generics.RetrieveUpdateDestroyAPIView):
return queryset return queryset
class ManufacturerPartFilter(rest_filters.FilterSet):
"""
Custom API filters for the ManufacturerPart list endpoint.
"""
# Filter by 'active' status of linked part
active = rest_filters.BooleanFilter(field_name='part__active')
class ManufacturerPartList(generics.ListCreateAPIView): class ManufacturerPartList(generics.ListCreateAPIView):
""" API endpoint for list view of ManufacturerPart object """ API endpoint for list view of ManufacturerPart object
@ -98,6 +109,7 @@ class ManufacturerPartList(generics.ListCreateAPIView):
) )
serializer_class = ManufacturerPartSerializer serializer_class = ManufacturerPartSerializer
filterset_class = ManufacturerPartFilter
def get_serializer(self, *args, **kwargs): def get_serializer(self, *args, **kwargs):
@ -115,36 +127,6 @@ class ManufacturerPartList(generics.ListCreateAPIView):
return self.serializer_class(*args, **kwargs) return self.serializer_class(*args, **kwargs)
def filter_queryset(self, queryset):
"""
Custom filtering for the queryset.
"""
queryset = super().filter_queryset(queryset)
params = self.request.query_params
# Filter by manufacturer
manufacturer = params.get('manufacturer', None)
if manufacturer is not None:
queryset = queryset.filter(manufacturer=manufacturer)
# Filter by parent part?
part = params.get('part', None)
if part is not None:
queryset = queryset.filter(part=part)
# Filter by 'active' status of the part?
active = params.get('active', None)
if active is not None:
active = str2bool(active)
queryset = queryset.filter(part__active=active)
return queryset
filter_backends = [ filter_backends = [
DjangoFilterBackend, DjangoFilterBackend,
filters.SearchFilter, filters.SearchFilter,
@ -152,6 +134,9 @@ class ManufacturerPartList(generics.ListCreateAPIView):
] ]
filter_fields = [ filter_fields = [
'MPN',
'manufacturer',
'part',
] ]
search_fields = [ search_fields = [