2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 04:25:42 +00:00

Almost there?

This commit is contained in:
eeintech
2021-05-10 17:31:29 -04:00
parent 6d8f94619b
commit c9e77edf04
5 changed files with 110 additions and 88 deletions

View File

@ -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()

View File

@ -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_<r>_col_<c>
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':