From 5744796506a988fb9ae4c2f9dce9930e96d11f54 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 28 Jul 2021 23:32:49 +1000 Subject: [PATCH 1/2] Adds an API filter class for the ManufacturerPart list endpoint --- InvenTree/company/api.py | 45 ++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 76b08ec27e..1d754900a5 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -6,6 +6,8 @@ Provides a JSON API for the Company app from __future__ import unicode_literals 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 generics @@ -84,6 +86,15 @@ class CompanyDetail(generics.RetrieveUpdateDestroyAPIView): 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): """ API endpoint for list view of ManufacturerPart object @@ -98,6 +109,7 @@ class ManufacturerPartList(generics.ListCreateAPIView): ) serializer_class = ManufacturerPartSerializer + filterset_class = ManufacturerPartFilter def get_serializer(self, *args, **kwargs): @@ -115,36 +127,6 @@ class ManufacturerPartList(generics.ListCreateAPIView): 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 = [ DjangoFilterBackend, filters.SearchFilter, @@ -152,6 +134,9 @@ class ManufacturerPartList(generics.ListCreateAPIView): ] filter_fields = [ + 'MPN', + 'manufacturer', + 'part', ] search_fields = [ From baa6283d20b9741125001eace0c0218842e38f48 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 28 Jul 2021 23:47:50 +1000 Subject: [PATCH 2/2] Fixes --- InvenTree/company/api.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 1d754900a5..6c51d97d66 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -91,6 +91,14 @@ class ManufacturerPartFilter(rest_filters.FilterSet): Custom API filters for the ManufacturerPart list endpoint. """ + class Meta: + model = ManufacturerPart + fields = [ + 'manufacturer', + 'MPN', + 'part', + ] + # Filter by 'active' status of linked part active = rest_filters.BooleanFilter(field_name='part__active') @@ -133,12 +141,6 @@ class ManufacturerPartList(generics.ListCreateAPIView): filters.OrderingFilter, ] - filter_fields = [ - 'MPN', - 'manufacturer', - 'part', - ] - search_fields = [ 'manufacturer__name', 'description',