db.person._after_insert.append(lambda f,id: pprint(f,id)) #book's example
#my code in model file after table definition code.
db.extracted_linear._after_insert.append(
lambda f, id:wiki_update_or_insert(
'extracted_linear',
f["name"],
table_slug(f["name"]),
id))The book says Herefis a dict of fields passed to insert or update,idis the id of the newly inserted record,sis the Set object used for update or delete.
#This is after my table definition inside my model filedb.extracted_linear._after_insert.append( lambda f, id:wiki_update_or_insert( tablename='extracted_linear', id=id, name=f['name'] or f['measuremnt']))db.extracted_linear._after_update.append( lambda s, id:wiki_update_or_insert( tablename='extracted_linear', id=id, colset=s(db.extracted_linear.id==id)))
#Here is function in the controller
def wiki_update_or_insert(**fields): newfields = fields or dict(colset) try: title=newfields['name'] or newfields['tablename'] + ' ' + newfields['id']['id'] except: title=newfields['tablename'] + ' ' + newfields['id']['id'] db.wiki_page.update_or_insert( title=title, slug=table_slug(title), body=MARKMIN('#ADD CODE TO DISPLAY GRAPHS HERE'), tablename=newfields['tablename'], record_id=newfields['id']['id'])
Traceback (most recent call last):
File "C:\web2py_src_2.2.1\web2py\gluon\restricted.py", line 212, in restricted
exec ccode in environment
File "C:/web2py_src_2.2.1/web2py/applications/PROD/controllers/default.py", line 173, in <module>
File "C:\web2py_src_2.2.1\web2py\gluon\globals.py", line 188, in <lambda>
self._caller = lambda f: f()
File "C:\web2py_src_2.2.1\web2py\gluon\tools.py", line 2911, in f
return action(*a, **b)
File "C:/web2py_src_2.2.1/web2py/applications/PROD/controllers/default.py", line 67, in linear_manage
form = SQLFORM.smartgrid(db.extracted_linear)
File "C:\web2py_src_2.2.1\web2py\gluon\sqlhtml.py", line 2376, in smartgrid
user_signature=user_signature, **kwargs)
File "C:\web2py_src_2.2.1\web2py\gluon\sqlhtml.py", line 1882, in grid
next=referrer)
File "C:\web2py_src_2.2.1\web2py\gluon\html.py", line 2170, in process
self.validate(**kwargs)
File "C:\web2py_src_2.2.1\web2py\gluon\html.py", line 2109, in validate
if self.accepts(**kwargs):
File "C:\web2py_src_2.2.1\web2py\gluon\sqlhtml.py", line 1473, in accepts
self.id_field_name]).update(**fields)
File "C:\web2py_src_2.2.1\web2py\gluon\dal.py", line 8814, in update
ret and [f(self,update_fields) for f in table._after_update]
File "C:/web2py_src_2.2.1/web2py/applications/PROD/models/db_wizard.py", line 118, in <lambda>
colset=s(db.extracted_linear.id==id)))
File "C:/web2py_src_2.2.1/web2py/applications/PROD/controllers/default.py", line 44, in wiki_update_or_insert
title=newfields['tablename'] + ' ' + newfields['id']['id']
KeyError: 'id'
def insert_wiki_after_insert(table, f, id): """Used for auto creation of wiki pages when a new item is added to database To use: In model file after table definition enter lines: db.mytable.wiki_page.required=False db.mytable._after_insert.append(lambda f,id: \ insert_wiki_after_insert(db.mytable, f, id)) @see_also gluon.dal.RecordUpdater, """ tablename=table._tablename name = f['name'] or f['title'] or tablename + ' ' + id wiki_page=db.wiki_page.update_or_insert( db.wiki_page.title==name, title=name.strip(), slug=table_slug(name.strip()), body='#ADD PLUGIN CODE TO DISPLAY GRAPHS HERE\n' + \ '##Use WIKI menu to edit or delete this page', changelog='inserted after ' + tablename + ' table update', tags=None, tablename=tablename, record_id=id) db(table.id==id).update_naive(wiki_page=wiki_page) return
def insert_wiki_if_not_exists(table, s, f): """Used for auto creation of wiki pages for use with items already in your table. On any update to the table the wiki page will be create if it doesn't exist. To use: In model file after table definition enter lines: db.mytable._after_update.append(lambda s,f: \ insert_wiki_if_not_exist(db.mytable, s, f)) @see_also gluon.dal.RecordUpdater, """ tablename=table._tablename table_wikis_set=db(db.wiki_page.tablename==tablename)
for s_row in s.select(): for row in table_wikis_set.select(): id = s_row.id name = s_row.name or tablename + ' ' + id f['wiki_page']=db.wiki_page.update_or_insert( db.wiki_page.title==name, title=name, slug=table_slug(name.strip()), body='#ADD PLUGIN CODE TO DISPLAY GRAPHS HERE\n' + \ '##Use WIKI menu to edit or delete this page', changelog='inserted after ' + tablename + ' table update', tags=None, tablename=tablename, record_id=id) s.update_naive() return