StockItem: filtering improvements

- Optional 'cacade' param
- Filter by null parent
This commit is contained in:
Oliver Walters 2020-04-03 11:40:37 +11:00
parent fb94949538
commit d4da6211be
2 changed files with 22 additions and 7 deletions

View File

@ -42,6 +42,10 @@ function loadStockTable(table, options) {
var params = options.params || {};
// Enforce 'cascade' option
// TODO - Make this user-configurable?
params.cascade = true;
console.log('load stock table');
table.inventreeTable({

View File

@ -396,13 +396,24 @@ class StockList(generics.ListCreateAPIView):
# Does the client wish to filter by stock location?
loc_id = self.request.query_params.get('location', None)
if loc_id:
try:
location = StockLocation.objects.get(pk=loc_id)
stock_list = stock_list.filter(location__in=location.getUniqueChildren())
cascade = str2bool(self.request.query_params.get('cascade', False))
except (ValueError, StockLocation.DoesNotExist):
pass
if loc_id is not None:
# Filter by 'null' location (i.e. top-level items)
if isNull(loc_id):
stock_list = stock_list.filter(location=None)
else:
try:
# If '?cascade=true' then include items which exist in sub-locations
if cascade:
location = StockLocation.objects.get(pk=loc_id)
stock_list = stock_list.filter(location__in=location.getUniqueChildren())
else:
stock_list = stock_list.filter(location=loc_id)
except (ValueError, StockLocation.DoesNotExist):
pass
# Does the client wish to filter by part category?
cat_id = self.request.query_params.get('category', None)