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,6 +205,8 @@ class FileManagementFormView(MultiStepFormView):
stored_data = self.storage.get_step_data(self.steps.current)
if stored_data:
self.get_form_table_data(stored_data)
else:
if form.is_valid() or self.steps.current == 'items':
# Set form table data
self.set_form_table_data(form=form)
@ -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['data'] = data
if self.column_names:
current_row = row
current_row['data'] = show_data
rows_shown.append(current_row)
row = {
'index': row_idx,
'data': data,
'errors': {},
}
self.rows.append(row)
if self.column_names and self.get_step_index() == 3:
self.rows = rows_shown
# 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
# In the item selection step: update row data to contain fields
if form and self.steps.current == 'items':

View File

@ -32,8 +32,8 @@
{% block form_content %}
<thead>
<tr>
<th></th>
<th>{% trans "File Fields" %}</th>
<th></th>
{% for col in form %}
<th>
<div>
@ -49,8 +49,8 @@
</thead>
<tbody>
<tr>
<td></td>
<td>{% trans "Match Fields" %}</td>
<td></td>
{% for col in form %}
<td>
{{ col }}
@ -65,18 +65,18 @@
{% endfor %}
</tr>
{% for row in rows %}
{% with forloop.counter as row_index %}
{% with forloop.counter0 as row_index %}
<tr>
<td>
<button class='btn btn-default btn-remove' onClick='removeRowFromBomWizard()' id='del_row_{{ row_index }}' style='display: inline; float: right;' title='{% trans "Remove row" %}'>
<td style='width: 32px;'>
<button class='btn btn-default btn-remove' onClick='removeRowFromBomWizard()' id='del_row_{{ row_index }}' style='display: inline; float: left;' title='{% trans "Remove row" %}'>
<span row_id='{{ row_index }}' class='fas fa-trash-alt icon-red'></span>
</button>
</td>
<td>{{ row_index }}</td>
<td style='text-align: left;'>{{ row_index }}</td>
{% for item in row.data %}
<td>
<input type='hidden' name='row_{{ row_index }}_col_{{ forloop.counter0 }}' value='{{ item }}'/>
{{ item.cell }}
{{ item }}
</td>
{% endfor %}
</tr>

View File

@ -4,6 +4,8 @@
{% load static %}
{% block form_alert %}
{% if form.errors %}
{% endif %}
{% if form_errors %}
<div class='alert alert-danger alert-block' role='alert'>
{% trans "Errors exist in the submitted data" %}
@ -21,11 +23,12 @@
{% block form_content %}
<thead>
<tr>
<th></th>
<th></th>
<th>{% trans "Row" %}</th>
<th>{% trans "Select Supplier Part" %}</th>
<th>{% trans "Quantity" %}</th>
{% for col in columns %}
{% if col.name != 'Quantity' %}
<th>
<input type='hidden' name='col_name_{{ forloop.counter0 }}' value='{{ col.name }}'/>
<input type='hidden' name='col_guess_{{ forloop.counter0 }}' value='{{ col.guess }}'/>
@ -35,6 +38,7 @@
{{ col.name }}
{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
</thead>
@ -42,13 +46,12 @@
{% for row in rows %}
<tr {% if row.errors %} style='background: #ffeaea;'{% endif %} part-select='#select_part_{{ row.index }}'>
<td>
<button class='btn btn-default btn-remove' onClick='removeRowFromBomWizard()' id='del_row_{{ forloop.counter }}' style='display: inline; float: right;' title='{% trans "Remove row" %}'>
<button class='btn btn-default btn-remove' onClick='removeRowFromBomWizard()' id='del_row_{{ forloop.counter0 }}' style='display: inline; float: right;' title='{% trans "Remove row" %}'>
<span row_id='{{ forloop.counter }}' class='fas fa-trash-alt icon-red'></span>
</button>
</td>
<td></td>
<td>
{% add row.index 1 %}
{{ row.index }}
</td>
<td>
{% for field in form.visible_fields %}
@ -60,9 +63,7 @@
<p class='help-inline'>{{ row.errors.part }}</p>
{% endif %}
</td>
{% for item in row.data %}
<td>
{% if item.column.guess == 'Quantity' %}
{% for field in form.visible_fields %}
{% if field.name == row.quantity_select %}
{{ field }}
@ -71,7 +72,11 @@
{% if row.errors.quantity %}
<p class='help-inline'>{{ row.errors.quantity }}</p>
{% endif %}
{% elif item.column.guess == 'Purchase_Price' %}
</td>
{% for item in row.data %}
{% if item.column.guess != 'Quantity' %}
<td>
{% if item.column.guess == 'Purchase_Price' %}
{% for field in form.visible_fields %}
{% if field.name == row.price_select %}
{{ field }}
@ -82,6 +87,7 @@
{% endif %}
<input type='hidden' name='row_{{ row.index }}_col_{{ forloop.counter0 }}' value='{{ item.cell }}'/>
</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}

View File

@ -651,8 +651,8 @@ class PurchaseOrderUpload(FileManagementFormView):
except (ValueError, SupplierPart.DoesNotExist, SupplierPart.MultipleObjectsReturned):
exact_match_part = None
# Check if there is a column corresponding to "Manufacturer MPN"
if m_idx >= 0:
# Check if there is a column corresponding to "Manufacturer MPN" and no exact match found yet
if m_idx >= 0 and not exact_match_part:
mpn = row['data'][m_idx]['cell']
try:
@ -755,7 +755,7 @@ class PurchaseOrderUpload(FileManagementFormView):
order=order,
part=supplier_part,
quantity=purchase_order_item['quantity'],
purchase_price=purchase_order_item['purchase_price'],
purchase_price=purchase_order_item.get('purchase_price', None),
)
try:
purchase_order_line_item.save()