Part table filtering

- Filter by active status
- Filter by 'is_template' status
- Filter by 'has_stock'
- Allow cascading sublocations
- API improvements to allow new features
This commit is contained in:
Oliver Walters 2020-04-11 22:10:15 +10:00
parent d606df16f7
commit 5e706554b1
5 changed files with 48 additions and 14 deletions

View File

@ -87,17 +87,15 @@ function loadPartTable(table, url, options={}) {
* buttons: If provided, link buttons to selection status of this table * buttons: If provided, link buttons to selection status of this table
*/ */
// Default query params var params = options.parms || {};
query = options.query;
if (!options.allowInactive) { var filters = loadTableFilters("parts");
// Only display active parts
query.active = true; for (var key in params) {
filters[key] = params[key];
} }
// Include sub-category search setupFilterList("parts", $(table));
// TODO - Make this user-configurable!
query.cascade = true;
var columns = [ var columns = [
{ {
@ -217,10 +215,10 @@ function loadPartTable(table, url, options={}) {
url: url, url: url,
sortName: 'name', sortName: 'name',
method: 'get', method: 'get',
queryParams: filters,
groupBy: false,
original: params,
formatNoMatches: function() { return "No parts found"; }, formatNoMatches: function() { return "No parts found"; },
queryParams: function(p) {
return query;
},
columns: columns, columns: columns,
}); });

View File

@ -215,6 +215,21 @@ class PartList(generics.ListCreateAPIView):
building=Sum('builds__quantity', filter=build_filter), building=Sum('builds__quantity', filter=build_filter),
) )
# If we are filtering by 'has_stock' status,
# Check if the 'has_stock' quantity is zero
has_stock = self.request.query_params.get('has_stock', None)
if has_stock is not None:
has_stock = str2bool(has_stock)
if has_stock:
# Filter items which have a non-null 'in_stock' quantity above zero
data = data.exclude(in_stock=None)
data = data.filter(in_stock__gt=0)
else:
# Filter items which a null or zero 'in_stock' quantity
data = data.filter(Q(in_stock__lte=0) | Q(in_stock=None))
# Reduce the number of lookups we need to do for the part categories # Reduce the number of lookups we need to do for the part categories
categories = {} categories = {}

View File

@ -659,7 +659,7 @@ class Part(models.Model):
if total: if total:
return total return total
else: else:
return 0 return Decimal(0)
@property @property
def has_bom(self): def has_bom(self):

View File

@ -99,7 +99,7 @@
<div class='button-toolbar container-fluid' style="float: right;"> <div class='button-toolbar container-fluid' style="float: right;">
<button class='btn btn-default' id='part-export' title='Export Part Data'>Export</button> <button class='btn btn-default' id='part-export' title='Export Part Data'>Export</button>
<button class='btn btn-success' id='part-create'>New Part</button> <button class='btn btn-success' id='part-create'>New Part</button>
<div class='dropdown' style='float: right;'> <div class='btn dropdown'>
<button id='part-options' class='btn btn-primary dropdown-toggle' type='button' data-toggle="dropdown">Options<span class='caret'></span></button> <button id='part-options' class='btn btn-primary dropdown-toggle' type='button' data-toggle="dropdown">Options<span class='caret'></span></button>
<ul class='dropdown-menu'> <ul class='dropdown-menu'>
<li><a href='#' id='multi-part-category' title='Set category'>Set Category</a></li> <li><a href='#' id='multi-part-category' title='Set category'>Set Category</a></li>
@ -107,6 +107,9 @@
<li><a href='#' id='multi-part-export' title='Export'>Export Data</a></li> <li><a href='#' id='multi-part-export' title='Export'>Export Data</a></li>
</ul> </ul>
</div> </div>
<div class='filter-list' id='filter-list-parts'>
<!-- Empty div -->
</div>
</div> </div>
</div> </div>

View File

@ -59,6 +59,24 @@ function getAvailableTableFilters(tableKey) {
// Filters for the "Parts" table // Filters for the "Parts" table
if (tableKey == "parts") { if (tableKey == "parts") {
return { return {
cascade: {
type: 'bool',
title: '{% trans "Include subcategories" %}',
description: '{% trans "Include parts in subcategories" %}',
},
active: {
type: 'bool',
title: '{% trans "Active" %}',
description: '{% trans "Show active parts" %}',
},
is_template: {
type: 'bool',
title: '{% trans "Template" %}',
},
has_stock: {
type: 'bool',
title: '{% trans "Stock Available" %}'
},
}; };
} }