unifying of messages and added to uploader

pull/676/head
Dave Cranwell 2014-10-08 15:35:35 +01:00 zatwierdzone przez Karl Hobley
rodzic 82657344de
commit 66c92f0333
4 zmienionych plików z 26 dodań i 23 usunięć

Wyświetl plik

@ -9,44 +9,43 @@ from django.template.defaultfilters import filesizeformat
from django.conf import settings from django.conf import settings
ALLOWED_EXTENSIONS = ['git', 'jpg', 'jpeg', 'png'] ALLOWED_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png']
ALLOWED_EXTENSIONS_TEXT = _("Supported formats: GIF, JPEG, PNG")
INVALID_IMAGE_ERROR = _( INVALID_IMAGE_ERROR = _(
"Not a supported image type. Please use a gif, jpeg or png file " "Not a supported image format. %s"
"with the correct file extension (*.gif, *.jpg or *.png)." ) % ALLOWED_EXTENSIONS_TEXT
)
INVALID_IMAGE_KNOWN_FORMAT_ERROR = _( INVALID_IMAGE_KNOWN_FORMAT_ERROR = _(
"Not a valid %s image. Please use a gif, jpeg or png file " "Not a valid %s image."
"with the correct file extension (*.gif, *.jpg or *.png)."
) )
MAX_UPLOAD_SIZE = getattr(settings, 'WAGTAILIMAGES_MAX_UPLOAD_SIZE', 10 * 1024 * 1024) MAX_UPLOAD_SIZE = getattr(settings, 'WAGTAILIMAGES_MAX_UPLOAD_SIZE', 10 * 1024 * 1024)
if MAX_UPLOAD_SIZE is not None: if MAX_UPLOAD_SIZE is not None:
MAX_UPLOAD_SIZE_TEXT = filesizeformat(MAX_UPLOAD_SIZE) MAX_UPLOAD_SIZE_TEXT = _("Maximum filesize: %s") % filesizeformat(MAX_UPLOAD_SIZE)
FILE_TOO_LARGE_ERROR = _( FILE_TOO_LARGE_ERROR = _("This file is too big. %s.") % (MAX_UPLOAD_SIZE_TEXT, )
"This file is too big (%%s). Image files must not exceed %s."
FILE_TOO_LARGE_ERROR_SIZE_KNOWN = _(
"This file is too big (%%s). %s."
) % (MAX_UPLOAD_SIZE_TEXT, ) ) % (MAX_UPLOAD_SIZE_TEXT, )
IMAGE_FIELD_HELP_TEXT = _( IMAGE_FIELD_HELP_TEXT = _(
"Supported formats: gif, jpeg, png. Max size: %s" "%s. %s"
) % (MAX_UPLOAD_SIZE_TEXT, ) ) % (ALLOWED_EXTENSIONS_TEXT, MAX_UPLOAD_SIZE_TEXT, )
else: else:
MAX_UPLOAD_SIZE_TEXT = "" MAX_UPLOAD_SIZE_TEXT = ""
FILE_TOO_LARGE_ERROR = "" FILE_TOO_LARGE_ERROR = ""
IMAGE_FIELD_HELP_TEXT = _( IMAGE_FIELD_HELP_TEXT = ALLOWED_EXTENSIONS_TEXT
"Supported formats: gif, jpeg, png."
)
class WagtailImageField(ImageField): class WagtailImageField(ImageField):
default_error_messages = { default_error_messages = {
'invalid_image': INVALID_IMAGE_ERROR, 'invalid_image': INVALID_IMAGE_ERROR,
'invalid_image_known_format': INVALID_IMAGE_KNOWN_FORMAT_ERROR, 'invalid_image_known_format': INVALID_IMAGE_KNOWN_FORMAT_ERROR,
'file_too_large': FILE_TOO_LARGE_ERROR, 'file_too_large': FILE_TOO_LARGE_ERROR_SIZE_KNOWN,
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -73,7 +72,8 @@ class WagtailImageField(ImageField):
image = Image.open(f) image = Image.open(f)
except IOError: except IOError:
# Uploaded file is not even an image file (or corrupted) # Uploaded file is not even an image file (or corrupted)
raise ValidationError(self.error_messages['invalid_image'], code='invalid_image') raise ValidationError(self.error_messages['invalid_image_known_format'],
code='invalid_image_known_format')
f.seek(file_position) f.seek(file_position)
else: else:

Wyświetl plik

@ -21,8 +21,8 @@ $(function(){
previewMinHeight:150, previewMinHeight:150,
previewMaxHeight:150, previewMaxHeight:150,
messages: { messages: {
acceptFileTypes: window.fileupload_opts.messages.accepted_file_types, acceptFileTypes: window.fileupload_opts.errormessages.accepted_file_types,
maxFileSize: window.fileupload_opts.messages.max_file_size maxFileSize: window.fileupload_opts.errormessages.max_file_size
}, },
add: function (e, data) { add: function (e, data) {
var $this = $(this); var $this = $(this);

Wyświetl plik

@ -16,6 +16,7 @@
<div class="nice-padding"> <div class="nice-padding">
<div class="drop-zone"> <div class="drop-zone">
<p>{% trans "Drag and drop images into this area to upload immediately." %}</p> <p>{% trans "Drag and drop images into this area to upload immediately." %}</p>
<p>{{ help_text }}
<form action="{% url 'wagtailimages_add_multiple' %}" method="POST" enctype="multipart/form-data"> <form action="{% url 'wagtailimages_add_multiple' %}" method="POST" enctype="multipart/form-data">
<div class="replace-file-input"> <div class="replace-file-input">
@ -72,11 +73,11 @@
<script> <script>
window.fileupload_opts = { window.fileupload_opts = {
simple_upload_url: "{% url 'wagtailimages_add_image' %}", simple_upload_url: "{% url 'wagtailimages_add_image' %}",
accepted_file_types: /(\.|\/)(gif|jpe?g|png)$/i, //must be regex accepted_file_types: /(\.|\/)({% for extn in allowed_extensions %}{{ extn }}{% if not forloop.last %}|{% endif %}{% endfor %})$/i, //must be regex
max_file_size: {{ max_filesize }}, //numeric format max_file_size: {{ max_filesize }}, //numeric format
messages: { errormessages: {
max_file_size: "max filesize message", max_file_size: "{{ error_max_file_size }}",
accepted_file_types: "filetypes message" accepted_file_types: "{{ error_accepted_file_types }}"
} }
} }
window.tagit_opts = { window.tagit_opts = {

Wyświetl plik

@ -15,7 +15,7 @@ from wagtail.wagtailsearch.backends import get_search_backends
from wagtail.wagtailimages.models import get_image_model from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.forms import get_image_form from wagtail.wagtailimages.forms import get_image_form
from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE, IMAGE_FIELD_HELP_TEXT, ALLOWED_EXTENSIONS from wagtail.wagtailimages.fields import MAX_UPLOAD_SIZE, IMAGE_FIELD_HELP_TEXT, INVALID_IMAGE_ERROR, ALLOWED_EXTENSIONS, ALLOWED_EXTENSIONS_TEXT, FILE_TOO_LARGE_ERROR
def json_response(document): def json_response(document):
@ -84,6 +84,8 @@ def add(request):
'max_filesize': MAX_UPLOAD_SIZE, 'max_filesize': MAX_UPLOAD_SIZE,
'help_text': IMAGE_FIELD_HELP_TEXT, 'help_text': IMAGE_FIELD_HELP_TEXT,
'allowed_extensions': ALLOWED_EXTENSIONS, 'allowed_extensions': ALLOWED_EXTENSIONS,
'error_max_file_size': FILE_TOO_LARGE_ERROR,
'error_accepted_file_types': INVALID_IMAGE_ERROR,
}) })