mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Provide download link to export BOM file
- Helper function for generating temporary file (in memory) for exporting
This commit is contained in:
parent
b58c49e066
commit
b8e28c003d
32
InvenTree/InvenTree/helpers.py
Normal file
32
InvenTree/InvenTree/helpers.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import io
|
||||||
|
import os
|
||||||
|
from wsgiref.util import FileWrapper
|
||||||
|
from django.http import StreamingHttpResponse, HttpResponse
|
||||||
|
|
||||||
|
|
||||||
|
def WrapWithQuotes(text):
|
||||||
|
# TODO - Make this better
|
||||||
|
if not text.startswith('"'):
|
||||||
|
text = '"' + text
|
||||||
|
|
||||||
|
if not text.endswith('"'):
|
||||||
|
text = text + '"'
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def DownloadFile(data, filename, content_type='application/text'):
|
||||||
|
"""
|
||||||
|
Create a dynamic file for the user to download.
|
||||||
|
@param data is the raw file data
|
||||||
|
"""
|
||||||
|
|
||||||
|
filename = WrapWithQuotes(filename)
|
||||||
|
|
||||||
|
wrapper = FileWrapper(io.StringIO(data))
|
||||||
|
|
||||||
|
response = StreamingHttpResponse(wrapper, content_type=content_type)
|
||||||
|
response['Content-Length'] = len(data)
|
||||||
|
response['Content-Disposition'] = 'attachment; filename={f}'.format(f=filename)
|
||||||
|
|
||||||
|
return response
|
@ -62,16 +62,14 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
$("#export-bom").click(function () {
|
$("#export-bom").click(function () {
|
||||||
|
/*
|
||||||
launchModalForm(
|
launchModalForm(
|
||||||
"{% url 'bom-export' part.id %}",
|
"{% url 'bom-export' part.id %}",
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
reload: true,
|
|
||||||
data: {
|
|
||||||
format: 'csv',
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
//TODO - Select format of the data
|
||||||
|
location.href = "{% url 'bom-export' part.id %}";
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#bom-table").bootstrapTable({
|
$("#bom-table").bootstrapTable({
|
||||||
|
@ -21,6 +21,7 @@ from .forms import EditSupplierPartForm
|
|||||||
|
|
||||||
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
||||||
|
|
||||||
|
from InvenTree.helpers import DownloadFile
|
||||||
|
|
||||||
class PartIndex(ListView):
|
class PartIndex(ListView):
|
||||||
model = Part
|
model = Part
|
||||||
@ -111,34 +112,36 @@ class PartEdit(AjaxUpdateView):
|
|||||||
ajax_form_title = 'Edit Part Properties'
|
ajax_form_title = 'Edit Part Properties'
|
||||||
|
|
||||||
|
|
||||||
class BomExport(AjaxUpdateView):
|
class BomExport(AjaxView):
|
||||||
|
# TODO - This should no longer extend an AjaxView!
|
||||||
|
|
||||||
model = Part
|
model = Part
|
||||||
form_class = BomExportForm
|
#form_class = BomExportForm
|
||||||
template_name = 'part/bom_export.html'
|
#template_name = 'part/bom_export.html'
|
||||||
ajax_form_title = 'Export Bill of Materials'
|
#ajax_form_title = 'Export Bill of Materials'
|
||||||
ajax_submit_text = 'Export'
|
#ajax_submit_text = 'Export'
|
||||||
context_object_name = 'part'
|
#context_object_name = 'part'
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
|
||||||
part = get_object_or_404(Part, pk=self.kwargs['pk'])
|
part = get_object_or_404(Part, pk=self.kwargs['pk'])
|
||||||
form = self.form_class(instance=part, data=request.POST, files=request.FILES)
|
|
||||||
|
|
||||||
return self.renderJsonResponse(request, form=form, context={'part': part})
|
export_format = request.GET.get('format', 'csv')
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
# Placeholder to test file export
|
||||||
|
filename = '"' + part.name + '_BOM.' + export_format + '"'
|
||||||
|
|
||||||
part = get_object_or_404(Part, pk=self.kwargs['pk'])
|
filedata = "Part,Quantity,Available\n"
|
||||||
form = self.form_class(instance=part, data=request.POST, files=request.FILES)
|
|
||||||
|
|
||||||
export_format = request.POST.get('format', None)
|
for bom_item in part.bom_items.all():
|
||||||
|
filedata += bom_item.sub_part.name
|
||||||
|
filedata += ","
|
||||||
|
filedata += str(bom_item.quantity)
|
||||||
|
filedata += ","
|
||||||
|
filedata += str(bom_item.sub_part.available_stock)
|
||||||
|
filedata += "\n"
|
||||||
|
|
||||||
if not export_format:
|
return DownloadFile(filedata, filename)
|
||||||
# TODO
|
|
||||||
pass
|
|
||||||
|
|
||||||
return self.renderJsonResponse(request, form=form, context={'part': part})
|
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user