Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9: Validate file size before content type #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions testing/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_form_invalid_size(self):
self.assertFalse(form.is_valid())
self.assertEqual(len(form.errors), 1)
self.assertEqual(len(form.errors['the_file']), 1)
self.assertEqual(form.errors['the_file'][0], u'Files of size greater than 10.0 KB are not allowed. Your file is 14.2 KB')
self.assertEqual(form.errors['the_file'][0], u'Files of size greater than 10.0\xa0KB are not allowed. Your file is 14.2\xa0KB')


def test_form_invalid_filetype(self):
Expand All @@ -73,7 +73,7 @@ def test_form_invalid_filetype_and_size(self):
self.assertFalse(form.is_valid())
self.assertEqual(len(form.errors), 1)
self.assertEqual(len(form.errors['the_file']), 1)
self.assertEqual(form.errors['the_file'][0], u'Files of type application/pdf are not supported.')
self.assertEqual(form.errors['the_file'][0], u'Files of size greater than 10.0\xa0KB are not allowed. Your file is 14.9\xa0KB')


def test_form_fake_filetype(self):
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_form_quota_exceeded(self):
self.assertEqual(len(form.errors), 1)
self.assertEqual(len(form.errors['the_file']), 1)
self.assertEqual(form.errors['the_file'][0],
u'Please keep the total uploaded files under 9.8 KB. With this file, the total would be 16.3 KB.')
u'Please keep the total uploaded files under 9.8\xa0KB. With this file, the total would be 16.3\xa0KB.')

element.the_file.delete()
element.delete()
Expand Down
14 changes: 7 additions & 7 deletions validatedfile/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def clean(self, *args, **kwargs):
data = super(ValidatedFileField, self).clean(*args, **kwargs)
file = data.file

if self.max_upload_size and hasattr(file, '_size'):
if file._size > self.max_upload_size:
raise forms.ValidationError(
_('Files of size greater than %(max_size)s are not allowed. Your file is %(current_size)s') %
{'max_size': filesizeformat(self.max_upload_size), 'current_size': filesizeformat(file._size)}
)

if self.content_types:
uploaded_content_type = getattr(file, 'content_type', '')

Expand All @@ -35,13 +42,6 @@ def clean(self, *args, **kwargs):
_('Files of type %(type)s are not supported.') % {'type': content_type_magic}
)

if self.max_upload_size and hasattr(file, '_size'):
if file._size > self.max_upload_size:
raise forms.ValidationError(
_('Files of size greater than %(max_size)s are not allowed. Your file is %(current_size)s') %
{'max_size': filesizeformat(self.max_upload_size), 'current_size': filesizeformat(file._size)}
)

return data


Expand Down