mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Link to download a BOM template file
This commit is contained in:
		@@ -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.
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								InvenTree/part/templates/part/bom_upload.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								InvenTree/part/templates/part/bom_upload.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
{% extends "modal_form.html" %}
 | 
			
		||||
 | 
			
		||||
{% block pre_form_content %}
 | 
			
		||||
 | 
			
		||||
{{ block.super }}
 | 
			
		||||
 | 
			
		||||
<p>Select a BOM file to upload for <b>{{ part.name }} - <i>{{ part.description }}</i></b>.</p>
 | 
			
		||||
 | 
			
		||||
<p>The BOM file must contain the required named columns as provided in the <a href="/part/bom_template/">BOM Upload Template</a></a></p>
 | 
			
		||||
 | 
			
		||||
<p>Supported file formats: <i>.csv, .tsv, .xls, .xlsx</i></p>
 | 
			
		||||
 | 
			
		||||
{% endblock %}
 | 
			
		||||
@@ -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<pk>\d+)/', include(part_detail_urls)),
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ from django.shortcuts import get_object_or_404
 | 
			
		||||
from django.utils.translation import gettext_lazy as _
 | 
			
		||||
from django.urls import reverse_lazy
 | 
			
		||||
from django.views.generic import DetailView, ListView
 | 
			
		||||
from django.views.generic.edit import FormMixin
 | 
			
		||||
from django.forms.models import model_to_dict
 | 
			
		||||
from django.forms import HiddenInput, CheckboxInput
 | 
			
		||||
 | 
			
		||||
@@ -26,7 +27,7 @@ from . import forms as part_forms
 | 
			
		||||
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
 | 
			
		||||
from InvenTree.views import QRCodeView
 | 
			
		||||
 | 
			
		||||
from InvenTree.helpers import DownloadFile, str2bool
 | 
			
		||||
from InvenTree.helpers import DownloadFile, str2bool, MakeBomTemplate
 | 
			
		||||
from InvenTree.status_codes import OrderStatus
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -722,6 +723,17 @@ class BomUpload(AjaxView):
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return self.renderJsonResponse(request, form, data=data)
 | 
			
		||||
class BomUploadTemplate(AjaxView):
 | 
			
		||||
    """
 | 
			
		||||
    Provide a BOM upload template file for download.
 | 
			
		||||
    - Generates a template file in the provided format e.g. ?format=csv
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    def get(self, request, *args, **kwargs):
 | 
			
		||||
 | 
			
		||||
        export_format = request.GET.get('format', 'csv')
 | 
			
		||||
 | 
			
		||||
        return MakeBomTemplate(export_format)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BomDownload(AjaxView):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user