Skip to content

Commit

Permalink
Fixed #24727 -- Prevented ClearableFileInput from masking exceptions …
Browse files Browse the repository at this point in the history
…on Python 2
  • Loading branch information
berkerpeksag committed Feb 15, 2016
1 parent 7424ad0 commit 043383e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/forms/widgets.py
Expand Up @@ -372,7 +372,7 @@ def is_initial(self, value):
"""
Return whether value is considered to be initial value.
"""
return bool(value and hasattr(value, 'url'))
return bool(value and getattr(value, 'url', False))

def get_template_substitution_values(self, value):
"""
Expand Down
39 changes: 39 additions & 0 deletions tests/forms_tests/widget_tests/test_clearablefileinput.py
Expand Up @@ -105,3 +105,42 @@ def test_clear_input_checked_returns_false_only_if_not_required(self):
name='myfile',
)
self.assertEqual(value, field)

def test_html_does_not_mask_exceptions(self):
"""
A ClearableFileInput should not mask exceptions produced while
checking that it has a value.
"""
@python_2_unicode_compatible
class FailingURLFieldFile(object):
@property
def url(self):
raise ValueError('Canary')

def __str__(self):
return 'value'

with self.assertRaisesMessage(ValueError, 'Canary'):
self.widget.render('myfile', FailingURLFieldFile())

def test_url_as_property(self):
@python_2_unicode_compatible
class URLFieldFile(object):
@property
def url(self):
return 'https://www.python.org/'

def __str__(self):
return 'value'

html = self.widget.render('myfile', URLFieldFile())
self.assertInHTML('<a href="https://www.python.org/">value</a>', html)

def test_return_false_if_url_does_not_exists(self):
@python_2_unicode_compatible
class NoURLFieldFile(object):
def __str__(self):
return 'value'

html = self.widget.render('myfile', NoURLFieldFile())
self.assertHTMLEqual(html, '<input name="myfile" type="file" />')

0 comments on commit 043383e

Please sign in to comment.