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:
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
|
Reference in New Issue
Block a user