diff --git a/wagtail/wagtailimages/fields.py b/wagtail/wagtailimages/fields.py index 93c0ac950f..2663d6667b 100644 --- a/wagtail/wagtailimages/fields.py +++ b/wagtail/wagtailimages/fields.py @@ -49,6 +49,10 @@ class WagtailImageField(ImageField): "This file is too big (%%s). Maximum filesize %s." ) % max_upload_size_text + self.error_messages['file_too_large_unknown_size'] = _( + "This file is too big. Maximum filesize %s." + ) % max_upload_size_text + def check_image_file_format(self, f): # Check file extension extension = os.path.splitext(f.name)[1].lower()[1:] diff --git a/wagtail/wagtailimages/tests/test_admin_views.py b/wagtail/wagtailimages/tests/test_admin_views.py index 10a90941ec..8a4b954bb8 100644 --- a/wagtail/wagtailimages/tests/test_admin_views.py +++ b/wagtail/wagtailimages/tests/test_admin_views.py @@ -7,6 +7,7 @@ from django.utils.http import urlquote from django.core.urlresolvers import reverse from django.contrib.auth.models import Permission from django.core.files.uploadedfile import SimpleUploadedFile +from django.template.defaultfilters import filesizeformat # Get the chars that Django considers safe to leave unescaped in a URL # This list changed in Django 1.8: https://github.com/django/django/commit/e167e96cfea670422ca75d0b35fe7c4195f25b63 @@ -99,9 +100,11 @@ class TestImageAddView(TestCase, WagtailTestUtils): @override_settings(WAGTAILIMAGES_MAX_UPLOAD_SIZE=1) def test_add_too_large_file(self): + file_content = get_test_image_file().file.getvalue() + response = self.post({ 'title': "Test image", - 'file': SimpleUploadedFile('test.png', get_test_image_file().file.getvalue()), + 'file': SimpleUploadedFile('test.png', file_content), }) # Shouldn't redirect anywhere @@ -109,8 +112,10 @@ class TestImageAddView(TestCase, WagtailTestUtils): self.assertTemplateUsed(response, 'wagtailimages/images/add.html') # The form should have an error - # Note: \xa0 = non-blocking space - self.assertFormError(response, 'form', 'file', "This file is too big (1.9\xa0KB). Maximum filesize 1\xa0byte.") + self.assertFormError(response, 'form', 'file', "This file is too big ({file_size}). Maximum filesize {max_file_size}.".format( + file_size=filesizeformat(len(file_content)), + max_file_size=filesizeformat(1), + )) class TestImageEditView(TestCase, WagtailTestUtils): @@ -302,6 +307,13 @@ class TestMultipleImageUploader(TestCase, WagtailTestUtils): self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailimages/multiple/add.html') + @override_settings(WAGTAILIMAGES_MAX_UPLOAD_SIZE=1000) + def test_add_max_file_size_context_variables(self): + response = self.client.get(reverse('wagtailimages_add_multiple')) + + self.assertEqual(response.context['max_filesize'], 1000) + self.assertEqual(response.context['error_max_file_size'], "This file is too big. Maximum filesize 1000\xa0bytes.") + def test_add_post(self): """ This tests that a POST request to the add view saves the image and returns an edit form diff --git a/wagtail/wagtailimages/views/multiple.py b/wagtail/wagtailimages/views/multiple.py index 29eb9ae5d5..13142aa44a 100644 --- a/wagtail/wagtailimages/views/multiple.py +++ b/wagtail/wagtailimages/views/multiple.py @@ -88,7 +88,7 @@ def add(request): 'max_filesize': form.fields['file'].max_upload_size, 'help_text': form.fields['file'].help_text, 'allowed_extensions': ALLOWED_EXTENSIONS, - 'error_max_file_size': form.fields['file'].error_messages['file_too_large'], + 'error_max_file_size': form.fields['file'].error_messages['file_too_large_unknown_size'], 'error_accepted_file_types': form.fields['file'].error_messages['invalid_image'], })