Consolidate StockItem serializer

This commit is contained in:
Oliver Walters 2020-04-20 08:24:43 +10:00
parent ef66a3b8f3
commit 4ec5e9a907
2 changed files with 31 additions and 11 deletions

View File

@ -58,20 +58,28 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = StockItemSerializer serializer_class = StockItemSerializer
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
def get_queryset(self, *args, **kwargs):
queryset = super().get_queryset(*args, **kwargs)
queryset = StockItemSerializer.prefetch_queryset(queryset)
queryset = StockItemSerializer.annotate_queryset(queryset)
return queryset
def get_serializer(self, *args, **kwargs): def get_serializer(self, *args, **kwargs):
try: try:
kwargs['part_detail'] = str2bool(self.request.GET.get('part_detail', False)) kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', False))
except AttributeError: except AttributeError:
pass pass
try: try:
kwargs['location_detail'] = str2bool(self.request.GET.get('location_detail', False)) kwargs['location_detail'] = str2bool(self.request.query_params.get('location_detail', False))
except AttributeError: except AttributeError:
pass pass
try: try:
kwargs['supplier_detail'] = str2bool(self.request.GET.get('supplier_detail', False)) kwargs['supplier_part_detail'] = str2bool(self.request.query_params.get('supplier_detail', False))
except AttributeError: except AttributeError:
pass pass
@ -321,14 +329,19 @@ class StockList(generics.ListCreateAPIView):
def get_serializer(self, *args, **kwargs): def get_serializer(self, *args, **kwargs):
try: try:
part_detail = str2bool(self.request.query_params.get('part_detail', None)) kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', None))
location_detail = str2bool(self.request.query_params.get('location_detail', None))
except AttributeError: except AttributeError:
part_detail = None pass
location_detail = None
kwargs['part_detail'] = part_detail try:
kwargs['location_detail'] = location_detail kwargs['location_detail'] = str2bool(self.request.query_params.get('location_detail', None))
except AttributeError:
pass
try:
kwargs['supplier_part_detail'] = str2bool(self.request.query_params.get('supplier_part_detail', None))
except AttributeError:
pass
# Ensure the request context is passed through # Ensure the request context is passed through
kwargs['context'] = self.get_serializer_context() kwargs['context'] = self.get_serializer_context()

View File

@ -7,6 +7,7 @@ from rest_framework import serializers
from .models import StockItem, StockLocation from .models import StockItem, StockLocation
from .models import StockItemTracking from .models import StockItemTracking
from company.serializers import SupplierPartSerializer
from part.serializers import PartBriefSerializer from part.serializers import PartBriefSerializer
from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer
@ -78,13 +79,14 @@ class StockItemSerializer(InvenTreeModelSerializer):
performing database queries as efficiently as possible. performing database queries as efficiently as possible.
""" """
# TODO # TODO - Add custom annotated fields
pass return queryset
status_text = serializers.CharField(source='get_status_display', read_only=True) status_text = serializers.CharField(source='get_status_display', read_only=True)
part_detail = PartBriefSerializer(source='part', many=False, read_only=True) part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
location_detail = LocationBriefSerializer(source='location', many=False, read_only=True) location_detail = LocationBriefSerializer(source='location', many=False, read_only=True)
supplier_part_detail = SupplierPartSerializer(source='supplier_part', many=False, read_only=True)
tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True) tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True)
@ -92,6 +94,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
part_detail = kwargs.pop('part_detail', False) part_detail = kwargs.pop('part_detail', False)
location_detail = kwargs.pop('location_detail', False) location_detail = kwargs.pop('location_detail', False)
supplier_part_detail = kwargs.pop('supplier_part_detail', False)
super(StockItemSerializer, self).__init__(*args, **kwargs) super(StockItemSerializer, self).__init__(*args, **kwargs)
@ -101,6 +104,9 @@ class StockItemSerializer(InvenTreeModelSerializer):
if location_detail is not True: if location_detail is not True:
self.fields.pop('location_detail') self.fields.pop('location_detail')
if supplier_part_detail is not True:
self.fields.pop('supplier_part_detail')
class Meta: class Meta:
model = StockItem model = StockItem
fields = [ fields = [
@ -116,6 +122,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
'quantity', 'quantity',
'serial', 'serial',
'supplier_part', 'supplier_part',
'supplier_part_detail',
'status', 'status',
'status_text', 'status_text',
'tracking_items', 'tracking_items',