Add basic support for "download" and "rawbytes" API views

This commit is contained in:
Lance Edgar 2021-01-06 13:12:27 -06:00
parent fd1342c605
commit 4d8e29c892
2 changed files with 74 additions and 9 deletions

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2020 Lance Edgar
# Copyright © 2010-2021 Lance Edgar
#
# This file is part of Rattail.
#
@ -166,7 +166,7 @@ class View(object):
return render_to_response('json', data,
request=self.request)
def file_response(self, path, filename=None):
def file_response(self, path, filename=None, attachment=True):
"""
Returns a generic FileResponse from the given path
"""
@ -174,11 +174,12 @@ class View(object):
return self.notfound()
response = FileResponse(path, request=self.request)
response.content_length = os.path.getsize(path)
if not filename:
filename = os.path.basename(path)
if six.PY2:
filename = filename.encode('ascii', 'replace')
response.content_disposition = str('attachment; filename="{}"'.format(filename))
if attachment:
if not filename:
filename = os.path.basename(path)
if six.PY2:
filename = filename.encode('ascii', 'replace')
response.content_disposition = str('attachment; filename="{}"'.format(filename))
return response
def get_quickie_context(self):