Refs #41 - django_downloadview.sendfile is a PathDownloadView (was StorageDownloadView). PathDownloadView.get_file() raises FileNotFound if file does not exists.

This commit is contained in:
Benoît Bryon 2013-11-28 23:29:50 +01:00
parent 194699045c
commit 59d9b4966e
3 changed files with 41 additions and 9 deletions

View file

@ -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)

View file

@ -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):

View file

@ -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'))