0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 09:33:54 +01:00

Merge branch 'issue-1334' of https://github.com/kaedroho/wagtail into kaedroho-issue-1334

This commit is contained in:
Matt Westcott 2015-06-15 18:50:30 +01:00
commit ad4426b84d
5 changed files with 59 additions and 67 deletions

View File

@ -12,60 +12,42 @@ from django.conf import settings
ALLOWED_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png']
SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG")
INVALID_IMAGE_ERROR = _(
"Not a supported image format. Supported formats: %s."
) % SUPPORTED_FORMATS_TEXT
INVALID_IMAGE_KNOWN_FORMAT_ERROR = _(
"Not a valid %s image."
)
MAX_UPLOAD_SIZE = getattr(settings, 'WAGTAILIMAGES_MAX_UPLOAD_SIZE', 10 * 1024 * 1024)
if MAX_UPLOAD_SIZE is not None:
MAX_UPLOAD_SIZE_TEXT = filesizeformat(MAX_UPLOAD_SIZE)
FILE_TOO_LARGE_ERROR = _(
"This file is too big. Maximum filesize %(max_upload_size)s."
) % {
'max_upload_size': MAX_UPLOAD_SIZE_TEXT,
}
FILE_TOO_LARGE_KNOWN_SIZE_ERROR = _(
"This file is too big (%%(max_upload_size)s). Maximum filesize %s."
) % {
'max_upload_size': MAX_UPLOAD_SIZE_TEXT,
}
IMAGE_FIELD_HELP_TEXT = _(
"Supported formats: %(supported_formats)s. Maximum filesize: %(max_upload_size)s."
) % {
'supported_formats': SUPPORTED_FORMATS_TEXT,
'max_upload_size': MAX_UPLOAD_SIZE_TEXT,
}
else:
MAX_UPLOAD_SIZE_TEXT = ""
FILE_TOO_LARGE_ERROR = ""
FILE_TOO_LARGE_KNOWN_SIZE_ERROR = ""
IMAGE_FIELD_HELP_TEXT = _(
"Supported formats: %(supported_formats)s."
) % {
'supported_formats': SUPPORTED_FORMATS_TEXT,
}
class WagtailImageField(ImageField):
default_error_messages = {
'invalid_image': INVALID_IMAGE_ERROR,
'invalid_image_known_format': INVALID_IMAGE_KNOWN_FORMAT_ERROR,
'file_too_large': FILE_TOO_LARGE_KNOWN_SIZE_ERROR,
}
def __init__(self, *args, **kwargs):
super(WagtailImageField, self).__init__(*args, **kwargs)
self.help_text = IMAGE_FIELD_HELP_TEXT
# Get max upload size from settings
self.max_upload_size = getattr(settings, 'WAGTAILIMAGES_MAX_UPLOAD_SIZE', 10 * 1024 * 1024)
max_upload_size_text = filesizeformat(self.max_upload_size)
# Help text
if self.max_upload_size is not None:
self.help_text = _(
"Supported formats: %(supported_formats)s. Maximum filesize: %(max_upload_size)s."
) % {
'supported_formats': SUPPORTED_FORMATS_TEXT,
'max_upload_size': max_upload_size_text,
}
else:
self.help_text = _(
"Supported formats: %(supported_formats)s."
) % {
'supported_formats': SUPPORTED_FORMATS_TEXT,
}
# Error messages
self.error_messages['invalid_image'] = _(
"Not a supported image format. Supported formats: %s."
) % SUPPORTED_FORMATS_TEXT
self.error_messages['invalid_image_known_format'] = _(
"Not a valid %s image."
)
self.error_messages['file_too_large'] = _(
"This file is too big (%%s). Maximum filesize %s."
) % max_upload_size_text
def check_image_file_format(self, f):
# Check file extension
@ -111,11 +93,11 @@ class WagtailImageField(ImageField):
def check_image_file_size(self, f):
# Upload size checking can be disabled by setting max upload size to None
if MAX_UPLOAD_SIZE is None:
if self.max_upload_size is None:
return
# Check the filesize
if f.size > MAX_UPLOAD_SIZE:
if f.size > self.max_upload_size:
raise ValidationError(self.error_messages['file_too_large'] % (
filesizeformat(f.size),
), code='file_too_large')

View File

@ -1,6 +1,8 @@
from __future__ import unicode_literals
import json
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils.http import urlquote
from django.core.urlresolvers import reverse
from django.contrib.auth.models import Permission
@ -95,6 +97,21 @@ class TestImageAddView(TestCase, WagtailTestUtils):
# The form should have an error
self.assertFormError(response, 'form', 'file', "This field is required.")
@override_settings(WAGTAILIMAGES_MAX_UPLOAD_SIZE=1)
def test_add_too_large_file(self):
response = self.post({
'title': "Test image",
'file': SimpleUploadedFile('test.png', get_test_image_file().file.getvalue()),
})
# Shouldn't redirect anywhere
self.assertEqual(response.status_code, 200)
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.")
class TestImageEditView(TestCase, WagtailTestUtils):
def setUp(self):

View File

@ -12,7 +12,6 @@ from wagtail.wagtailsearch.backends import get_search_backends
from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.forms import get_image_form, ImageInsertionForm
from wagtail.wagtailimages.formats import get_image_format
from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE
def get_image_json(image):
@ -147,7 +146,7 @@ def chooser_upload(request):
return render_modal_workflow(
request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js',
{'images': images, 'uploadform': form, 'searchform': searchform, 'max_filesize': MAX_UPLOAD_SIZE}
{'images': images, 'uploadform': form, 'searchform': searchform}
)

View File

@ -17,7 +17,6 @@ from wagtail.wagtailsearch.backends import get_search_backends
from wagtail.wagtailimages.models import get_image_model, Filter
from wagtail.wagtailimages.forms import get_image_form, URLGeneratorForm
from wagtail.wagtailimages.utils import generate_signature
from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE
from wagtail.wagtailimages.exceptions import InvalidFilterSpecError
@ -252,7 +251,6 @@ def add(request):
return render(request, "wagtailimages/images/add.html", {
'form': form,
'max_filesize': MAX_UPLOAD_SIZE,
})

View File

@ -12,13 +12,7 @@ from wagtail.wagtailsearch.backends import get_search_backends
from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.forms import get_image_form
from wagtail.wagtailimages.fields import (
MAX_UPLOAD_SIZE,
IMAGE_FIELD_HELP_TEXT,
INVALID_IMAGE_ERROR,
ALLOWED_EXTENSIONS,
FILE_TOO_LARGE_ERROR,
)
from wagtail.wagtailimages.fields import ALLOWED_EXTENSIONS
from wagtail.utils.compat import render_to_string
@ -87,13 +81,15 @@ def add(request):
# https://github.com/django/django/blob/stable/1.6.x/django/forms/util.py#L45
'error_message': '\n'.join(['\n'.join([force_text(i) for i in v]) for k, v in form.errors.items()]),
})
else:
form = ImageForm()
return render(request, 'wagtailimages/multiple/add.html', {
'max_filesize': MAX_UPLOAD_SIZE,
'help_text': IMAGE_FIELD_HELP_TEXT,
'max_filesize': form.fields['file'].max_upload_size,
'help_text': form.fields['file'].help_text,
'allowed_extensions': ALLOWED_EXTENSIONS,
'error_max_file_size': FILE_TOO_LARGE_ERROR,
'error_accepted_file_types': INVALID_IMAGE_ERROR,
'error_max_file_size': form.fields['file'].error_messages['file_too_large'],
'error_accepted_file_types': form.fields['file'].error_messages['invalid_image'],
})