Improve data importing

- Automatically prune empty rows
- prevent automatic conversion of integers to floats
This commit is contained in:
Oliver Walters 2019-06-29 19:56:04 +10:00
parent b089a61f74
commit a23595c28d
3 changed files with 28 additions and 9 deletions

View File

@ -121,7 +121,6 @@ class BomUploadManager:
return matches[0]['header']
return None
def get_headers(self):
""" Return a list of headers for the thingy """
@ -157,13 +156,33 @@ class BomUploadManager:
for i in range(self.row_count()):
data = [item for item in self.get_row_data(i)]
# Is the row completely empty? Skip!
empty = True
for idx, item in enumerate(data):
if len(str(item).strip()) > 0:
empty = False
try:
# Excel import casts number-looking-items into floats, which is annoying
if item == int(item) and not str(item) == str(int(item)):
print("converting", item, "to", int(item))
data[idx] = int(item)
except ValueError:
pass
if empty:
print("Empty - continuing")
continue
row = {
'data': self.get_row_data(i),
'data': data,
'index': i
}
if row:
rows.append(row)
rows.append(row)
return rows

View File

@ -44,6 +44,7 @@
</thead>
<tbody>
<tr>
<td></td>
{% for col in bom_cols %}
<td>
<select class='select' id='id_col_{{ forloop.counter0 }}' name='col_select_{{ forloop.counter0 }}'>

View File

@ -520,7 +520,6 @@ class PartDetail(DetailView):
else:
context['editing_enabled'] = 0
context['starred'] = part.isStarredBy(self.request.user)
context['disabled'] = not part.active
@ -744,7 +743,7 @@ class BomUpload(AjaxView, FormMixin):
self.ajax_template_name = 'part/bom_upload/select_fields.html'
# Map the columns
# Map the columns
column_names = {}
column_selections = {}
@ -780,7 +779,7 @@ class BomUpload(AjaxView, FormMixin):
row_id = int(s[1])
col_id = int(s[3])
if not row_id in row_data:
if row_id not in row_data:
row_data[row_id] = {}
row_data[row_id][col_id] = value
@ -812,7 +811,7 @@ class BomUpload(AjaxView, FormMixin):
missing = []
for col in BomUploadManager.REQUIRED_HEADERS:
if not col in column_selections.values():
if col not in column_selections.values():
missing.append(col)
# Re-construct the data table
@ -823,7 +822,7 @@ class BomUpload(AjaxView, FormMixin):
items = []
for col_idx in sorted(row.keys()):
if not col_idx in column_selections.keys():
if col_idx not in column_selections.keys():
continue
value = row[col_idx]