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',
'active',
'assembly',
'is_template',
'purchaseable',
'salable',
'stock',
'trackable',
'virtual',
]

View File

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

View File

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

View File

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