possible bug on db.table.field.retrieve() function

37 views
Skip to first unread message

André Kablu

unread,
Feb 1, 2014, 4:48:51 PM2/1/14
to web2py-d...@googlegroups.com


Imagine this scenario:


TEMP_IMAGES
= path.join(request.folder, 'uploads/temp')
TEST_IMAGES = path.join(request.folder, 'uploads/test')

db
.define_table('temp_images',
               
Field('add_date', 'datetime', default=request.now),
               
Field('original', 'upload', autodelete=True,
                      uploadfolder
=TEMP_IMAGES),
               
Field('image', 'upload', autodelete=True,
                      uploadfolder
=TEMP_IMAGES)
db.temp_images.image.requires = lambda row: make_thumb(row.original) #THIS IS BRUNO ROCHA`s MAKE THUMB FUNCTION IN WEB2PY RECIPES/SLICES


db
.define_table('test_images',
               
Field('image', 'upload', autodelete=True,
                      uploadfolder
=TEST_IMAGES))




if I perform this:


row
= db.temp_images(1)
(filename, stream) = db.temp_images.original.retrieve(row.original)
    db
.test_images.insert(image=db.test_images.image.store(stream,  filename))


it stores the image in the other table / folder,




however if I perform this:


row
= db.temp_images(1)
(filename, stream) = db.temp_images.image.retrieve(row.image)
    db
.test_images.insert(image=db.test_images.image.store(stream,  filename))


I receive this error:

IOError: [Errno 2] No such file or directory: '/web2py/applications/test/uploads/temp/temp_images.image.8f7f4653c49cff58.74656d705f696d616765732e6f726967696e616c2e393736396565356638393539376332622e333833363334333635663634363537343631363936633566363236623637363432653661373036375f696d672e6a7067.jpg'


Note the size of the filename is not compatible with the size in folder, even the name does not have _thumb.jpg as in folder
temp_images.original.9769ee5f89597c2b.383634365f64657461696c5f626b67642e6a7067.jpg (this is in folder)


well, I was using this with tables with only one upload field with no problem, now that I have 2 or 3 fields, the function retrieve only work with the FIRST defined field.

maybe I am doing it wrong, but maybe there is a bug on retrieve function

Best regards!!






Anthony

unread,
Feb 1, 2014, 5:45:37 PM2/1/14
to web2py-d...@googlegroups.com
                Field('image', 'upload', autodelete=True,
                      uploadfolder
=TEMP_IMAGES)
db.temp_images.image.requires = lambda row: make_thumb(row.original) #THIS IS BRUNO ROCHA`s MAKE THUMB FUNCTION IN WEB2PY RECIPES/SLICES

The "requires" attribute takes a validator or list of validators. The value that is passed to the validator(s) is not the entire row but just the value of the field itself. So, if no file is uploaded for the "image" field, the lambda will just get an empty string (I think), which should cause row.original to raise an exception. Also, note that validators should return a tuple including the original (or transformed) value and either None or an error message.

Instead, you probably want to use a computed field:

Field('image', 'upload', autodelete=True, uploadfolder=TEMP_IMAGES,
      compute
=lambda row: make_thumb(row.original))

Anthony

Anthony

unread,
Feb 1, 2014, 5:48:10 PM2/1/14
to web2py-d...@googlegroups.com
Also, generally better to report this kind of thing on the users list, unless you have actually diagnosed the source of a bug or want to propose/discuss actual code changes to make a fix.

Anthony

Kablu®

unread,
Feb 1, 2014, 5:51:04 PM2/1/14
to web2py-d...@googlegroups.com
Ok I wrote it wrong, it is on compute field, and it is writing fine in the folder.

So this is not the case.

Ok next time I`ll put it on users list, sorry about that. I just wrote here b/c I works fine for first field so it is probably a bug


--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py-developers/WXJRoNci06I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Anthony

unread,
Feb 1, 2014, 8:58:10 PM2/1/14
to web2py-d...@googlegroups.com
We can't tell anything until we see the make_thumb code. I assume it either doesn't put the file in the proper folder (it would have to know what the uploadfolder is) or doesn't generate the filename properly.

Anthony

Kablu®

unread,
Feb 2, 2014, 8:56:51 AM2/2/14
to web2py-d...@googlegroups.com
And yes, it is writing in proper folder with correct name:
ORIGINAL: temp_images.original.9769ee5f89597c2b.383634365f64657461696c5f626b67642e6a7067.jpg
IMAGE(thumb): temp_images.original.9769ee5f89597c2b.383634365f64657461696c5f626b67642e6a7067_thumb.jpg

Also I can use BOTH IMAGES in controllers, views, helpers, download it and do whatever I need, the only thing I cannot do is to retrieve the image field, as you can see in the previous message it tries to open a file with a giant filename.

Anthony

unread,
Feb 2, 2014, 10:41:48 AM2/2/14
to
On Sunday, February 2, 2014 8:56:51 AM UTC-5, André Kablu wrote:
 
Did you modify that function in any way. At minimum, it looks like you would have to add code to account for the fact the the upload folder is /uploads/temp rather than /uploads?

Also, the recipe appears to be incomplete. It doesn't show how the thumbnail is retrieved properly (the view code also refers to "article.image", when there is in fact no "image" field in the model code). By keeping the original filename but just appending "_thumb", the built-in web2py retrieve method will not work. This is not a bug in web2py but in the recipe code.

And yes, it is writing in proper folder with correct name:
ORIGINAL: temp_images.original.9769ee5f89597c2b.383634365f64657461696c5f626b67642e6a7067.jpg
IMAGE(thumb): temp_images.original.9769ee5f89597c2b.383634365f64657461696c5f626b67642e6a7067_thumb.jpg

The above filename of the thumb image is not what web2py would be expecting upon retrieval. The prefix should be "temp_images.image" instead of "temp_images.original", and there should be no "_thumb" at the end (web2py assumes everything between the last two dots is a b16encoded string that needs to be decoded to restore the original filename).

Anthony
Reply all
Reply to author
Forward
0 new messages