SQLFORM update or insert depending on record existence checked at callback... and side effects

22 views
Skip to first unread message

fred....@gmail.com

unread,
Sep 6, 2015, 12:55:25 AM9/6/15
to web2py-users
Greetings,

I asked the question here in title on StackOverflow and it seems that the solution Richard nicely brought to me/us should work "as is", but it also seem to have side effects in my environment so that I can't make it work properly.

So I came here, on his suggestion, in order to try to understand how it could work as needed.

The big picture:
  • My application manages "objects" (say, celestial phenomena), sometimes they have variants, and I'm on managing they occurrences.
  • I need to produce a very simple interface, in only one form with 2 or 3 fields on one page (demanding customer requirement ^^).
  • Object occurrences are mainly defined by a reference id and a file that can be uploaded later.
  • Uploading the file triggers a scheduler task that will process it and will remove it form application and database once processed. (this part is not mentioned in my post to StackOverflow because I didn't want to overload it and I did not expect side effects on it).
So I came to a model that partly looks like this:
db.define_table('t_object_occurence',
   
Field('f_refid', type='string', label=T('Reference')),
   
Field('f_object_id', 'reference t_object', label=T('Object')),
   
# this one should store the uploaded file name, right ?
   
Field('f_temp_file', type='upload', label=T('File'), autodelete=True)
   
# this one will be used to store the original file name as sugested in litterature
   
Field('f_temp_filename', type='string', readable=False, writable=False),
   
# this last one will tell me if the file has already been uploaded and processed
   
Field('f_temp_file_processed', type='boolean', writable=True),
   
#more fileds
)

Following Richard's solution, I had to put all of my checks and processing right in onvalidation_insert_or_update(form) but something goes wrong when it comes to build the uploaded file path, expression:
os.path.join(os.path.abspath('.'),'applications/'+request.application+'/uploads/'+form.vars.f_temp_file)

raises a
TypeError: cannot concatenate 'str' and 'instance' objects

whereas it just works like a charm when it is executed inside the controller, right after
if form.process(dbio=False, onvalidation=onvalidation_insert_or_update).accepted:

(How) can I get the uploaded file name from inside onvalidation_insert_or_update()?

By the way, I'm not sure but it seems that there is no need to put a db.commit() inside this function. Is it correct?

Thank you,
Fred.

PS: sorry if the post comes twice, I first set the type of post as "ask a question" but I did not see it appearing, so I send it again with its type set as "start a discussion"
Reply all
Reply to author
Forward
0 new messages