Refs #43 - ObjectDownloadView.get_file() raises FileNotFound if field is empty. Raises AttributeError if field does not exist (view or model is misconfigured).

This commit is contained in:
Benoît Bryon 2013-11-24 10:09:46 +01:00
parent 0b2a26e180
commit 567cf591b9
2 changed files with 12 additions and 10 deletions

View file

@ -208,12 +208,17 @@ class ObjectDownloadViewTestCase(unittest.TestCase):
view.get_file()
def test_get_file_wrong_field(self):
"""ObjectDownloadView.get_file() raises FileNotFound if field does not
exist."""
"""ObjectDownloadView.get_file() raises AttributeError if field does
not exist.
``AttributeError`` is expected because this is a configuration error,
i.e. it is related to Python code.
"""
view = setup_view(views.ObjectDownloadView(file_field='other_field'),
'fake request')
view.object = mock.Mock(spec=['file'])
with self.assertRaises(exceptions.FileNotFound):
with self.assertRaises(AttributeError):
view.get_file()
def test_get_file_empty_field(self):

View file

@ -61,18 +61,15 @@ class ObjectDownloadView(SingleObjectMixin, BaseDownloadView):
is typically a :class:`~django.db.models.fields.files.FieldFile` or
subclass.
Raises :class:`~django_downloadview.exceptions.FileNotFound` if
instance's field is empty.
Additional attributes are set on the file wrapper if :attr:`encoding`,
:attr:`mime_type`, :attr:`charset`, :attr:`modification_time` or
:attr:`size` are configured.
"""
try:
file_instance = getattr(self.object, self.file_field)
except AttributeError:
raise FileNotFound('Failed to retrieve file from field="{field}" '
'on object="{object}"'.format(
field=self.file_field,
object=self.object))
file_instance = getattr(self.object, self.file_field)
if not file_instance:
raise FileNotFound('Field="{field}" on object="{object}" is '
'empty'.format(