From caf52c6ce55793eb8ebb692339eaea1f60155638 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 18 Feb 2021 00:07:44 +1100 Subject: [PATCH] Add optional detail elements to SOAllocation API --- InvenTree/order/api.py | 10 ++++++++++ InvenTree/order/serializers.py | 35 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/InvenTree/order/api.py b/InvenTree/order/api.py index d8e189dbfb..e50bc376cb 100644 --- a/InvenTree/order/api.py +++ b/InvenTree/order/api.py @@ -494,6 +494,16 @@ class SOAllocationList(generics.ListCreateAPIView): queryset = SalesOrderAllocation.objects.all() serializer_class = SalesOrderAllocationSerializer + def get_serializer(self, *args, **kwargs): + + params = self.request.query_params + + kwargs['part_detail'] = str2bool(params.get('part_detail', False)) + kwargs['item_detail'] = str2bool(params.get('item_detail', False)) + kwargs['order_detail'] = str2bool(params.get('order_detail', False)) + + return self.serializer_class(*args, **kwargs) + def filter_queryset(self, queryset): queryset = super().filter_queryset(queryset) diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index 6879cdc401..db39eec8b3 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -17,7 +17,7 @@ from InvenTree.serializers import InvenTreeAttachmentSerializerField from company.serializers import CompanyBriefSerializer, SupplierPartSerializer from part.serializers import PartBriefSerializer -from stock.serializers import LocationBriefSerializer +from stock.serializers import LocationBriefSerializer, StockItemSerializer from .models import PurchaseOrder, PurchaseOrderLineItem from .models import PurchaseOrderAttachment, SalesOrderAttachment @@ -236,13 +236,33 @@ class SalesOrderAllocationSerializer(InvenTreeModelSerializer): This includes some fields from the related model objects. """ - location_path = serializers.CharField(source='get_location_path', read_only=True) - location = serializers.IntegerField(source='get_location', read_only=True) part = serializers.PrimaryKeyRelatedField(source='item.part', read_only=True) order = serializers.PrimaryKeyRelatedField(source='line.order', many=False, read_only=True) serial = serializers.CharField(source='get_serial', read_only=True) quantity = serializers.FloatField(read_only=True) + # Extra detail fields + order_detail = SalesOrderSerializer(source='line.order', many=False, read_only=True) + part_detail = PartBriefSerializer(source='item.part', many=False, read_only=True) + item_detail = StockItemSerializer(source='item', many=False, read_only=True) + + def __init__(self, *args, **kwargs): + + order_detail = kwargs.pop('order_detail', False) + part_detail = kwargs.pop('part_detail', False) + item_detail = kwargs.pop('item_detail', False) + + super().__init__(*args, **kwargs) + + if not order_detail: + self.fields.pop('order_detail') + + if not part_detail: + self.fields.pop('part_detail') + + if not item_detail: + self.fields.pop('item_detail') + class Meta: model = SalesOrderAllocation @@ -251,11 +271,12 @@ class SalesOrderAllocationSerializer(InvenTreeModelSerializer): 'line', 'serial', 'quantity', - 'order', - 'part', - 'location', - 'location_path', 'item', + 'item_detail', + 'order', + 'order_detail', + 'part', + 'part_detail', ]