Pass part selection back-and-forwards between client/server

This commit is contained in:
Oliver Walters 2019-07-10 09:22:38 +10:00
parent 782d740323
commit 99dee64f79
2 changed files with 52 additions and 5 deletions

View File

@ -55,17 +55,20 @@
<span row_id='{{ row.index }}' class='glyphicon glyphicon-small glyphicon-plus' onClick='newPartFromBomWizard()'/>
</button>
<select class='select bomselect' id='select_part_{{ row.index }}' name='part_select_{{ row.index }}'>
<select class='select bomselect' id='select_part_{{ row.index }}' name='part_{{ row.index }}'>
<option value=''>---------</option>
{% for part in row.part_options %}
<option value='{{ part.id }}'>{{ part.full_name }} - <i>{{ part.description }}</i></option>
<option value='{{ part.id }}'{% if part.id == row.part.id %} selected='selected'{% endif %}>{{ part.full_name }} - {{ part.description }}</option>
{% endfor %}
</select>
<i>{{ item.cell }}</i>
{% if row.errors.part %}
<p class='help-inline'>{{ row.errors.part }}</p>
{% endif %}
{% elif item.column.guess == 'Quantity' %}
<input name='quantity_{{ row.index }}' class='numberinput' type='number' min='1' value='{{ row.quantity }}'/>
{% if row.errors.quantity %}
<p class='help-block'>{{ row.errors.quantity }}</p>
<p class='help-inline'>{{ row.errors.quantity }}</p>
{% endif %}
{% elif item.column.guess == 'Reference' %}
<input name='reference_{{ row.index }}' value='{{ row.reference }}'/>

View File

@ -702,7 +702,7 @@ class BomUpload(FormView):
rows.append({
'index': row.get('index', -1),
'data': data,
'part_options': row.get('part_options', []),
'part_options': row.get('part_options', self.allowed_parts),
# User-input (passed between client and server)
'quantity': row.get('quantity', None),
@ -1025,7 +1025,6 @@ class BomUpload(FormView):
row = self.getRowByIndex(row_id)
if row is None:
print("No match found for row", row_id)
continue
q = 1
@ -1042,6 +1041,51 @@ class BomUpload(FormView):
except ValueError:
continue
# Extract part from each row
if key.startswith('part_'):
try:
row_id = int(key.replace('part_', ''))
row = self.getRowByIndex(row_id)
if row is None:
continue
except ValueError:
# Row ID non integer value
continue
try:
part_id = int(value)
part = Part.objects.get(id=part_id)
except ValueError:
row['errors']['part'] = _('Select valid part')
continue
except Part.DoesNotExist:
row['errors']['part'] = _('Select valid part')
continue
# Keep track of how many of each part we have seen
if part_id in parts:
parts[part_id]['quantity'] += 1
row['errors']['part'] = _('Duplicate part selected')
else:
parts[part_id] = {
'part': part,
'quantity': 1,
}
row['part'] = part
# Are there any errors after form handling?
valid = True
for row in self.bom_rows:
errors = row.get('errors', [])
if len(errors) > 0:
valid = False
self.template_name = 'part/bom_upload/select_parts.html'
return self.render_to_response(self.get_context_data(form=None))