From b7d25a75c48b4f873771a9654a3d54ba7aaef92b Mon Sep 17 00:00:00 2001 From: eeintech Date: Thu, 1 Oct 2020 10:03:49 -0500 Subject: [PATCH] Hide part toolbar, nicer part representation, improved parameters prefetching --- InvenTree/part/models.py | 32 +++++++++++---- .../templates/part/category_parametric.html | 6 +++ InvenTree/part/views.py | 23 ++++++++--- InvenTree/templates/js/part.html | 40 +++++++++++++++---- 4 files changed, 82 insertions(+), 19 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 41fcf0bfce..272afacd0e 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -111,11 +111,20 @@ class PartCategory(InvenTreeTree): """ True if there are any parts in this category """ 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 """ + 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 parameter in part.parameters.all(): @@ -123,19 +132,28 @@ class PartCategory(InvenTreeTree): if parameter_name not in unique_parameters_names: 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 """ + 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: part_parameters = { - 'IPN': part.IPN, - 'Name': part.name, + 'pk': part.pk, + '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(): parameter_name = parameter.template.name parameter_value = parameter.data diff --git a/InvenTree/part/templates/part/category_parametric.html b/InvenTree/part/templates/part/category_parametric.html index 4643dfee8a..3cf4f37a20 100644 --- a/InvenTree/part/templates/part/category_parametric.html +++ b/InvenTree/part/templates/part/category_parametric.html @@ -13,6 +13,12 @@ {% block js_ready %} {{ block.super }} + + /* Hide Button Toolbar */ + window.onload = function hideButtonToolbar() { + var toolbar = document.getElementById("button-toolbar"); + toolbar.style.display = "none"; + }; loadParametricPartTable( "#parametric-part-table", diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 0f0f21b9fc..dc8d07f5cd 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -1872,6 +1872,7 @@ class PartParameterDelete(AjaxDeleteView): class CategoryDetail(DetailView): """ Detail view for PartCategory """ + model = PartCategory context_object_name = 'category' queryset = PartCategory.objects.all().prefetch_related('children') @@ -1891,17 +1892,29 @@ class CategoryDetail(DetailView): class CategoryParametric(CategoryDetail): """ Parametric view for PartCategory """ + template_name = 'part/category_parametric.html' def get_context_data(self, **kwargs): context = super(CategoryParametric, self).get_context_data(**kwargs).copy() - category = kwargs['object'] - context['headers'] = category.get_unique_parameters() - context['headers'].append('IPN') - context['headers'].append('Name') - context['parameters'] = category.get_parts_parameters() + # Get current category + category = kwargs.get('object', None) + + if category: + 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 diff --git a/InvenTree/templates/js/part.html b/InvenTree/templates/js/part.html index 5ba46b4f31..59406e7672 100644 --- a/InvenTree/templates/js/part.html +++ b/InvenTree/templates/js/part.html @@ -179,17 +179,43 @@ function loadParametricPartTable(table, options={}) { var columns = []; for (header of table_headers) { - columns.push({ - field: header, - title: header, - sortable: true, - }) + if (header === 'part') { + columns.push({ + field: 'part', + 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({ /* url: url,*/ - sortName: 'name', - method: 'get', + sortName: 'part', +/* method: 'get',*/ queryParams: table_headers, groupBy: false, name: options.name || 'parametric',