mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-06 07:18:48 +00:00
Add ability to download file as StringIO or BytesIO
This commit is contained in:
parent
fd9f25dc0d
commit
55000d5c48
@ -170,7 +170,7 @@ class InvenTreeAPITestCase(APITestCase):
|
|||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def download_file(self, url, data, expected_code=None, expected_fn=None):
|
def download_file(self, url, data, expected_code=None, expected_fn=None, decode=False):
|
||||||
"""
|
"""
|
||||||
Download a file from the server, and return an in-memory file
|
Download a file from the server, and return an in-memory file
|
||||||
"""
|
"""
|
||||||
@ -191,11 +191,20 @@ class InvenTreeAPITestCase(APITestCase):
|
|||||||
|
|
||||||
fn = result.groups()[0]
|
fn = result.groups()[0]
|
||||||
|
|
||||||
with io.BytesIO() as fo:
|
|
||||||
fo.name = fn
|
|
||||||
fo.write(response.getvalue())
|
|
||||||
|
|
||||||
if expected_fn is not None:
|
if expected_fn is not None:
|
||||||
self.assertEqual(expected_fn, fn)
|
self.assertEqual(expected_fn, fn)
|
||||||
|
|
||||||
return fo
|
if decode:
|
||||||
|
# Decode data and return as StringIO file object
|
||||||
|
fo = io.StringIO()
|
||||||
|
fo.name = fo
|
||||||
|
fo.write(response.getvalue().decode('UTF-8'))
|
||||||
|
else:
|
||||||
|
# Return a a BytesIO file object
|
||||||
|
fo = io.BytesIO()
|
||||||
|
fo.name = fn
|
||||||
|
fo.write(response.getvalue())
|
||||||
|
|
||||||
|
fo.seek(0)
|
||||||
|
|
||||||
|
return fo
|
||||||
|
Loading…
x
Reference in New Issue
Block a user