From 3b8f30d223a7f307223bdd1312b0428594c07ef4 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 3 Jun 2015 14:23:58 +0100 Subject: [PATCH] Moved image field help text/error message generation into __init__ This allows us to override the WAGTAILIMAGES_MAX_UPLOAD_SIZE setting for testing --- wagtail/wagtailimages/fields.py | 86 ++++++++++--------------- wagtail/wagtailimages/views/chooser.py | 3 +- wagtail/wagtailimages/views/images.py | 2 - wagtail/wagtailimages/views/multiple.py | 18 ++---- 4 files changed, 43 insertions(+), 66 deletions(-) diff --git a/wagtail/wagtailimages/fields.py b/wagtail/wagtailimages/fields.py index a674568eb..36351caf7 100644 --- a/wagtail/wagtailimages/fields.py +++ b/wagtail/wagtailimages/fields.py @@ -12,60 +12,44 @@ 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 (%%(max_upload_size)s). Maximum filesize %s." + ) % { + 'max_upload_size': max_upload_size_text, + } def check_image_file_format(self, f): # Check file extension @@ -111,11 +95,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') diff --git a/wagtail/wagtailimages/views/chooser.py b/wagtail/wagtailimages/views/chooser.py index fbfd9bbb2..edc6ac387 100644 --- a/wagtail/wagtailimages/views/chooser.py +++ b/wagtail/wagtailimages/views/chooser.py @@ -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} ) diff --git a/wagtail/wagtailimages/views/images.py b/wagtail/wagtailimages/views/images.py index 339ee4a8f..eaf00cc96 100644 --- a/wagtail/wagtailimages/views/images.py +++ b/wagtail/wagtailimages/views/images.py @@ -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, }) diff --git a/wagtail/wagtailimages/views/multiple.py b/wagtail/wagtailimages/views/multiple.py index 71888a3d2..29eb9ae5d 100644 --- a/wagtail/wagtailimages/views/multiple.py +++ b/wagtail/wagtailimages/views/multiple.py @@ -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'], })