2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-16 09:46:31 +00:00

Provide download link to export BOM file

- Helper function for generating temporary file (in memory) for exporting
This commit is contained in:
Oliver Walters
2019-04-14 00:35:19 +10:00
parent b58c49e066
commit b8e28c003d
3 changed files with 57 additions and 24 deletions

View 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