From 73a1765a1185357217c5ad94e57f22f5f0930e1f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 20 Oct 2020 11:54:54 +1100 Subject: [PATCH 1/2] Fix table sorting when groups are used. This is necessary because the field names of the tables are specified like "part_detail.IPN" and multi-level string-based object access is weird. Luckily someone has worked out the hard part for me. Ref: https://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference --- .../InvenTree/static/script/inventree/tables.js | 11 +++++++++-- InvenTree/templates/js/stock.html | 13 +++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/tables.js b/InvenTree/InvenTree/static/script/inventree/tables.js index 6d57240979..1a50780393 100644 --- a/InvenTree/InvenTree/static/script/inventree/tables.js +++ b/InvenTree/InvenTree/static/script/inventree/tables.js @@ -181,8 +181,15 @@ function customGroupSorter(sortName, sortOrder, sortData) { sortData.sort(function(a, b) { // Extract default field values - var aa = a[sortName]; - var bb = b[sortName]; + // Allow multi-level access if required + // Ref: https://stackoverflow.com/a/6394168 + + function extract(obj, i) { + return obj[i]; + } + + var aa = sortName.split('.').reduce(extract, a); + var bb = sortName.split('.').reduce(extract, b); // Extract parent information var aparent = a._data && a._data['parent-index']; diff --git a/InvenTree/templates/js/stock.html b/InvenTree/templates/js/stock.html index f8388c3b62..c4209733ef 100644 --- a/InvenTree/templates/js/stock.html +++ b/InvenTree/templates/js/stock.html @@ -274,16 +274,16 @@ function loadStockTable(table, options) { var row = data[0]; - if (field == 'part_name') { + if (field == 'part_detail.name') { var name = row.part_detail.full_name; return imageHoverIcon(row.part_detail.thumbnail) + name + ' (' + data.length + ' items)'; } - else if (field == 'IPN') { + else if (field == 'part_detail.IPN') { return row.part_detail.IPN; } - else if (field == 'part_description') { + else if (field == 'part_detail.description') { return row.part_detail.description; } else if (field == 'quantity') { @@ -417,9 +417,10 @@ function loadStockTable(table, options) { switchable: false, }, { - field: 'part_name', + field: 'part_detail.name', title: '{% trans "Part" %}', sortable: true, + switchable: false, formatter: function(value, row, index, field) { var url = `/stock/item/${row.pk}/`; @@ -432,7 +433,7 @@ function loadStockTable(table, options) { } }, { - field: 'IPN', + field: 'part_detail.IPN', title: 'IPN', sortable: true, formatter: function(value, row, index, field) { @@ -440,7 +441,7 @@ function loadStockTable(table, options) { }, }, { - field: 'part_description', + field: 'part_detail.description', title: '{% trans "Description" %}', sortable: true, formatter: function(value, row, index, field) { From 4193d2e7fe93a9409ec1288bf34021208d260f8f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 20 Oct 2020 19:52:04 +1100 Subject: [PATCH 2/2] Except IntegrityError - This error may be thrown if the correct migrations have not been applied --- InvenTree/common/apps.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/InvenTree/common/apps.py b/InvenTree/common/apps.py index afdf845f15..0535709686 100644 --- a/InvenTree/common/apps.py +++ b/InvenTree/common/apps.py @@ -1,5 +1,5 @@ from django.apps import AppConfig -from django.db.utils import OperationalError, ProgrammingError +from django.db.utils import OperationalError, ProgrammingError, IntegrityError class CommonConfig(AppConfig): @@ -48,7 +48,7 @@ class CommonConfig(AppConfig): value=instance_name ) - except (OperationalError, ProgrammingError): + except (OperationalError, ProgrammingError, IntegrityError): # Migrations have not yet been applied - table does not exist pass @@ -87,6 +87,6 @@ class CommonConfig(AppConfig): setting.key = key setting.save() - except (OperationalError, ProgrammingError): + except (OperationalError, ProgrammingError, IntegrityError): # Table might not yet exist pass