2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

Rendering for part selection step

This commit is contained in:
Oliver Walters 2019-07-09 19:21:54 +10:00
parent dff8d1fb95
commit e9eb814990
3 changed files with 88 additions and 43 deletions

View File

@ -54,7 +54,7 @@
<td></td> <td></td>
{% for col in bom_columns %} {% for col in bom_columns %}
<td> <td>
<select class='select' id='id_col_{{ forloop.counter0 }}' name='col_select_{{ forloop.counter0 }}'> <select class='select' id='id_col_{{ forloop.counter0 }}' name='col_guess_{{ forloop.counter0 }}'>
<option value=''>---------</option> <option value=''>---------</option>
{% for req in bom_headers %} {% for req in bom_headers %}
<option value='{{ req }}'{% if req == col.guess %}selected='selected'{% endif %}>{{ req }}</option> <option value='{{ req }}'{% if req == col.guess %}selected='selected'{% endif %}>{{ req }}</option>

View File

@ -11,6 +11,8 @@
<form method="post" action='' class='js-modal-form' enctype="multipart/form-data"> <form method="post" action='' class='js-modal-form' enctype="multipart/form-data">
<button type="submit" class="save btn btn-default">Submit BOM</button>
{% csrf_token %} {% csrf_token %}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
@ -24,6 +26,8 @@
<th>Row</th> <th>Row</th>
{% for col in bom_columns %} {% for col in bom_columns %}
<th> <th>
<input type='hidden' name='col_name_{{ forloop.counter0 }}' value='{{ col.name }}'/>
<input type='hidden' name='col_guess_{{ forloop.counter0 }}' value='{{ col.guess }}'/>
{% if col.guess %} {% if col.guess %}
{{ col.guess }} {{ col.guess }}
{% else %} {% else %}
@ -69,6 +73,7 @@
{% else %} {% else %}
{{ item.cell }} {{ item.cell }}
{% endif %} {% endif %}
<input type='hidden' name='row_{{ row.index }}_col_{{ forloop.counter0 }}' value='{{ item.cell }}'/>
</td> </td>
{% endfor %} {% endfor %}
</tr> </tr>

View File

@ -696,8 +696,7 @@ class BomUpload(FormView):
data.append({ data.append({
'cell': item, 'cell': item,
'idx': idx, 'idx': idx,
'column': self.bom_columns[idx], 'column': self.bom_columns[idx]
'editable': self.bom_columns[idx].get('guess', None) in BomUploadManager.EDITABLE_HEADERS
}) })
rows.append({ rows.append({
@ -860,34 +859,47 @@ class BomUpload(FormView):
self.bom_columns = bom.columns() self.bom_columns = bom.columns()
self.bom_rows = bom.rows() self.bom_rows = bom.rows()
def getTableDataFromPost(self):
""" Extract table cell data from POST request.
These data are used to maintain state between sessions.
Table data keys are as follows:
col_name_<idx> - Column name at idx as provided in the uploaded file
col_guess_<idx> - Column guess at idx as selected in the BOM
row_<x>_col<y> - Cell data as provided in the uploaded file
def handleFieldSelection(self):
""" Handle the output of the field selection form.
Here the user is presented with the raw data and must select the
column names and which rows to process.
""" """
# Map the columns # Map the columns
self.column_names = {} self.column_names = {}
self.column_selections = {} self.column_selections = {}
row_data = {} self.row_data = {}
for item in self.request.POST: for item in self.request.POST:
value = self.request.POST[item] value = self.request.POST[item]
# Column names as passed as col_name_<idx> where idx is an integer
# Extract the column names # Extract the column names
if item.startswith('col_name_'): if item.startswith('col_name_'):
col_id = int(item.replace('col_name_', '')) try:
col_id = int(item.replace('col_name_', ''))
except ValueError:
continue
col_name = value col_name = value
self.column_names[col_id] = col_name self.column_names[col_id] = col_name
# Extract the column selections # Extract the column selections (in the 'select fields' view)
if item.startswith('col_select_'): if item.startswith('col_guess_'):
try:
col_id = int(item.replace('col_guess_', ''))
except ValueError:
continue
col_id = int(item.replace('col_select_', ''))
col_name = value col_name = value
self.column_selections[col_id] = value self.column_selections[col_id] = value
@ -900,38 +912,60 @@ class BomUpload(FormView):
if len(s) < 4: if len(s) < 4:
continue continue
row_id = int(s[1]) # Ignore row/col IDs which are not correct numeric values
col_id = int(s[3]) try:
row_id = int(s[1])
col_id = int(s[3])
except ValueError:
continue
if row_id not in row_data: if row_id not in self.row_data:
row_data[row_id] = {} self.row_data[row_id] = {}
row_data[row_id][col_id] = value self.row_data[row_id][col_id] = value
col_ids = sorted(self.column_names.keys()) self.col_ids = sorted(self.column_names.keys())
# Re-construct the data table
self.bom_rows = []
for row_idx in sorted(self.row_data.keys()):
row = self.row_data[row_idx]
items = []
for col_idx in sorted(row.keys()):
#if col_idx not in self.column_selections.keys():
# continue
value = row[col_idx]
items.append(value)
self.bom_rows.append({'index': row_idx, 'data': items})
# Construct the column data
self.bom_columns = [] self.bom_columns = []
# Track any duplicate column selections # Track any duplicate column selections
duplicates = False self.duplicates = False
for col in col_ids: for col in self.col_ids:
if col not in self.column_selections:
continue if col in self.column_selections:
guess = self.column_selections[col]
else:
guess = None
header = ({ header = ({
'name': self.column_names[col], 'name': self.column_names[col],
'guess': self.column_selections[col] 'guess': guess
}) })
# Duplicate guess?
guess = self. column_selections[col]
if guess: if guess:
n = list(self.column_selections.values()).count(self.column_selections[col]) n = list(self.column_selections.values()).count(self.column_selections[col])
if n > 1: if n > 1:
header['duplicate'] = True header['duplicate'] = True
duplicates = True self.duplicates = True
self.bom_columns.append(header) self.bom_columns.append(header)
@ -942,35 +976,39 @@ class BomUpload(FormView):
if col not in self.column_selections.values(): if col not in self.column_selections.values():
self.missing_columns.append(col) self.missing_columns.append(col)
# Re-construct the data table
self.bom_rows = []
for row_idx in sorted(row_data.keys()): def handleFieldSelection(self):
row = row_data[row_idx] """ Handle the output of the field selection form.
items = [] Here the user is presented with the raw data and must select the
for col_idx in sorted(row.keys()): column names and which rows to process.
"""
if col_idx not in self.column_selections.keys(): # Extract POST data
continue self.getTableDataFromPost()
value = row[col_idx] valid = len(self.missing_columns) == 0 and not self.duplicates
items.append(value)
self.bom_rows.append({'index': row_idx, 'data': items})
valid = len(self.missing_columns) == 0 and not duplicates
form = part_forms.BomUploadSelectFields form = part_forms.BomUploadSelectFields
if valid: if valid:
# Try to extract meaningful data # Try to extract meaningful data
self.preFillSelections() self.preFillSelections()
form = self.template_name = 'part/bom_upload/select_parts.html' form = None
self.template_name = 'part/bom_upload/select_parts.html'
else: else:
self.template_name = 'part/bom_upload/select_fields.html' self.template_name = 'part/bom_upload/select_fields.html'
return self.render_to_response(self.get_context_data(form=form)) return self.render_to_response(self.get_context_data(form=form))
def handlePartSelection(self):
# Extract POST data
self.getTableDataFromPost()
self.template_name = 'part/bom_upload/select_parts.html'
return self.render_to_response(self.get_context_data(form=None))
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
""" Perform the various 'POST' requests required. """ Perform the various 'POST' requests required.
""" """
@ -989,6 +1027,8 @@ class BomUpload(FormView):
return self.handleBomFileUpload() return self.handleBomFileUpload()
elif form_step == 'select_fields': elif form_step == 'select_fields':
return self.handleFieldSelection() return self.handleFieldSelection()
elif form_step == 'select_parts':
return self.handlePartSelection()
return self.render_to_response(self.get_context_data(form=self.form)) return self.render_to_response(self.get_context_data(form=self.form))