SQLFORM onvalidate

14 views
Skip to first unread message

leone

unread,
Oct 9, 2009, 8:20:41 AM10/9/09
to web2py-users
I have questions about SQLFORM onvalidate.
Before inserting a row I have to resize an image by PIL and push
b64encoded bytes on a blob field.
So I have defined onvalidate=DoSomething in SQLFORM.
DoSomething has form as input arg, resize image as thumbnails, encode
it and push in form.vars.myblob
(or db.mytable.myblob???).
The new record is OK, but blob field is empty.
Can someone orient me?

leone

unread,
Oct 9, 2009, 8:48:03 AM10/9/09
to web2py-users
excuse me: onvalidation not onvalidate.

leone

unread,
Oct 9, 2009, 9:49:49 AM10/9/09
to web2py-users
Solved:

if form.accepts(request.vars, session, keepvalues=True,
onvalidation=WriteThumb):
.........

def WriteThumb(form):
i = cStringIO.StringIO()
o = cStringIO.StringIO()
i.write(form.vars.imgfile.value)
i.seek(0)
im = Image.open(i, 'r')
im.thumbnail((100,100),Image.ANTIALIAS)
im.save(o,'jpeg')
form.vars.thumbnail = base64.b64encode(o.getvalue())
o.close()
i.close()

<img src="data:image/jpeg;base64,{{=row['thumbnail']}}"
alt="Thumbnail photo" />

leone

unread,
Oct 9, 2009, 9:51:04 AM10/9/09
to web2py-users
I forget:

from PIL import Image
Reply all
Reply to author
Forward
0 new messages