From a23595c28db179c764ba43428b2a1f930c276955 Mon Sep 17 00:00:00 2001 From: Oliver Walters <oliver.henry.walters@gmail.com> Date: Sat, 29 Jun 2019 19:56:04 +1000 Subject: [PATCH] Improve data importing - Automatically prune empty rows - prevent automatic conversion of integers to floats --- InvenTree/part/bom.py | 27 ++++++++++++++++--- .../part/bom_upload/select_fields.html | 1 + InvenTree/part/views.py | 9 +++---- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/InvenTree/part/bom.py b/InvenTree/part/bom.py index 3b6a7fe928..70cea0601b 100644 --- a/InvenTree/part/bom.py +++ b/InvenTree/part/bom.py @@ -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 diff --git a/InvenTree/part/templates/part/bom_upload/select_fields.html b/InvenTree/part/templates/part/bom_upload/select_fields.html index ee4a61fc0f..ad90530ad8 100644 --- a/InvenTree/part/templates/part/bom_upload/select_fields.html +++ b/InvenTree/part/templates/part/bom_upload/select_fields.html @@ -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 }}'> diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 1763ce918e..259549cc8d 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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]