Expose some more stock item data over API

This commit is contained in:
Oliver Walters 2020-05-22 23:33:27 +10:00
parent 203062a67a
commit 4bd0872b2c
4 changed files with 17 additions and 15 deletions

View File

@ -105,9 +105,11 @@ class PartBriefSerializer(InvenTreeModelSerializer):
'thumbnail', 'thumbnail',
'active', 'active',
'assembly', 'assembly',
'is_template',
'purchaseable', 'purchaseable',
'salable', 'salable',
'stock', 'stock',
'trackable',
'virtual', 'virtual',
] ]

View File

@ -80,21 +80,10 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
def get_serializer(self, *args, **kwargs): def get_serializer(self, *args, **kwargs):
try: kwargs['part_detail'] = True
kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', False)) kwargs['location_detail'] = True
except AttributeError: kwargs['supplier_part_detail'] = True
pass kwargs['test_detail'] = True
try:
kwargs['location_detail'] = str2bool(self.request.query_params.get('location_detail', False))
except AttributeError:
pass
try:
kwargs['supplier_part_detail'] = str2bool(self.request.query_params.get('supplier_detail', False))
except AttributeError:
pass
kwargs['context'] = self.get_serializer_context() kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs) return self.serializer_class(*args, **kwargs)

View File

@ -1007,6 +1007,10 @@ class StockItem(MPTTModel):
'failed': failed, 'failed': failed,
} }
@property
def required_test_count(self):
return self.part.getRequiredTests().count()
def hasRequiredTests(self): def hasRequiredTests(self):
return self.part.getRequiredTests().count() > 0 return self.part.getRequiredTests().count() > 0

View File

@ -108,11 +108,14 @@ class StockItemSerializer(InvenTreeModelSerializer):
quantity = serializers.FloatField() quantity = serializers.FloatField()
allocated = serializers.FloatField() allocated = serializers.FloatField()
required_tests = serializers.IntegerField(source='required_test_count', read_only=True)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
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) supplier_part_detail = kwargs.pop('supplier_part_detail', False)
test_detail = kwargs.pop('test_detail', False)
super(StockItemSerializer, self).__init__(*args, **kwargs) super(StockItemSerializer, self).__init__(*args, **kwargs)
@ -125,6 +128,9 @@ class StockItemSerializer(InvenTreeModelSerializer):
if supplier_part_detail is not True: if supplier_part_detail is not True:
self.fields.pop('supplier_part_detail') self.fields.pop('supplier_part_detail')
if test_detail is not True:
self.fields.pop('required_tests')
class Meta: class Meta:
model = StockItem model = StockItem
fields = [ fields = [
@ -141,6 +147,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
'part_detail', 'part_detail',
'pk', 'pk',
'quantity', 'quantity',
'required_tests',
'sales_order', 'sales_order',
'serial', 'serial',
'supplier_part', 'supplier_part',