Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sendfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _get_sendfile():



def sendfile(request, filename, attachment=False, attachment_filename=None, mimetype=None, encoding=None):
def sendfile(request, filename, attachment=False, attachment_filename=None, mimetype=None, encoding=None, backend=None):
'''
create a response to send file using backend configured in SENDFILE_BACKEND

Expand All @@ -48,7 +48,7 @@ def sendfile(request, filename, attachment=False, attachment_filename=None, mime
If no mimetype or encoding are specified, then they will be guessed via the
filename (using the standard python mimetypes module)
'''
_sendfile = _get_sendfile()
_sendfile = backend or _get_sendfile()

if not os.path.exists(filename):
from django.http import Http404
Expand Down
14 changes: 14 additions & 0 deletions sendfile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ def test_attachment_filename_unicode(self):
self.assertTrue(response is not None)
self.assertEqual('attachment; filename="test\'s.txt"; filename*=UTF-8\'\'test%E2%80%99s.txt', response['Content-Disposition'])

def test_override_backend(self):
def overriden_sendfile(*args, **kwargs):
# Add an extra header to the respone
response = sendfile(*args, **kwargs)
response['X-Backend-Overriden'] = 'yes'
return response

response = real_sendfile(HttpRequest(), self._get_readme(), backend=overriden_sendfile)
self.assertTrue(response is not None)
self.assertEqual('text/plain', response['Content-Type'])
self.assertEqual(self._get_readme(), smart_str(response.content))

self.assertEqual('yes', response['X-Backend-Overriden'])


class TestXSendfileBackend(TempFileTestCase):

Expand Down