From 0c60387626f8394d5ca88cd1e3082f33a9c2ae61 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 14 Oct 2021 16:50:56 +1100 Subject: [PATCH 1/3] Extract a list of existing substitute parts from the form --- InvenTree/templates/js/translated/bom.js | 25 +++++++++++++++++++++- InvenTree/templates/js/translated/forms.js | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/InvenTree/templates/js/translated/bom.js b/InvenTree/templates/js/translated/bom.js index 7cab63153b..2cfae3a361 100644 --- a/InvenTree/templates/js/translated/bom.js +++ b/InvenTree/templates/js/translated/bom.js @@ -157,6 +157,19 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) { } } + // Extract a list of all existing "substitute" id values + function getSubstituteIdValues(modal) { + + var id_values = []; + + $(modal).find('.substitute-row').each(function(el) { + var part = $(this).attr('part'); + id_values.push(part); + }); + + return id_values; + } + function renderSubstituteRow(substitute) { var pk = substitute.pk; @@ -171,7 +184,7 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) { // Render a single row var html = ` - + ${thumb} ${part.full_name} @@ -246,6 +259,16 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) { }, part: { required: false, + adjustFilters: function(query, opts) { + + var subs = getSubstituteIdValues(opts.modal); + + if (subs.length > 0) { + query.exclude_id = subs; + } + + return query; + } }, }, preFormContent: html, diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index fecf5f1bd6..2483263219 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -1349,7 +1349,7 @@ function initializeRelatedField(field, fields, options) { // Allow custom run-time filter augmentation if ('adjustFilters' in field) { - query = field.adjustFilters(query); + query = field.adjustFilters(query, options); } return query; From 9b00ede61a11913b5e2e197a4f825216cd5778f8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 14 Oct 2021 17:12:08 +1100 Subject: [PATCH 2/3] Add part queryset filtering to exclude particular ID values --- InvenTree/part/api.py | 21 +++++++++++++++++++++ InvenTree/templates/js/translated/bom.js | 7 +++++++ 2 files changed, 28 insertions(+) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index dccc2f9ac1..4dd9841332 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -814,6 +814,27 @@ class PartList(generics.ListCreateAPIView): except (ValueError, Part.DoesNotExist): pass + # Exclude specific part ID values? + exclude_id = [] + + for key in ['exclude_id', 'exclude_id[]']: + if key in params: + exclude_id += params.getlist(key, []) + + if exclude_id: + + id_values = [] + + for val in exclude_id: + try: + # pk values must be integer castable + val = int(val) + id_values.append(val) + except ValueError: + pass + + queryset = queryset.exclude(pk__in=id_values) + # Exclude part variant tree? exclude_tree = params.get('exclude_tree', None) diff --git a/InvenTree/templates/js/translated/bom.js b/InvenTree/templates/js/translated/bom.js index 2cfae3a361..71cf380952 100644 --- a/InvenTree/templates/js/translated/bom.js +++ b/InvenTree/templates/js/translated/bom.js @@ -263,6 +263,12 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) { var subs = getSubstituteIdValues(opts.modal); + // Also exclude the "master" part (if provided) + if (options.sub_part) { + subs.push(options.sub_part); + console.log("sub_part:", options.sub_part); + } + if (subs.length > 0) { query.exclude_id = subs; } @@ -824,6 +830,7 @@ function loadBomTable(table, options) { subs, { table: table, + sub_part: row.sub_part, } ); }); From 4327cbedceeb80689d293040ae41dfcbbb9ecfe1 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 14 Oct 2021 18:01:16 +1100 Subject: [PATCH 3/3] Remove debug message --- InvenTree/templates/js/translated/bom.js | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/templates/js/translated/bom.js b/InvenTree/templates/js/translated/bom.js index 71cf380952..89ae428bcd 100644 --- a/InvenTree/templates/js/translated/bom.js +++ b/InvenTree/templates/js/translated/bom.js @@ -266,7 +266,6 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) { // Also exclude the "master" part (if provided) if (options.sub_part) { subs.push(options.sub_part); - console.log("sub_part:", options.sub_part); } if (subs.length > 0) {