2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 03:00:54 +00:00

Path detail API (#5569)

* Add "path_detail" to stock location serializer

- Requires ?path_detail=1 in URL query parameters
- Only avaialable on the StockLocation detail URL endpoint (not list, too expensive)

* Implement path_detail option for PartCategory detail API endpoint

* Add "path_detail" option to PartSerializer

* Add optional path_detail to StockItem serializer

* Cleanup

* Increment API version

* Add unit test for Part and PartCategory

* Remove debug statement
This commit is contained in:
Oliver
2023-09-19 17:44:06 +10:00
committed by GitHub
parent 314c93d55c
commit c60efd9a1d
8 changed files with 166 additions and 5 deletions

View File

@ -76,11 +76,18 @@ class StockDetail(RetrieveUpdateDestroyAPI):
def get_serializer(self, *args, **kwargs):
"""Set context before returning serializer."""
kwargs['part_detail'] = True
kwargs['location_detail'] = True
kwargs['supplier_part_detail'] = True
kwargs['context'] = self.get_serializer_context()
try:
params = self.request.query_params
kwargs['part_detail'] = str2bool(params.get('part_detail', True))
kwargs['location_detail'] = str2bool(params.get('location_detail', True))
kwargs['supplier_part_detail'] = str2bool(params.get('supplier_part_detail', True))
kwargs['path_detail'] = str2bool(params.get('path_detail', False))
except AttributeError:
pass
return self.serializer_class(*args, **kwargs)
@ -1343,6 +1350,20 @@ class LocationDetail(CustomRetrieveUpdateDestroyAPI):
queryset = StockLocation.objects.all()
serializer_class = StockSerializers.LocationSerializer
def get_serializer(self, *args, **kwargs):
"""Add extra context to serializer based on provided query parameters"""
try:
params = self.request.query_params
kwargs['path_detail'] = str2bool(params.get('path_detail', False))
except AttributeError:
pass
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
def get_queryset(self, *args, **kwargs):
"""Return annotated queryset for the StockLocationList endpoint"""

View File

@ -145,6 +145,7 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
'link',
'location',
'location_detail',
'location_path',
'notes',
'owner',
'packaging',
@ -205,6 +206,12 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
label=_("Part"),
)
location_path = serializers.ListField(
child=serializers.DictField(),
source='location.get_path',
read_only=True,
)
"""
Field used when creating a stock item
"""
@ -329,6 +336,7 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
location_detail = kwargs.pop('location_detail', False)
supplier_part_detail = kwargs.pop('supplier_part_detail', False)
tests = kwargs.pop('tests', False)
path_detail = kwargs.pop('path_detail', False)
super(StockItemSerializer, self).__init__(*args, **kwargs)
@ -344,6 +352,9 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
if not tests:
self.fields.pop('tests')
if not path_detail:
self.fields.pop('location_path')
class SerializeStockItemSerializer(serializers.Serializer):
"""A DRF serializer for "serializing" a StockItem.
@ -768,6 +779,7 @@ class LocationSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
'description',
'parent',
'pathstring',
'path',
'items',
'owner',
'icon',
@ -781,6 +793,16 @@ class LocationSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
'barcode_hash',
]
def __init__(self, *args, **kwargs):
"""Optionally add or remove extra fields"""
path_detail = kwargs.pop('path_detail', False)
super().__init__(*args, **kwargs)
if not path_detail:
self.fields.pop('path')
@staticmethod
def annotate_queryset(queryset):
"""Annotate extra information to the queryset"""
@ -800,6 +822,12 @@ class LocationSerializer(InvenTree.serializers.InvenTreeTagModelSerializer):
tags = TagListSerializerField(required=False)
path = serializers.ListField(
child=serializers.DictField(),
source='get_path',
read_only=True,
)
class StockItemAttachmentSerializer(InvenTree.serializers.InvenTreeAttachmentSerializer):
"""Serializer for StockItemAttachment model."""