WagtailImageField help text. Some refactoring

This commit is contained in:
Karl Hobley 2014-10-08 14:55:43 +01:00 committed by Karl Hobley
parent 140855c03c
commit 71c48e01ed
4 changed files with 49 additions and 26 deletions

View file

@ -9,29 +9,50 @@ from django.template.defaultfilters import filesizeformat
from django.conf import settings
def get_max_image_filesize():
return getattr(settings, 'WAGTAILIMAGES_MAX_UPLOAD_SIZE', 10 * 1024 * 1024)
ALLOWED_EXTENSIONS = ['git', 'jpg', 'jpeg', 'png']
INVALID_IMAGE_ERROR = _(
"Not a supported image type. Please use a gif, jpeg or png file "
"with the correct file extension (*.gif, *.jpg or *.png)."
)
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 (%%s). Image files must not exceed %s."
) % (MAX_UPLOAD_SIZE_TEXT, )
IMAGE_FIELD_HELP_TEXT = _(
"Supported formats: gif, jpeg, png. Max size: %s"
) % (MAX_UPLOAD_SIZE_TEXT, )
else:
MAX_UPLOAD_SIZE_TEXT = ""
FILE_TOO_LARGE_ERROR = ""
IMAGE_FIELD_HELP_TEXT = _(
"Supported formats: gif, jpeg, png."
)
class WagtailImageField(ImageField):
default_error_messages = {
'invalid_image': _(
"Not a supported image type. Please use a gif, jpeg or png file "
"with the correct file extension (*.gif, *.jpg or *.png)."
),
'file_too_large': _(
"This file is too big (%s). Image files must not exceed %s."
),
'invalid_image': INVALID_IMAGE_ERROR,
'file_too_large': FILE_TOO_LARGE_ERROR,
}
def __init__(self, *args, **kwargs):
super(WagtailImageField, self).__init__(*args, **kwargs)
self.help_text = IMAGE_FIELD_HELP_TEXT
def check_image_file_format(self, f):
# Check file extension
extension = os.path.splitext(f.name)[1].lower()[1:]
if extension == 'jpg':
extension = 'jpeg'
if extension not in ['gif', 'jpeg', 'png']:
if extension not in ALLOWED_EXTENSIONS:
raise ValidationError(self.error_messages['invalid_image'], code='invalid_image')
if hasattr(f, 'image'):
@ -53,24 +74,24 @@ class WagtailImageField(ImageField):
# Couldn't get the PIL image, skip checking the internal file format
return
image_format = extension
if extension == 'jpg':
image_format = 'jpeg'
# Check that the internal format matches the extension
# It is possible to upload PSD files if their extension is set to jpg, png or gif. This should catch them out
if image.format.upper() != extension.upper():
if image.format.upper() != image_format.upper():
raise ValidationError(self.error_messages['invalid_image'], code='invalid_image')
def check_image_file_size(self, f):
# Get max size
max_size = get_max_image_filesize()
# Upload size checking can be disabled by setting max upload size to None
if max_size is None:
if MAX_UPLOAD_SIZE is None:
return
# Check the filesize
if f.size > max_size:
if f.size > MAX_UPLOAD_SIZE:
raise ValidationError(self.error_messages['file_too_large'] % (
filesizeformat(f.size),
filesizeformat(max_size),
), code='file_too_large')
def to_python(self, data):

View file

@ -11,7 +11,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, ImageInsertionForm
from wagtail.wagtailimages.formats import get_image_format
from wagtail.wagtailimages.fields import get_max_image_filesize
from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE
def get_image_json(image):
@ -147,7 +147,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': get_max_image_filesize()}
{'images': images, 'uploadform': form, 'searchform': searchform, 'max_filesize': MAX_UPLOAD_SIZE}
)

View file

@ -17,7 +17,7 @@ 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.crypto import generate_signature
from wagtail.wagtailimages.fields import get_max_image_filesize
from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE
@permission_required('wagtailimages.add_image')
@ -224,7 +224,7 @@ def add(request):
return render(request, "wagtailimages/images/add.html", {
'form': form,
'max_filesize': get_max_image_filesize(),
'max_filesize': MAX_UPLOAD_SIZE,
})

View file

@ -15,7 +15,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 get_max_image_filesize
from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE, IMAGE_FIELD_HELP_TEXT, ALLOWED_EXTENSIONS
def json_response(document):
@ -81,7 +81,9 @@ def add(request):
})
return render(request, 'wagtailimages/multiple/add.html', {
'max_filesize': get_max_image_filesize(),
'max_filesize': MAX_UPLOAD_SIZE,
'help_text': IMAGE_FIELD_HELP_TEXT,
'allowed_extensions': ALLOWED_EXTENSIONS,
})