forms: show uploaded images as thumbnails in form and validation pages (#8675)

This commit is contained in:
Frédéric Péters 2015-10-17 14:29:54 +02:00
parent 7088b86772
commit 39a88562d9
3 changed files with 17 additions and 5 deletions

2
debian/control vendored
View File

@ -9,7 +9,7 @@ X-Python-Version: 2.7
Package: wcs
Architecture: all
Depends: ${python:Depends}, python-quixote, python-scgi, graphviz, python-feedparser
Depends: ${python:Depends}, python-quixote, python-scgi, graphviz, python-feedparser, python-imaging
Recommends: python-dns, python-m2crypto, python-xlwt, python-qrcode, libjs-leaflet
Suggests: libapache2-mod-scgi | libapache-mod-scgi, python-libxml2, python-lasso, python-psycopg2
Description: web application to design and set up online forms

View File

@ -20,6 +20,11 @@ from StringIO import StringIO
import sys
import urllib2
try:
from PIL import Image
except ImportError:
Image = None
try:
import qrcode
except ImportError:
@ -908,7 +913,15 @@ class FormPage(Directory):
if tempfile['charset']:
response.set_charset(tempfile['charset'])
return get_session().get_tempfile_content(t).get_file_pointer().read()
file_pointer = get_session().get_tempfile_content(t).get_file_pointer()
if Image is not None and get_request().form.get('thumbnail') == '1':
image = Image.open(file_pointer)
image.thumbnail((500, 300))
image_thumb_fp = StringIO()
image.save(image_thumb_fp, "PNG")
return image_thumb_fp.getvalue()
else:
return file_pointer.read()
def validating(self, data):
self.html_top(self.formdef.name)

View File

@ -599,9 +599,8 @@ class FileWithPreviewWidget(CompositeWidget):
elif temp:
filetype = mimetypes.guess_type(temp.get('orig_filename', ''))
if filetype and filetype[0] and filetype[0].startswith('image'):
r += htmltext('<img alt="%s" src="tempfile?t=%s" />' % \
(temp.get('orig_filename', ''),
self.get('token')))
r += htmltext('<img alt="" src="tempfile?t=%s&thumbnail=1" />' % \
self.get('token'))
r += htmltext('</div>')
return r.getvalue()