API updates

- Allow filtering of POLineItem list endpoint by base part instance
- Include "order detail" in POLineItem serializer
This commit is contained in:
Oliver 2021-12-03 20:53:59 +11:00
parent 977f0e7e8d
commit 43ee4e390a
3 changed files with 36 additions and 2 deletions

View File

@ -12,11 +12,15 @@ import common.models
INVENTREE_SW_VERSION = "0.6.0 dev" INVENTREE_SW_VERSION = "0.6.0 dev"
# InvenTree API version # InvenTree API version
INVENTREE_API_VERSION = 19 INVENTREE_API_VERSION = 20
""" """
Increment this API version number whenever there is a significant change to the API that any clients need to know about Increment this API version number whenever there is a significant change to the API that any clients need to know about
v20 -> 2021-12-03
- Adds ability to filter POLineItem endpoint by "base_part"
- Adds optional "order_detail" to POLineItem list endpoint
v19 -> 2021-12-02 v19 -> 2021-12-02
- Adds the ability to filter the StockItem API by "part_tree" - Adds the ability to filter the StockItem API by "part_tree"
- Returns only stock items which match a particular part.tree_id field - Returns only stock items which match a particular part.tree_id field

View File

@ -13,7 +13,6 @@ from rest_framework import generics
from rest_framework import filters, status from rest_framework import filters, status
from rest_framework.response import Response from rest_framework.response import Response
from InvenTree.filters import InvenTreeOrderingFilter from InvenTree.filters import InvenTreeOrderingFilter
from InvenTree.helpers import str2bool from InvenTree.helpers import str2bool
from InvenTree.api import AttachmentMixin from InvenTree.api import AttachmentMixin
@ -300,6 +299,7 @@ class POLineItemList(generics.ListCreateAPIView):
try: try:
kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', False)) kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', False))
kwargs['order_detail'] = str2bool(self.request.query_params.get('order_detail', False))
except AttributeError: except AttributeError:
pass pass
@ -307,6 +307,28 @@ class POLineItemList(generics.ListCreateAPIView):
return self.serializer_class(*args, **kwargs) return self.serializer_class(*args, **kwargs)
def filter_queryset(self, queryset):
"""
Additional filtering options
"""
params = self.request.query_params
queryset = super().filter_queryset(queryset)
base_part = params.get('base_part', None)
if base_part:
try:
base_part = Part.objects.get(pk=base_part)
queryset = queryset.filter(part__part=base_part)
except (ValueError, Part.DoesNotExist):
pass
return queryset
filter_backends = [ filter_backends = [
rest_filters.DjangoFilterBackend, rest_filters.DjangoFilterBackend,
filters.SearchFilter, filters.SearchFilter,

View File

@ -143,12 +143,17 @@ class POLineItemSerializer(InvenTreeModelSerializer):
part_detail = kwargs.pop('part_detail', False) part_detail = kwargs.pop('part_detail', False)
order_detail = kwargs.pop('order_detail', False)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if part_detail is not True: if part_detail is not True:
self.fields.pop('part_detail') self.fields.pop('part_detail')
self.fields.pop('supplier_part_detail') self.fields.pop('supplier_part_detail')
if order_detail is not True:
self.fields.pop('order_detail')
quantity = serializers.FloatField(default=1) quantity = serializers.FloatField(default=1)
received = serializers.FloatField(default=0) received = serializers.FloatField(default=0)
@ -170,6 +175,8 @@ class POLineItemSerializer(InvenTreeModelSerializer):
help_text=_('Purchase price currency'), help_text=_('Purchase price currency'),
) )
order_detail = POSerializer(source='order', read_only=True, many=False)
class Meta: class Meta:
model = PurchaseOrderLineItem model = PurchaseOrderLineItem
@ -179,6 +186,7 @@ class POLineItemSerializer(InvenTreeModelSerializer):
'reference', 'reference',
'notes', 'notes',
'order', 'order',
'order_detail',
'part', 'part',
'part_detail', 'part_detail',
'supplier_part_detail', 'supplier_part_detail',