diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 272afacd0e..24c7ac1eb7 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -114,7 +114,7 @@ class PartCategory(InvenTreeTree): def prefetch_parts_parameters(self, cascade=True): """ Prefectch parts parameters """ - return self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template') + return self.get_parts(cascade=cascade).prefetch_related('parameters', 'parameters__template').all() def get_unique_parameters(self, cascade=True, prefetch=None): """ Get all unique parameter names for all parts from this category """ diff --git a/InvenTree/part/test_category.py b/InvenTree/part/test_category.py index 40f0f113a0..7fa38d7dcf 100644 --- a/InvenTree/part/test_category.py +++ b/InvenTree/part/test_category.py @@ -1,7 +1,7 @@ from django.test import TestCase from django.core.exceptions import ValidationError -from .models import Part, PartCategory +from .models import Part, PartCategory, PartParameter, PartParameterTemplate class CategoryTest(TestCase): @@ -15,6 +15,7 @@ class CategoryTest(TestCase): 'category', 'part', 'location', + 'params', ] def setUp(self): @@ -94,6 +95,31 @@ class CategoryTest(TestCase): self.assertEqual(self.electronics.item_count, self.electronics.partcount()) + def test_parameters(self): + """ Test that the Category parameters are correctly fetched """ + + # Check number of SQL queries to iterate other parameters + with self.assertNumQueries(3): + # Prefetch: 3 queries (parts, parameters and parameters_template) + fasteners = self.fasteners.prefetch_parts_parameters() + # Iterate through all parts and parameters + for fastener in fasteners: + self.assertIsInstance(fastener, Part) + for parameter in fastener.parameters.all(): + self.assertIsInstance(parameter, PartParameter) + self.assertIsInstance(parameter.template, PartParameterTemplate) + + # Test number of unique parameters + self.assertEqual(len(self.fasteners.get_unique_parameters(prefetch=fasteners)), 1) + # Test number of parameters found for each part + parts_parameters = self.fasteners.get_parts_parameters(prefetch=fasteners) + part_infos = ['pk', 'name', 'description'] + for part_parameter in parts_parameters: + # Remove part informations + for item in part_infos: + part_parameter.pop(item) + self.assertEqual(len(part_parameter), 1) + def test_invalid_name(self): # Test that an illegal character is prohibited in a category name diff --git a/InvenTree/templates/js/part.html b/InvenTree/templates/js/part.html index 3001790eac..e5fafef070 100644 --- a/InvenTree/templates/js/part.html +++ b/InvenTree/templates/js/part.html @@ -164,18 +164,16 @@ function loadSimplePartTable(table, url, options={}) { function loadParametricPartTable(table, options={}) { - /* Load parametric part data into specified table. + /* Load parametric table for part parameters * * Args: * - table: HTML reference to the table - * - table_headers: Table headers/columns + * - table_headers: Unique parameters found in category * - table_data: Parameters data */ var table_headers = options.headers var table_data = options.data -/* console.log(table_headers) - console.log(table_data)*/ var columns = []; @@ -211,19 +209,17 @@ function loadParametricPartTable(table, options={}) { title: header, sortable: true, filterControl: 'input', - clear: 'fa-times icon-red', + /* TODO: Search icons are not displayed */ + /*clear: 'fa-times icon-red',*/ }); } } $(table).inventreeTable({ -/* url: url,*/ sortName: 'part', -/* method: 'get',*/ queryParams: table_headers, groupBy: false, name: options.name || 'parametric', -/* original: params,*/ formatNoMatches: function() { return "{% trans "No parts found" %}"; }, columns: columns, showColumns: true,