Detect duplicate columns, and missing columns

This commit is contained in:
Oliver Walters 2019-06-28 20:16:17 +10:00
parent 857a214e7d
commit fd8ed44833
3 changed files with 59 additions and 10 deletions

View File

@ -46,14 +46,19 @@ class BomUploadManager:
""" Class for managing an uploaded BOM file """
# Fields which are absolutely necessary for valid upload
HEADERS = [
REQUIRED_HEADERS = [
'Part',
'Quantity',
'Quantity'
]
OPTIONAL_HEADERS = [
'Reference',
'Overage',
'Notes'
]
HEADERS = REQUIRED_HEADERS + OPTIONAL_HEADERS
def __init__(self, bom_file):
""" Initialize the BomUpload class with a user-uploaded file object """

View File

@ -5,6 +5,17 @@
<h4>Step 2 of 3 - Select BOM Fields</h4>
{% if missing and missing|length > 0 %}
<div class='alert alert-danger alert-block' role='alert'>
Missing selections for the following columns:
<ul>
{% for col in missing %}
<li>{{ col }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<form method="post" action='' class='js-modal-form' enctype="multipart/form-data">
{% csrf_token %}
{% load crispy_forms_tags %}
@ -25,6 +36,9 @@
<option value='{{ req }}'{% if req == col.guess %}selected='selected'{% endif %}>{{ req }}</option>
{% endfor %}
</select>
{% if col.duplicate %}
<p class='help-inline'>Duplicate column selection</p>
{% endif %}
<input type='hidden' name='col_name_{{ forloop.counter0 }}' value='{{ col.name }}'/>
{{ col.name }}
</th>

View File

@ -752,13 +752,11 @@ class BomUpload(AjaxView, FormMixin):
for item in self.request.POST:
print(item)
value = self.request.POST[item]
# Extract the column names
if item.startswith('col_name_'):
col_id = item.replace('col_name_', '')
col_id = int(item.replace('col_name_', ''))
col_name = value
column_names[col_id] = col_name
@ -766,7 +764,7 @@ class BomUpload(AjaxView, FormMixin):
# Extract the column selections
if item.startswith('col_select_'):
col_id = item.replace('col_select_', '')
col_id = int(item.replace('col_select_', ''))
col_name = value
column_selections[col_id] = value
@ -779,17 +777,49 @@ class BomUpload(AjaxView, FormMixin):
if len(s) < 4:
continue
row_id = s[1]
col_id = s[3]
row_id = int(s[1])
col_id = int(s[3])
if not row_id in row_data:
row_data[row_id] = {}
row_data[row_id][col_id] = value
col_ids = sorted(column_names.keys())
headers = []
for col in col_ids:
if col not in column_selections:
continue
header = ({
'name': column_names[col],
'guess': column_selections[col]
})
# Duplicate guess?
guess = column_selections[col]
if guess:
n = list(column_selections.values()).count(column_selections[col])
if n > 1:
header['duplicate'] = True
headers.append(header)
# Are there any missing columns?
missing = []
for col in BomUploadManager.REQUIRED_HEADERS:
if not col in column_selections.values():
missing.append(col)
ctx = {
# The headers that we know about
'known_headers': BomUploadManager.HEADERS,
'req_cols': BomUploadManager.HEADERS,
'bom_cols': headers,
'missing': missing,
}
return self.renderJsonResponse(self.request, form=self.get_form(), data=data, context=ctx)