diff --git a/testing/tests.py b/testing/tests.py index 079e4fb..05a43a9 100644 --- a/testing/tests.py +++ b/testing/tests.py @@ -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): @@ -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): @@ -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() diff --git a/validatedfile/fields.py b/validatedfile/fields.py index 0985c18..270f3e3 100644 --- a/validatedfile/fields.py +++ b/validatedfile/fields.py @@ -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', '') @@ -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