Pass args and kwargs around so that the get_file() method can response

to URL parameters.
This commit is contained in:
Thomas Willson 2016-05-25 15:08:53 -07:00
parent 6166783914
commit c66a1d7e75
2 changed files with 10 additions and 4 deletions

View file

@ -60,7 +60,7 @@ class DownloadMixin(object):
#: :mod:`mimetypes`.
encoding = None
def get_file(self):
def get_file(self, *args, **kwargs):
"""Return a file wrapper instance.
Raises :class:`~django_downloadview.exceptions.FileNotFound` if file
@ -151,7 +151,7 @@ class DownloadMixin(object):
"""
try:
self.file_instance = self.get_file()
self.file_instance = self.get_file(*response_args, **response_kwargs)
except exceptions.FileNotFound:
return self.file_not_found_response()
# Respect the If-Modified-Since header.
@ -165,6 +165,7 @@ class DownloadMixin(object):
class BaseDownloadView(DownloadMixin, View):
"""A base :class:`DownloadMixin` that implements :meth:`get`."""
def get(self, request, *args, **kwargs):
"""Handle GET requests: stream a file."""
return self.render_to_response()
return self.render_to_response(*args, **kwargs)

View file

@ -21,6 +21,7 @@ from django_downloadview import views
class DownloadMixinTestCase(unittest.TestCase):
"""Test suite around :class:`django_downloadview.views.DownloadMixin`."""
def test_get_file(self):
"""DownloadMixin.get_file() raise NotImplementedError.
@ -218,6 +219,7 @@ class DownloadMixinTestCase(unittest.TestCase):
class BaseDownloadViewTestCase(unittest.TestCase):
"Tests around :class:`django_downloadviews.views.base.BaseDownloadView`."
def test_get(self):
"""BaseDownloadView.get() calls render_to_response()."""
request = django.test.RequestFactory().get('/dummy-url')
@ -228,11 +230,12 @@ class BaseDownloadViewTestCase(unittest.TestCase):
return_value=mock.sentinel.response)
response = view.get(request, *args, **kwargs)
self.assertIs(response, mock.sentinel.response)
view.render_to_response.assert_called_once_with()
view.render_to_response.assert_called_once_with(*args, **kwargs)
class PathDownloadViewTestCase(unittest.TestCase):
"Tests for :class:`django_downloadviews.views.path.PathDownloadView`."
def test_get_file_ok(self):
"PathDownloadView.get_file() returns ``File`` instance."
view = setup_view(views.PathDownloadView(path=__file__),
@ -262,6 +265,7 @@ class PathDownloadViewTestCase(unittest.TestCase):
class ObjectDownloadViewTestCase(unittest.TestCase):
"Tests for :class:`django_downloadviews.views.object.ObjectDownloadView`."
def test_get_file_ok(self):
"ObjectDownloadView.get_file() returns ``file`` field by default."
view = setup_view(views.ObjectDownloadView(), 'fake request')
@ -296,6 +300,7 @@ class ObjectDownloadViewTestCase(unittest.TestCase):
class VirtualDownloadViewTestCase(unittest.TestCase):
"""Test suite around
:py:class:`django_downloadview.views.VirtualDownloadView`."""
def test_was_modified_since_specific(self):
"""VirtualDownloadView.was_modified_since() delegates to file wrapper.