From c9e77edf04abcc348591ba474f4932228d91d2e3 Mon Sep 17 00:00:00 2001 From: eeintech Date: Mon, 10 May 2021 17:31:29 -0400 Subject: [PATCH] Almost there? --- InvenTree/common/forms.py | 43 ++++---- InvenTree/common/views.py | 101 +++++++++++------- .../order/order_wizard/match_fields.html | 14 +-- .../order/order_wizard/match_parts.html | 34 +++--- InvenTree/order/views.py | 6 +- 5 files changed, 110 insertions(+), 88 deletions(-) diff --git a/InvenTree/common/forms.py b/InvenTree/common/forms.py index ee70755dbc..d00b868e04 100644 --- a/InvenTree/common/forms.py +++ b/InvenTree/common/forms.py @@ -126,15 +126,17 @@ class MatchItem(forms.Form): for row in row_data: # Navigate column data for col in row['data']: + # Get column matching + col_guess = col['column'].get('guess', None) # Create input for required headers - if col['column']['guess'] in file_manager.REQUIRED_HEADERS: + if col_guess in file_manager.REQUIRED_HEADERS: # Set field name - field_name = col['column']['guess'].lower() + '-' + str(row['index']) + field_name = col_guess.lower() + '-' + str(row['index']) # Set field input box - if 'quantity' in col['column']['guess'].lower(): + if 'quantity' in col_guess.lower(): self.fields[field_name] = forms.CharField( - required=True, + required=False, widget=forms.NumberInput(attrs={ 'name': 'quantity' + str(row['index']), 'class': 'numberinput', # form-control', @@ -144,15 +146,11 @@ class MatchItem(forms.Form): 'value': row['quantity'], }) ) - else: - self.fields[field_name] = forms.Input( - required=True, - widget=forms.Select(attrs={ - }) - ) + # else: + # self.fields[field_name] = forms.TextInput() # Create item selection box - elif col['column']['guess'] in file_manager.ITEM_MATCH_HEADERS: + elif col_guess in file_manager.ITEM_MATCH_HEADERS: # Get item options item_options = [(option.id, option) for option in row['item_options']] # Get item match @@ -169,30 +167,27 @@ class MatchItem(forms.Form): ) # Update select box when match was found if item_match: - # Make it a required field - self.fields[field_name].required = True + # Make it a required field: does not validate if + # removed using JS function + # self.fields[field_name].required = True # Update initial value self.fields[field_name].initial = item_match.id # Optional entries - elif col['column']['guess'] in file_manager.OPTIONAL_HEADERS: + elif col_guess in file_manager.OPTIONAL_HEADERS: # Set field name - field_name = col['column']['guess'].lower() + '-' + str(row['index']) + field_name = col_guess.lower() + '-' + str(row['index']) # Get value - value = row.get(col['column']['guess'].lower(), '') + value = row.get(col_guess.lower(), '') # Set field input box - if 'price' in col['column']['guess'].lower(): + if 'price' in col_guess.lower(): self.fields[field_name] = MoneyField( - label=_(col['column']['guess']), + label=_(col_guess), default_currency=InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY'), decimal_places=5, max_digits=19, required=False, default_amount=value, ) - else: - self.fields[field_name] = forms.Input( - required=True, - widget=forms.Select(attrs={ - }) - ) + # else: + # self.fields[field_name] = forms.TextInput() diff --git a/InvenTree/common/views.py b/InvenTree/common/views.py index 3ebc18f5c4..757a8ded4a 100644 --- a/InvenTree/common/views.py +++ b/InvenTree/common/views.py @@ -205,8 +205,10 @@ class FileManagementFormView(MultiStepFormView): stored_data = self.storage.get_step_data(self.steps.current) if stored_data: self.get_form_table_data(stored_data) - # Set form table data - self.set_form_table_data(form=form) + else: + if form.is_valid() or self.steps.current == 'items': + # Set form table data + self.set_form_table_data(form=form) # Update context context.update({'rows': self.rows}) @@ -302,7 +304,7 @@ class FileManagementFormView(MultiStepFormView): except ValueError: continue - self.column_names[value] = col_id + self.column_names[col_id] = value # Extract the column selections (in the 'select fields' view) if item.startswith('fields-'): @@ -312,7 +314,30 @@ class FileManagementFormView(MultiStepFormView): except ValueError: continue - self.column_selections[col_name] = value + for idx, name in self.column_names.items(): + if name == col_name: + self.column_selections[idx] = value + break + + # Extract the row data + if item.startswith('row_'): + # Item should be of the format row__col_ + s = item.split('_') + + if len(s) < 4: + continue + + # Ignore row/col IDs which are not correct numeric values + try: + row_id = int(s[1]) + col_id = int(s[3]) + except ValueError: + continue + + if row_id not in self.row_data: + self.row_data[row_id] = {} + + self.row_data[row_id][col_id] = value def set_form_table_data(self, form=None): """ Set the form table data """ @@ -321,10 +346,10 @@ class FileManagementFormView(MultiStepFormView): # Re-construct the column data self.columns = [] - for key in self.column_names: + for idx, value in self.column_names.items(): header = ({ - 'name': key, - 'guess': self.column_selections.get(key, ''), + 'name': value, + 'guess': self.column_selections.get(idx, ''), }) self.columns.append(header) @@ -332,49 +357,45 @@ class FileManagementFormView(MultiStepFormView): # Re-construct the row data self.rows = [] - for row_idx in sorted(self.row_data.keys()): - row = self.row_data[row_idx] - items = [] - - for col_idx in sorted(row.keys()): - value = row[col_idx] - items.append(value) - - self.rows.append({ - 'index': row_idx, - 'column': self.columns[row_idx], - 'data': items, - 'errors': {}, - }) - else: - if self.column_names: - rows_shown = [] + # if self.column_names: + # rows_shown = [] # Update the row data - for row in self.rows: - row_data = row['data'] + for row_idx in sorted(self.row_data.keys()): + row_data = self.row_data[row_idx] data = [] - show_data = [] + # show_data = [] - for idx, item in enumerate(row_data): + for idx, item in row_data.items(): column_data = { + 'name': self.column_names[idx], + 'guess': self.column_selections[idx], + } + + cell_data = { 'cell': item, 'idx': idx, - 'column': self.columns[idx], + 'column': column_data, } - data.append(column_data) - if not self.column_names or self.columns[idx]['name'] in self.column_names: - show_data.append(column_data) + data.append(cell_data) + # if not self.column_names or column_data.get('name', '') in self.column_names: + # show_data.append(cell_data) + + row = { + 'index': row_idx, + 'data': data, + 'errors': {}, + } + self.rows.append(row) + + # if self.column_names: + # current_row = row + # current_row['data'] = show_data + # rows_shown.append(current_row) - row['data'] = data - if self.column_names: - current_row = row - current_row['data'] = show_data - rows_shown.append(current_row) - - if self.column_names and self.get_step_index() == 3: - self.rows = rows_shown + # if self.column_names and self.get_step_index() == 3: + # self.rows = rows_shown # In the item selection step: update row data to contain fields if form and self.steps.current == 'items': diff --git a/InvenTree/order/templates/order/order_wizard/match_fields.html b/InvenTree/order/templates/order/order_wizard/match_fields.html index 8885526093..652226377b 100644 --- a/InvenTree/order/templates/order/order_wizard/match_fields.html +++ b/InvenTree/order/templates/order/order_wizard/match_fields.html @@ -32,8 +32,8 @@ {% block form_content %} - {% trans "File Fields" %} + {% for col in form %}
@@ -49,8 +49,8 @@ - {% trans "Match Fields" %} + {% for col in form %} {{ col }} @@ -65,18 +65,18 @@ {% endfor %} {% for row in rows %} - {% with forloop.counter as row_index %} + {% with forloop.counter0 as row_index %} - - - {{ row_index }} + {{ row_index }} {% for item in row.data %} - {{ item.cell }} + {{ item }} {% endfor %} diff --git a/InvenTree/order/templates/order/order_wizard/match_parts.html b/InvenTree/order/templates/order/order_wizard/match_parts.html index 6cfcd334f3..18e984408f 100644 --- a/InvenTree/order/templates/order/order_wizard/match_parts.html +++ b/InvenTree/order/templates/order/order_wizard/match_parts.html @@ -4,6 +4,8 @@ {% load static %} {% block form_alert %} +{% if form.errors %} +{% endif %} {% if form_errors %}