Re: [web2py] Re: delete uploads with SQLFORM.factory
Hi, I had a look at sqlform.py, the error comes from the fact that if a default value is set for the field upload, web2py tries to read it and store it as the new value for the upload field. As a result it is not possible to me to edit forms and remove uploads by setting the default value, this trick doesn't work. Moreover, I tried to fix the initial error about the missing file, by setting db.test_img['picture'].default equal to real path of the file. This worked well but than, if I understood correctly how web2py works, it tried to store again the default value as the new value, starting a kind of loop! That was wrong, and actually this has never worked, it stopped due to the 'error filename' to long. The good thing is that by changing this two line (line 1394 of sqlhtml.py) f = self.table[fieldname].default or '' fields[fieldname] = f to: fields[fieldname] = self.table[fieldname].default It seems that this solved the issue, but of course, I need something better. 2012/12/27 Paolo paolo.vall...@gmail.com Hi Massimo, thank for the answer, I updated the code but the problem is still there, I made several tests, no one figured out what is wrong. So that I tried to simplify the code as follows: db.define_table('test_img', Field(picture, upload, uploadfolder=os.path.join(request.folder,'uploads','pictures'), uploadseparate=True), Field('created_on', 'datetime', default=request.now, writable=False) ) def sqlform_add(): form = SQLFORM.factory(db.test_img, _class='well', upload=URL('default', 'download'), uploadfolder=os.path.join(request.folder, 'uploads','pictures'), uploadseparate=True, table_name='test') if form.process(dbio=False).accepted: id = db.test_img.insert(**dict(form.vars)) return response.render('generic.html', dict(form=form)) def sqlform_edit(): img = db(db.test_img.id0).select().first() db.test_img['picture'].default = img['picture'] form = SQLFORM.factory(db.test_img, _class='well', upload=URL('default', 'download'), uploadfolder=os.path.join(request.folder, 'uploads','pictures'), uploadseparate=True, table_name='test') if form.process(dbio=False).accepted: print 'form accepted' return response.render('generic.html', dict(form=form)) what I got: - adding an img with sqlform_add and then removing it with appadmin -- worked - adding an img with appadmin and then removing it with sqlform_edit -- failed - using both sqlform_add and sqlform_edit -- failed Now I am thinking about the way I set the default value, maybe for a field as an upload I have to set something more? Paolo On Thursday, December 27, 2012 9:28:54 PM UTC+1, Massimo Di Pierro wrote: perhaps not the cause of the problem but request.folder+'uploads/**pictures' should be os.path.join(request.folder,'**uploads','pictures') On Thursday, 27 December 2012 00:30:00 UTC-6, Paolo wrote: Hi Massimo, I've just tried to post and then edit with both SQLFORM.factory having uploadseparate but nothing has changed. The problem is still there, actually I am making explicitly inserts and edits. The whole SQLFORM.factory to edit a field is this: for field in ['title','description', 'user_contact', 'picture' ]: db.club[field].default = club[field] for field in ['name']: db.cities[field].default = city[field] form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'),uploadfolder =request.folder+'**uploads/pictures', uploadseparate=True, autodelete=True,table_name ='club') Paolo On Thursday, December 27, 2012 12:47:19 AM UTC+1, Massimo Di Pierro wrote: Did you upload the file first and then add uploadseparate/uploadfolder? On Wednesday, 26 December 2012 12:49:01 UTC-6, Paolo wrote: Hi all, it seems to me that SQLFORM.factory doesn't honor the uploadseparate option because I'am not able to delete the uploaded file with SQLFORM.factory form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'), uploadfolder =request.folder+'**uploads/pictures', uploadseparate=True, autodelete=True,table_name ='club' ) The error: Enter code here...2012-12-26 19:38:07,728 - web2py - ERROR - Traceback (most recent call last): File /home/paolo/Dropbox/git/**web2py/gluon/restricted.py, line 212, in restricted exec ccode in environment File /home/paolo/Dropbox/git/**web2py/applications/bikend/** controllers/club.py, line 123, in module
[web2py] Re: delete uploads with SQLFORM.factory
Hi, I thought about the option requires, right now I have: requires=[IS_IMAGE(), IS_UPLOAD_FILENAME(extension='jpg|jpeg|png'), IS_IMAGE (extensions=('jpeg', 'png')) I thought that as defined, the field could not be empty, so, I tried to remove any constrain requires=None but then the delete button is no longer shown, why ?! I've also tried: requires=[] but in this case I got the same error as in the other cases. Paolo On Thursday, December 27, 2012 7:30:00 AM UTC+1, Paolo wrote: Hi Massimo, I've just tried to post and then edit with both SQLFORM.factory having uploadseparate but nothing has changed. The problem is still there, actually I am making explicitly inserts and edits. The whole SQLFORM.factory to edit a field is this: for field in ['title','description', 'user_contact','picture' ]: db.club[field].default = club[field] for field in ['name']: db.cities[field].default = city[field] form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'),uploadfolder =request.folder+'uploads/pictures', uploadseparate=True, autodelete=True,table_name ='club') Paolo On Thursday, December 27, 2012 12:47:19 AM UTC+1, Massimo Di Pierro wrote: Did you upload the file first and then add uploadseparate/uploadfolder? On Wednesday, 26 December 2012 12:49:01 UTC-6, Paolo wrote: Hi all, it seems to me that SQLFORM.factory doesn't honor the uploadseparate option because I'am not able to delete the uploaded file with SQLFORM.factory form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'), uploadfolder= request.folder+'uploads/pictures', uploadseparate=True, autodelete=True, table_name= 'club' ) The error: Enter code here...2012-12-26 19:38:07,728 - web2py - ERROR - Traceback (most recent call last): File /home/paolo/Dropbox/git/web2py/gluon/restricted.py, line 212, in restricted exec ccode in environment File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 123, in module File /home/paolo/Dropbox/git/web2py/gluon/globals.py, line 193, in lambda self._caller = lambda f: f() File /home/paolo/Dropbox/git/web2py/gluon/tools.py, line 2935, in f return action(*a, **b) File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 35, in modal d = {'form':club_form(c_id=club_id)} File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 103, in club_form if form.process(dbio=False).accepted: File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2179, inprocess self.validate(**kwargs) File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2118, invalidate if self.accepts(**kwargs): File /home/paolo/Dropbox/git/web2py/gluon/sqlhtml.py, line 1408, inaccepts source_file = open(f, 'rb') IOError: [Errno 2] No such file or directory: '/home/paolo/Dropbox/git/web2py/applications/bikend/club.picture.a4bd97789f289bcc.6c6f676f322e706e67.png' It tries to remove the file in the wrong directory. The right one would be: cod... Show originalhttps://groups.google.com/group/web2py/msg/ff7178943040387c?dmode=sourceoutput=gplainnoredirect --
[web2py] Re: delete uploads with SQLFORM.factory
perhaps not the cause of the problem but request.folder+'uploads/pictures' should be os.path.join(request.folder,'uploads','pictures') On Thursday, 27 December 2012 00:30:00 UTC-6, Paolo wrote: Hi Massimo, I've just tried to post and then edit with both SQLFORM.factory having uploadseparate but nothing has changed. The problem is still there, actually I am making explicitly inserts and edits. The whole SQLFORM.factory to edit a field is this: for field in ['title','description', 'user_contact','picture' ]: db.club[field].default = club[field] for field in ['name']: db.cities[field].default = city[field] form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'),uploadfolder =request.folder+'uploads/pictures', uploadseparate=True, autodelete=True,table_name ='club') Paolo On Thursday, December 27, 2012 12:47:19 AM UTC+1, Massimo Di Pierro wrote: Did you upload the file first and then add uploadseparate/uploadfolder? On Wednesday, 26 December 2012 12:49:01 UTC-6, Paolo wrote: Hi all, it seems to me that SQLFORM.factory doesn't honor the uploadseparate option because I'am not able to delete the uploaded file with SQLFORM.factory form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'), uploadfolder= request.folder+'uploads/pictures', uploadseparate=True, autodelete=True, table_name= 'club' ) The error: Enter code here...2012-12-26 19:38:07,728 - web2py - ERROR - Traceback (most recent call last): File /home/paolo/Dropbox/git/web2py/gluon/restricted.py, line 212, in restricted exec ccode in environment File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 123, in module File /home/paolo/Dropbox/git/web2py/gluon/globals.py, line 193, in lambda self._caller = lambda f: f() File /home/paolo/Dropbox/git/web2py/gluon/tools.py, line 2935, in f return action(*a, **b) File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 35, in modal d = {'form':club_form(c_id=club_id)} File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 103, in club_form if form.process(dbio=False).accepted: File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2179, inprocess self.validate(**kwargs) File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2118, invalidate if self.accepts(**kwargs): File /home/paolo/Dropbox/git/web2py/gluon/sqlhtml.py, line 1408, inaccepts source_file = open(f, 'rb') IOError: [Errno 2] No such file or directory: '/home/paolo/Dropbox/git/web2py/applications/bikend/club.picture.a4bd97789f289bcc.6c6f676f322e706e67.png' It tries to remove the file in the wrong directory. The right one would be: cod... Show originalhttps://groups.google.com/group/web2py/msg/ff7178943040387c?dmode=sourceoutput=gplainnoredirect --
[web2py] Re: delete uploads with SQLFORM.factory
Hi Massimo, thank for the answer, I updated the code but the problem is still there, I made several tests, no one figured out what is wrong. So that I tried to simplify the code as follows: db.define_table('test_img', Field(picture, upload, uploadfolder=os.path.join(request.folder,'uploads','pictures'), uploadseparate=True), Field('created_on', 'datetime', default=request.now, writable=False) ) def sqlform_add(): form = SQLFORM.factory(db.test_img, _class='well', upload=URL('default', 'download'), uploadfolder=os.path.join(request.folder, 'uploads','pictures'), uploadseparate=True, table_name='test') if form.process(dbio=False).accepted: id = db.test_img.insert(**dict(form.vars)) return response.render('generic.html', dict(form=form)) def sqlform_edit(): img = db(db.test_img.id0).select().first() db.test_img['picture'].default = img['picture'] form = SQLFORM.factory(db.test_img, _class='well', upload=URL('default', 'download'), uploadfolder=os.path.join(request.folder, 'uploads','pictures'), uploadseparate=True, table_name='test') if form.process(dbio=False).accepted: print 'form accepted' return response.render('generic.html', dict(form=form)) what I got: - adding an img with sqlform_add and then removing it with appadmin -- worked - adding an img with appadmin and then removing it with sqlform_edit -- failed - using both sqlform_add and sqlform_edit -- failed Now I am thinking about the way I set the default value, maybe for a field as an upload I have to set something more? Paolo On Thursday, December 27, 2012 9:28:54 PM UTC+1, Massimo Di Pierro wrote: perhaps not the cause of the problem but request.folder+'uploads/pictures' should be os.path.join(request.folder,'uploads','pictures') On Thursday, 27 December 2012 00:30:00 UTC-6, Paolo wrote: Hi Massimo, I've just tried to post and then edit with both SQLFORM.factory having uploadseparate but nothing has changed. The problem is still there, actually I am making explicitly inserts and edits. The whole SQLFORM.factory to edit a field is this: for field in ['title','description', 'user_contact','picture' ]: db.club[field].default = club[field] for field in ['name']: db.cities[field].default = city[field] form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'),uploadfolder =request.folder+'uploads/pictures', uploadseparate=True, autodelete=True,table_name ='club') Paolo On Thursday, December 27, 2012 12:47:19 AM UTC+1, Massimo Di Pierro wrote: Did you upload the file first and then add uploadseparate/uploadfolder? On Wednesday, 26 December 2012 12:49:01 UTC-6, Paolo wrote: Hi all, it seems to me that SQLFORM.factory doesn't honor the uploadseparate option because I'am not able to delete the uploaded file with SQLFORM.factory form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'), uploadfolder= request.folder+'uploads/pictures', uploadseparate=True, autodelete=True, table_name ='club' ) The error: Enter code here...2012-12-26 19:38:07,728 - web2py - ERROR - Traceback (most recent call last): File /home/paolo/Dropbox/git/web2py/gluon/restricted.py, line 212, in restricted exec ccode in environment File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 123, in module File /home/paolo/Dropbox/git/web2py/gluon/globals.py, line 193, in lambda self._caller = lambda f: f() File /home/paolo/Dropbox/git/web2py/gluon/tools.py, line 2935, in f return action(*a, **b) File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 35, in modal d = {'form':club_form(c_id=club_id)} File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py , line 103, in club_form if form.process(dbio=False).accepted: File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2179, inprocess self.validate(**kwargs) File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2118, invalidate if self.accepts(**kwargs): File /home/paolo/Dropbox/git/web2py/gluon/sqlhtml.py, line 1408, inaccepts source_file = open(f, 'rb') IOError: [Errno 2] No such file or directory: '/home/paolo/Dropbox/git/web2py/applications/bikend/club.picture.a4bd97789f289bcc.6c6f676f322e706e67.png' It tries to remove the file in the wrong directory. The right
[web2py] Re: delete uploads with SQLFORM.factory
Did you upload the file first and then add uploadseparate/uploadfolder? On Wednesday, 26 December 2012 12:49:01 UTC-6, Paolo wrote: Hi all, it seems to me that SQLFORM.factory doesn't honor the uploadseparate option because I'am not able to delete the uploaded file with SQLFORM.factory form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'), uploadfolder= request.folder+'uploads/pictures', uploadseparate=True, autodelete=True, table_name= 'club' ) The error: Enter code here...2012-12-26 19:38:07,728 - web2py - ERROR - Traceback (most recent call last): File /home/paolo/Dropbox/git/web2py/gluon/restricted.py, line 212, inrestricted exec ccode in environment File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py,line 123, in module File /home/paolo/Dropbox/git/web2py/gluon/globals.py, line 193, in lambda self._caller = lambda f: f() File /home/paolo/Dropbox/git/web2py/gluon/tools.py, line 2935, in f return action(*a, **b) File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py,line 35, in modal d = {'form':club_form(c_id=club_id)} File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py,line 103, in club_form if form.process(dbio=False).accepted: File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2179, inprocess self.validate(**kwargs) File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2118, invalidate if self.accepts(**kwargs): File /home/paolo/Dropbox/git/web2py/gluon/sqlhtml.py, line 1408, inaccepts source_file = open(f, 'rb') IOError: [Errno 2] No such file or directory: '/home/paolo/Dropbox/git/web2py/applications/bikend/club.picture.a4bd97789f289bcc.6c6f676f322e706e67.png' It tries to remove the file in the wrong directory. The right one would be: /home/paolo/Dropbox/git/span style=color: #000; class=styled-b... Show originalhttps://groups.google.com/group/web2py/msg/77f8e5478e8b77b8?dmode=sourceoutput=gplainnoredirect --
[web2py] Re: delete uploads with SQLFORM.factory
Hi Massimo, I've just tried to post and then edit with both SQLFORM.factory having uploadseparate but nothing has changed. The problem is still there, actually I am making explicitly inserts and edits. The whole SQLFORM.factory to edit a field is this: for field in ['title','description', 'user_contact','picture' ]: db.club[field].default = club[field] for field in ['name']: db.cities[field].default = city[field] form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'),uploadfolder =request.folder+'uploads/pictures', uploadseparate=True, autodelete=True,table_name ='club') Paolo On Thursday, December 27, 2012 12:47:19 AM UTC+1, Massimo Di Pierro wrote: Did you upload the file first and then add uploadseparate/uploadfolder? On Wednesday, 26 December 2012 12:49:01 UTC-6, Paolo wrote: Hi all, it seems to me that SQLFORM.factory doesn't honor the uploadseparate option because I'am not able to delete the uploaded file with SQLFORM.factory form = SQLFORM.factory(db.club, db.cities, _class='well', formstyle='bootstrap', showid= False, upload=URL('default', 'download'), uploadfolder= request.folder+'uploads/pictures', uploadseparate=True, autodelete=True, table_name= 'club' ) The error: Enter code here...2012-12-26 19:38:07,728 - web2py - ERROR - Traceback (most recent call last): File /home/paolo/Dropbox/git/web2py/gluon/restricted.py, line 212, inrestricted exec ccode in environment File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py,line 123, in module File /home/paolo/Dropbox/git/web2py/gluon/globals.py, line 193, in lambda self._caller = lambda f: f() File /home/paolo/Dropbox/git/web2py/gluon/tools.py, line 2935, in f return action(*a, **b) File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py,line 35, in modal d = {'form':club_form(c_id=club_id)} File /home/paolo/Dropbox/git/web2py/applications/bikend/controllers/club.py,line 103, in club_form if form.process(dbio=False).accepted: File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2179, inprocess self.validate(**kwargs) File /home/paolo/Dropbox/git/web2py/gluon/html.py, line 2118, invalidate if self.accepts(**kwargs): File /home/paolo/Dropbox/git/web2py/gluon/sqlhtml.py, line 1408, inaccepts source_file = open(f, 'rb') IOError: [Errno 2] No such file or directory: '/home/paolo/Dropbox/git/web2py/applications/bikend/club.picture.a4bd97789f289bcc.6c6f676f322e706e67.png' It tries to remove the file in the wrong directory. The right one would be: /home/paolo/Dropbox/git/span style=color: #000; class=styled-b... Show originalhttps://groups.google.com/group/web2py/msg/77f8e5478e8b77b8?dmode=sourceoutput=gplainnoredirect --