From 59d9b4966ed2ace709b1f4574baff815d91b215c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Bryon?= Date: Thu, 28 Nov 2013 23:29:50 +0100 Subject: [PATCH] Refs #41 - django_downloadview.sendfile is a PathDownloadView (was StorageDownloadView). PathDownloadView.get_file() raises FileNotFound if file does not exists. --- django_downloadview/sendfile.py | 13 ++++--------- django_downloadview/tests/views.py | 31 ++++++++++++++++++++++++++++++ django_downloadview/views/path.py | 6 ++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/django_downloadview/sendfile.py b/django_downloadview/sendfile.py index 2031306..dbf9128 100644 --- a/django_downloadview/sendfile.py +++ b/django_downloadview/sendfile.py @@ -1,9 +1,6 @@ # -*- coding: utf-8 -*- """Port of django-sendfile in django-downloadview.""" -from django.conf import settings -from django.core.files.storage import FileSystemStorage - -from django_downloadview.views.storage import StorageDownloadView +from django_downloadview.views.path import PathDownloadView def sendfile(request, filename, attachment=False, attachment_filename=None, @@ -15,9 +12,7 @@ def sendfile(request, filename, attachment=False, attachment_filename=None, :class:`StorageDownloadView` to stream the file by ``filename``. """ - storage = FileSystemStorage(location=settings.SENDFILE_ROOT) - view = StorageDownloadView().as_view(storage=storage, - path=filename, - attachment=attachment, - basename=attachment_filename) + view = PathDownloadView().as_view(path=filename, + attachment=attachment, + basename=attachment_filename) return view(request) diff --git a/django_downloadview/tests/views.py b/django_downloadview/tests/views.py index d2a1a83..642c496 100644 --- a/django_downloadview/tests/views.py +++ b/django_downloadview/tests/views.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- """Unit tests around views.""" +import os import unittest try: from unittest import mock except ImportError: import mock +from django.core.files import File from django.http import Http404 from django.http.response import HttpResponseNotModified import django.test @@ -199,6 +201,35 @@ class BaseDownloadViewTestCase(unittest.TestCase): view.render_to_response.assert_called_once_with() +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__), + 'fake request') + file_wrapper = view.get_file() + self.assertTrue(isinstance(file_wrapper, File)) + + def test_get_file_does_not_exist(self): + """PathDownloadView.get_file() raises FileNotFound if field does not + exist. + + """ + view = setup_view(views.PathDownloadView(path='i-do-no-exist'), + 'fake request') + with self.assertRaises(exceptions.FileNotFound): + view.get_file() + + def test_get_file_is_directory(self): + """PathDownloadView.get_file() raises FileNotFound if file is a + directory.""" + view = setup_view( + views.PathDownloadView(path=os.path.dirname(__file__)), + 'fake request') + with self.assertRaises(exceptions.FileNotFound): + view.get_file() + + class ObjectDownloadViewTestCase(unittest.TestCase): "Tests for :class:`django_downloadviews.views.object.ObjectDownloadView`." def test_get_file_ok(self): diff --git a/django_downloadview/views/path.py b/django_downloadview/views/path.py index 487fd8a..2a5d09d 100644 --- a/django_downloadview/views/path.py +++ b/django_downloadview/views/path.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- """:class:`PathDownloadView`.""" +import os + from django.core.files import File +from django_downloadview.exceptions import FileNotFound from django_downloadview.views.base import BaseDownloadView @@ -30,4 +33,7 @@ class PathDownloadView(BaseDownloadView): def get_file(self): """Use path to return wrapper around file to serve.""" + filename = self.get_path() + if not os.path.isfile(filename): + raise FileNotFound('File "{0}" does not exists'.format(filename)) return File(open(self.get_path(), 'rb'))