mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Merge branch 'kaedroho-issue-1334'
This commit is contained in:
commit
09b4ca5966
@ -12,60 +12,46 @@ 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
|
||||
|
||||
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
|
||||
@ -111,11 +97,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')
|
||||
|
@ -1,10 +1,13 @@
|
||||
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
|
||||
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
|
||||
@ -95,6 +98,25 @@ 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):
|
||||
file_content = get_test_image_file().file.getvalue()
|
||||
|
||||
response = self.post({
|
||||
'title': "Test image",
|
||||
'file': SimpleUploadedFile('test.png', file_content),
|
||||
})
|
||||
|
||||
# Shouldn't redirect anywhere
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtailimages/images/add.html')
|
||||
|
||||
# The form should have an error
|
||||
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):
|
||||
def setUp(self):
|
||||
@ -285,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
|
||||
|
@ -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}
|
||||
)
|
||||
|
||||
|
||||
|
@ -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,
|
||||
})
|
||||
|
||||
|
||||
|
@ -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_unknown_size'],
|
||||
'error_accepted_file_types': form.fields['file'].error_messages['invalid_image'],
|
||||
})
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user