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

@ -55,12 +55,23 @@ class CategorySerializer(InvenTree.serializers.InvenTreeModelSerializer):
'parent',
'part_count',
'pathstring',
'path',
'starred',
'url',
'structural',
'icon',
]
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')
def get_starred(self, category):
"""Return True if the category is directly "starred" by the current user."""
return category in self.context.get('starred_categories', [])
@ -84,6 +95,12 @@ class CategorySerializer(InvenTree.serializers.InvenTreeModelSerializer):
starred = serializers.SerializerMethodField()
path = serializers.ListField(
child=serializers.DictField(),
source='get_path',
read_only=True,
)
class CategoryTree(InvenTree.serializers.InvenTreeModelSerializer):
"""Serializer for PartCategory tree."""
@ -481,6 +498,7 @@ class PartSerializer(InvenTree.serializers.RemoteImageMixin, InvenTree.serialize
'barcode_hash',
'category',
'category_detail',
'category_path',
'component',
'default_expiry',
'default_location',
@ -550,6 +568,7 @@ class PartSerializer(InvenTree.serializers.RemoteImageMixin, InvenTree.serialize
parameters = kwargs.pop('parameters', False)
create = kwargs.pop('create', False)
pricing = kwargs.pop('pricing', True)
path_detail = kwargs.pop('path_detail', False)
super().__init__(*args, **kwargs)
@ -559,6 +578,9 @@ class PartSerializer(InvenTree.serializers.RemoteImageMixin, InvenTree.serialize
if not parameters:
self.fields.pop('parameters')
if not path_detail:
self.fields.pop('category_path')
if not create:
# These fields are only used for the LIST API endpoint
for f in self.skip_create_fields()[1:]:
@ -670,6 +692,12 @@ class PartSerializer(InvenTree.serializers.RemoteImageMixin, InvenTree.serialize
# Extra detail for the category
category_detail = CategorySerializer(source='category', many=False, read_only=True)
category_path = serializers.ListField(
child=serializers.DictField(),
source='category.get_path',
read_only=True,
)
# Annotated fields
allocated_to_build_orders = serializers.FloatField(read_only=True)
allocated_to_sales_orders = serializers.FloatField(read_only=True)