mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-28 16:38:17 +00:00
Moved image field help text/error message generation into __init__
This allows us to override the WAGTAILIMAGES_MAX_UPLOAD_SIZE setting for testing
This commit is contained in:
parent
15c6d027b4
commit
3b8f30d223
4 changed files with 43 additions and 66 deletions
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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'],
|
||||
'error_accepted_file_types': form.fields['file'].error_messages['invalid_image'],
|
||||
})
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue