From 71c48e01edc2b0d9f9c598fa4eac36fd073b475e Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 8 Oct 2014 14:55:43 +0100 Subject: [PATCH] WagtailImageField help text. Some refactoring --- wagtail/wagtailimages/fields.py | 61 +++++++++++++++++-------- wagtail/wagtailimages/views/chooser.py | 4 +- wagtail/wagtailimages/views/images.py | 4 +- wagtail/wagtailimages/views/multiple.py | 6 ++- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/wagtail/wagtailimages/fields.py b/wagtail/wagtailimages/fields.py index 10d076bd4..489900f7a 100644 --- a/wagtail/wagtailimages/fields.py +++ b/wagtail/wagtailimages/fields.py @@ -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): diff --git a/wagtail/wagtailimages/views/chooser.py b/wagtail/wagtailimages/views/chooser.py index 2ce319e7e..bdab741c6 100644 --- a/wagtail/wagtailimages/views/chooser.py +++ b/wagtail/wagtailimages/views/chooser.py @@ -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} ) diff --git a/wagtail/wagtailimages/views/images.py b/wagtail/wagtailimages/views/images.py index d75373c2b..a2a6d4825 100644 --- a/wagtail/wagtailimages/views/images.py +++ b/wagtail/wagtailimages/views/images.py @@ -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, }) diff --git a/wagtail/wagtailimages/views/multiple.py b/wagtail/wagtailimages/views/multiple.py index 1d1119306..3f4865ca2 100644 --- a/wagtail/wagtailimages/views/multiple.py +++ b/wagtail/wagtailimages/views/multiple.py @@ -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, })