Hide part toolbar, nicer part representation, improved parameters prefetching

This commit is contained in:
eeintech 2020-10-01 10:03:49 -05:00
parent a71b5ef0a0
commit b7d25a75c4
4 changed files with 82 additions and 19 deletions

View File

@ -111,11 +111,20 @@ class PartCategory(InvenTreeTree):
""" True if there are any parts in this category """ """ True if there are any parts in this category """
return self.partcount() > 0 return self.partcount() > 0
def get_unique_parameters(self, cascade=True): def prefetch_parts_parameters(self, cascade=True):
""" Prefectch parts parameters """
return self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template')
def get_unique_parameters(self, cascade=True, prefetch=None):
""" Get all unique parameter names for all parts from this category """ """ Get all unique parameter names for all parts from this category """
unique_parameters_names = [] unique_parameters_names = []
parts = self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template') if prefetch:
parts = prefetch
else:
parts = self.prefetch_parts_parameters(cascade=cascade)
for part in parts: for part in parts:
for parameter in part.parameters.all(): for parameter in part.parameters.all():
@ -123,19 +132,28 @@ class PartCategory(InvenTreeTree):
if parameter_name not in unique_parameters_names: if parameter_name not in unique_parameters_names:
unique_parameters_names.append(parameter_name) unique_parameters_names.append(parameter_name)
return unique_parameters_names return sorted(unique_parameters_names)
def get_parts_parameters(self, cascade=True): def get_parts_parameters(self, cascade=True, prefetch=None):
""" Get all parameter names and values for all parts from this category """ """ Get all parameter names and values for all parts from this category """
category_parameters = [] category_parameters = []
parts = self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template') if prefetch:
parts = prefetch
else:
parts = self.prefetch_parts_parameters(cascade=cascade)
for part in parts: for part in parts:
part_parameters = { part_parameters = {
'IPN': part.IPN, 'pk': part.pk,
'Name': part.name, 'name': part.name,
'description': part.description,
} }
# Add IPN only if it exists
if part.IPN:
part_parameters['IPN'] = part.IPN
for parameter in part.parameters.all(): for parameter in part.parameters.all():
parameter_name = parameter.template.name parameter_name = parameter.template.name
parameter_value = parameter.data parameter_value = parameter.data

View File

@ -13,6 +13,12 @@
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
/* Hide Button Toolbar */
window.onload = function hideButtonToolbar() {
var toolbar = document.getElementById("button-toolbar");
toolbar.style.display = "none";
};
loadParametricPartTable( loadParametricPartTable(
"#parametric-part-table", "#parametric-part-table",

View File

@ -1872,6 +1872,7 @@ class PartParameterDelete(AjaxDeleteView):
class CategoryDetail(DetailView): class CategoryDetail(DetailView):
""" Detail view for PartCategory """ """ Detail view for PartCategory """
model = PartCategory model = PartCategory
context_object_name = 'category' context_object_name = 'category'
queryset = PartCategory.objects.all().prefetch_related('children') queryset = PartCategory.objects.all().prefetch_related('children')
@ -1891,17 +1892,29 @@ class CategoryDetail(DetailView):
class CategoryParametric(CategoryDetail): class CategoryParametric(CategoryDetail):
""" Parametric view for PartCategory """ """ Parametric view for PartCategory """
template_name = 'part/category_parametric.html' template_name = 'part/category_parametric.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(CategoryParametric, self).get_context_data(**kwargs).copy() context = super(CategoryParametric, self).get_context_data(**kwargs).copy()
category = kwargs['object'] # Get current category
context['headers'] = category.get_unique_parameters() category = kwargs.get('object', None)
context['headers'].append('IPN')
context['headers'].append('Name') if category:
context['parameters'] = category.get_parts_parameters() cascade = kwargs.get('cascade', True)
# Prefetch parts parameters
parts_parameters = category.prefetch_parts_parameters(cascade=cascade)
# Get table headers (unique parameters names)
context['headers'] = category.get_unique_parameters(cascade=cascade,
prefetch=parts_parameters)
# Insert part information
context['headers'].insert(0, 'description')
context['headers'].insert(0, 'part')
# Get parameters data
context['parameters'] = category.get_parts_parameters(cascade=cascade,
prefetch=parts_parameters)
return context return context

View File

@ -179,17 +179,43 @@ function loadParametricPartTable(table, options={}) {
var columns = []; var columns = [];
for (header of table_headers) { for (header of table_headers) {
columns.push({ if (header === 'part') {
field: header, columns.push({
title: header, field: 'part',
sortable: true, title: '{% trans 'Part' %}',
}) sortable: true,
formatter: function(value, row, index, field) {
var name = '';
if (row.IPN) {
name += row.IPN + ' | ' + row.name;
} else {
name += row.name;
}
return renderLink(name, '/part/' + row.pk + '/');
}
});
} else if (header === 'description') {
columns.push({
field: header,
title: '{% trans 'Description' %}',
sortable: true,
});
} else {
columns.push({
field: header,
title: header,
sortable: true,
});
}
} }
$(table).inventreeTable({ $(table).inventreeTable({
/* url: url,*/ /* url: url,*/
sortName: 'name', sortName: 'part',
method: 'get', /* method: 'get',*/
queryParams: table_headers, queryParams: table_headers,
groupBy: false, groupBy: false,
name: options.name || 'parametric', name: options.name || 'parametric',