diff --git a/InvenTree/common/files.py b/InvenTree/common/files.py index 9c4e7c039c..62f6426ef7 100644 --- a/InvenTree/common/files.py +++ b/InvenTree/common/files.py @@ -26,6 +26,8 @@ class FileManager: # Fields which would be helpful but are not required OPTIONAL_HEADERS = [] + OPTIONAL_MATCH_HEADERS = [] + EDITABLE_HEADERS = [] HEADERS = [] @@ -82,8 +84,8 @@ class FileManager: def update_headers(self): """ Update headers """ - self.HEADERS = self.REQUIRED_HEADERS + self.ITEM_MATCH_HEADERS + self.OPTIONAL_HEADERS - + self.HEADERS = self.REQUIRED_HEADERS + self.ITEM_MATCH_HEADERS + self.OPTIONAL_MATCH_HEADERS + self.OPTIONAL_HEADERS + def setup(self): """ Setup headers depending on the file name """ diff --git a/InvenTree/common/forms.py b/InvenTree/common/forms.py index c869d4a4f3..e166d235d8 100644 --- a/InvenTree/common/forms.py +++ b/InvenTree/common/forms.py @@ -220,3 +220,24 @@ class MatchItem(forms.Form): required=False, initial=value, ) + + # Optional item selection box + elif col_guess in file_manager.OPTIONAL_MATCH_HEADERS: + # Get item options + item_options = [(option.id, option) for option in row['match_options_' + col_guess]] + # Get item match + item_match = row['match_' + col_guess] + # Set field name + field_name = col_guess.lower() + '-' + str(row['index']) + # Set field select box + self.fields[field_name] = forms.ChoiceField( + choices=[('', '-' * 10)] + item_options, + required=False, + widget=forms.Select(attrs={ + 'class': 'select bomselect', + }) + ) + # Update select box when match was found + if item_match: + # Update initial value + self.fields[field_name].initial = item_match.id diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index ac2749a612..23799e4fec 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -751,6 +751,7 @@ class PartImport(FileManagementFormView): def get_field_selection(self): """ Fill the form fields for step 3 """ + self.file_manager.setup() # collect reference indexes idx_s = {} for col in self.file_manager.HEADERS: @@ -758,9 +759,24 @@ class PartImport(FileManagementFormView): if index >= 0: idx_s[col] = index + # fetch available elements + self.allowed_items = {} + self.matches = {} for row in self.rows: for idx in idx_s: data = row['data'][idx_s[idx]]['cell'] + + if idx in self.file_manager.OPTIONAL_MATCH_HEADERS: + try: + exact_match = self.allowed_items[idx].get(**{a:data for a in self.matches[idx]}) + except (ValueError, self.allowed_items[idx].model.DoesNotExist, self.allowed_items[idx].model.MultipleObjectsReturned): + exact_match = None + + row['match_options_' + idx] = self.allowed_items[idx] + row['match_' + idx] = exact_match + continue + + # general fields row[idx.lower()] = data def done(self, form_list, **kwargs): @@ -793,6 +809,19 @@ class PartImport(FileManagementFormView): # Create Part instances for part_data in items.values(): + + # set related parts + optional_matches = {} + for idx in self.file_manager.OPTIONAL_MATCH_HEADERS: + if idx.lower() in part_data: + try: + optional_matches[idx] = self.allowed_items[idx].get(pk=int(part_data[idx.lower()])) + except (ValueError, self.allowed_items[idx].model.DoesNotExist, self.allowed_items[idx].model.MultipleObjectsReturned): + optional_matches[idx] = None + else: + optional_matches[idx] = None + + # add part new_part = Part( name=part_data.get('name', ''), description=part_data.get('description', ''),