From b886e54709ec93383c8eee6d7ca1684984e7135e Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 19 Aug 2022 17:16:38 +1000 Subject: [PATCH] Build API improvements (#3581) * Improve build order lookup * Query efficiency improvements * Lazy load build allocation table * API defaults ensure consistent behaviour --- InvenTree/build/api.py | 17 ++++++++++++----- InvenTree/build/serializers.py | 4 ++++ InvenTree/build/templates/build/detail.html | 4 +++- InvenTree/stock/serializers.py | 2 -- InvenTree/templates/js/translated/build.js | 8 +++++--- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/InvenTree/build/api.py b/InvenTree/build/api.py index 3102850d4e..3ef0b2b1c8 100644 --- a/InvenTree/build/api.py +++ b/InvenTree/build/api.py @@ -365,6 +365,7 @@ class BuildItemList(ListCreateAPI): kwargs['part_detail'] = str2bool(params.get('part_detail', False)) kwargs['build_detail'] = str2bool(params.get('build_detail', False)) kwargs['location_detail'] = str2bool(params.get('location_detail', False)) + kwargs['stock_detail'] = str2bool(params.get('stock_detail', True)) except AttributeError: pass @@ -372,13 +373,19 @@ class BuildItemList(ListCreateAPI): def get_queryset(self): """Override the queryset method, to allow filtering by stock_item.part.""" - query = BuildItem.objects.all() + queryset = BuildItem.objects.all() - query = query.select_related('stock_item__location') - query = query.select_related('stock_item__part') - query = query.select_related('stock_item__part__category') + queryset = queryset.select_related( + 'bom_item', + 'bom_item__sub_part', + 'build', + 'install_into', + 'stock_item', + 'stock_item__location', + 'stock_item__part', + ) - return query + return queryset def filter_queryset(self, queryset): """Customm query filtering for the BuildItem list.""" diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 6c8c49c5bf..19371d4fba 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -856,6 +856,7 @@ class BuildItemSerializer(InvenTreeModelSerializer): build_detail = kwargs.pop('build_detail', False) part_detail = kwargs.pop('part_detail', False) location_detail = kwargs.pop('location_detail', False) + stock_detail = kwargs.pop('stock_detail', False) super().__init__(*args, **kwargs) @@ -868,6 +869,9 @@ class BuildItemSerializer(InvenTreeModelSerializer): if not location_detail: self.fields.pop('location_detail') + if not stock_detail: + self.fields.pop('stock_item_detail') + class Meta: """Serializer metaclass""" model = BuildItem diff --git a/InvenTree/build/templates/build/detail.html b/InvenTree/build/templates/build/detail.html index 26040d0fc6..e6ba0bb60b 100644 --- a/InvenTree/build/templates/build/detail.html +++ b/InvenTree/build/templates/build/detail.html @@ -455,7 +455,9 @@ function loadUntrackedStockTable() { ); } -loadUntrackedStockTable(); +onPanelLoad('allocate', function() { + loadUntrackedStockTable(); +}); {% endif %} diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 3690de5516..871df5a612 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -46,7 +46,6 @@ class LocationBriefSerializer(InvenTree.serializers.InvenTreeModelSerializer): class StockItemSerializerBrief(InvenTree.serializers.InvenTreeModelSerializer): """Brief serializers for a StockItem.""" - location_name = serializers.CharField(source='location', read_only=True) part_name = serializers.CharField(source='part.full_name', read_only=True) quantity = InvenTreeDecimalField() @@ -60,7 +59,6 @@ class StockItemSerializerBrief(InvenTree.serializers.InvenTreeModelSerializer): 'part_name', 'pk', 'location', - 'location_name', 'quantity', 'serial', 'supplier_part', diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index f5b16c87c6..52359ef45e 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -731,6 +731,7 @@ function loadBuildOrderAllocationTable(table, options={}) { options.params['part_detail'] = true; options.params['build_detail'] = true; options.params['location_detail'] = true; + options.params['stock_detail'] = true; var filters = loadTableFilters('buildorderallocation'); @@ -1718,9 +1719,10 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) { field: 'location', title: '{% trans "Location" %}', formatter: function(value, row) { - if (row.stock_item_detail.location) { - var text = row.stock_item_detail.location_name; - var url = `/stock/location/${row.stock_item_detail.location}/`; + + if (row.location && row.location_detail) { + var text = row.location_detail.name; + var url = `/stock/location/${row.location}/`; return renderLink(text, url); } else {