Does this works? Filename still shows None. On Wednesday, June 4, 2014 at 6:59:05 AM UTC-4, Michael Beller wrote: > > I used compute fields and it now works fine. Unless I did something > wrong, the instructions in the book for storing the original filename only > work for the initial insert and does not address updating the field that > contains the original filename during an update. > > Here is my final code (I know there are other threads that discuss how to > store file attributes for upload fields): > > def contracts_filename(row): > if request.vars.survey != None and not isinstance(request.vars.survey, > str): > return request.vars.survey.filename > > def contracts_filetype(row): > if request.vars.survey != None and not isinstance(request.vars.survey, > str): > return request.vars.survey.filename.split('.')[-1] > > def contracts_filesize(row): > if request.vars.survey != None and not isinstance(request.vars.survey, > str): > return request.env.content_length > > db.define_table('contracts', > Field('person', 'reference person'), > Field('survey_filename', compute=contracts_filename), > Field('survey_filetype', compute=contracts_filetype), > Field('survey_filesize', 'integer', compute= > contracts_filesize), > Field('survey', 'upload', autodelete=True), # uploadfolder > <== would be nice if this supported lambda's!! > Field('description'), > auth.signature > ) > > and my controller ... > > def index(): > grid=SQLFORM.smartgrid(db.person, > fields=[db.contracts.person, db.contracts. > survey_filename, db.contracts.survey_filesize, > db.contracts.survey_filetype, db. > contracts.survey, db.contracts.description > ] > ) > return dict(grid=grid) > > Two extra items to note: > > - I don't think the size stored is exactly correct since it's using > content-length but this is close enough for my purposes > - The compute fields do not display in edit forms by default so you > must explicitly list them in the smartgrid (or grid, sqlform) fields > argument. > > > On Monday, June 2, 2014 9:03:32 AM UTC-4, Michael Beller wrote: >> >> I'm trying to store the original filename, filetype, and filesize for an >> upload field using a smartgrid. >> >> I can add these fields during the insert (by setting the field defaults) >> but not update the fields if you update the uploaded file. The edit form >> does upload the new file but I don't know how to then update the stored >> attributes. The upload field, request.vars.survey, initially contains the >> cgi.FieldStorage object during insert (from the new form). However, the >> edit form then contains the string for the new filename in >> request.vars.survey unless their is a new file submitted. But I don't know >> how to update the stored file attributes for the form before the smartgrid >> processes the updated upload file. >> >> db.define_table('person', >> Field('first_name'), >> Field('last_name'), >> auth.signature, >> format = '%(first_name)s %(last_name)s' >> ) >> >> db.define_table('contracts', >> Field('person', 'reference person'), >> Field('survey_filename', writable=False), >> Field('survey_filetype', writable=False), >> Field('survey_filesize', 'integer', writable=False), >> Field('survey', 'upload', autodelete=True), >> Field('description'), >> auth.signature >> ) >> >> def index(): >> >> if request.vars.survey != None and not type(request.vars.survey) is >> str: >> db.contracts.survey_filename.default = >> request.vars.survey.filename >> db.contracts.survey_filesize.default = request.env.content_length >> db.contracts.survey_filetype.default = >> request.vars.survey.filename.split('.')[-1] >> >> grid=SQLFORM.smartgrid(db.person) >> return dict(grid=grid) >> >
-- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.