diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index ddb4e35fee..2c2f74fde6 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -6,6 +6,7 @@ import io import json import os.path from PIL import Image +import tablib from wsgiref.util import FileWrapper from django.http import StreamingHttpResponse @@ -91,6 +92,33 @@ def MakeBarcode(object_type, object_id, object_url, data={}): return json.dumps(data, sort_keys=True) +def IsValidSpreadsheetFormat(fmt): + """ Test if a file format specifier is in the valid list of spreadsheet file formats """ + + return fmt.lower() in ['csv', 'xls', 'xlsx', 'tsv'] + + +def MakeBomTemplate(fmt): + """ Generate a Bill of Materials upload template file (for user download) """ + + if not IsValidSpreadsheetFormat(fmt): + fmt = 'csv' + + fields = [ + 'Part', + 'Quantity', + 'Overage', + 'Reference', + 'Notes' + ] + + data = tablib.Dataset(headers=fields).export(fmt) + + filename = 'InvenTree_BOM_Template.' + fmt + + return DownloadFile(data, filename) + + def DownloadFile(data, filename, content_type='application/text'): """ Create a dynamic file for the user to download. diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 4512859828..8c90de9dd2 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -8,6 +8,7 @@ from __future__ import unicode_literals from InvenTree.forms import HelperForm from django import forms +from django.core.validators import MinValueValidator from .models import Part, PartCategory, PartAttachment from .models import BomItem diff --git a/InvenTree/part/templates/part/bom_upload.html b/InvenTree/part/templates/part/bom_upload.html new file mode 100644 index 0000000000..b62cbc9c49 --- /dev/null +++ b/InvenTree/part/templates/part/bom_upload.html @@ -0,0 +1,13 @@ +{% extends "modal_form.html" %} + +{% block pre_form_content %} + +{{ block.super }} + +
Select a BOM file to upload for {{ part.name }} - {{ part.description }}.
+ +The BOM file must contain the required named columns as provided in the BOM Upload Template
+ +Supported file formats: .csv, .tsv, .xls, .xlsx
+ +{% endblock %} \ No newline at end of file diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index a8f5184972..bfb542cb3f 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -74,6 +74,9 @@ part_urls = [ # Create a new BOM item url(r'^bom/new/?', views.BomItemCreate.as_view(), name='bom-item-create'), + # Download a BOM upload template + url(r'^bom_template/?', views.BomUploadTemplate.as_view(), name='bom-upload-template'), + # Individual part url(r'^(?P