From fd8ed44833fbc873b09e2dd056f3924b1318d688 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 28 Jun 2019 20:16:17 +1000 Subject: [PATCH] Detect duplicate columns, and missing columns --- InvenTree/part/bom.py | 9 +++- .../part/bom_upload/select_fields.html | 14 ++++++ InvenTree/part/views.py | 46 +++++++++++++++---- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/InvenTree/part/bom.py b/InvenTree/part/bom.py index fe973efaf0..b0ba1a5dae 100644 --- a/InvenTree/part/bom.py +++ b/InvenTree/part/bom.py @@ -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 """ diff --git a/InvenTree/part/templates/part/bom_upload/select_fields.html b/InvenTree/part/templates/part/bom_upload/select_fields.html index d06846b33d..6bb6223dba 100644 --- a/InvenTree/part/templates/part/bom_upload/select_fields.html +++ b/InvenTree/part/templates/part/bom_upload/select_fields.html @@ -5,6 +5,17 @@

Step 2 of 3 - Select BOM Fields

+{% if missing and missing|length > 0 %} + +{% endif %} +
{% csrf_token %} {% load crispy_forms_tags %} @@ -25,6 +36,9 @@ {% endfor %} + {% if col.duplicate %} +

Duplicate column selection

+ {% endif %} {{ col.name }} diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index abd787bc5c..1c90a54752 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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)