From c66a1d7e753de56c849ff6cfea1dad54d8a41836 Mon Sep 17 00:00:00 2001 From: Thomas Willson Date: Wed, 25 May 2016 15:08:53 -0700 Subject: [PATCH] Pass args and kwargs around so that the get_file() method can response to URL parameters. --- django_downloadview/views/base.py | 7 ++++--- tests/views.py | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/django_downloadview/views/base.py b/django_downloadview/views/base.py index 29121c3..fe35fb1 100644 --- a/django_downloadview/views/base.py +++ b/django_downloadview/views/base.py @@ -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) diff --git a/tests/views.py b/tests/views.py index 6ad8e1d..db7cf04 100644 --- a/tests/views.py +++ b/tests/views.py @@ -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.