I'm experimenting with dynamically generating functions, aka 'actions' in
controllers. However, I've been unsuccessful. I can use exec and closures
successfully in regular Python code, but I can't make it work with web2py.
Any thoughts on how to achieve this?
A closure example - FAILS in web2py:
top_pages = db(db.page.id > 0).select()
def add_actions(top_pages):
for page in top_pages:
def inneraction(msg):
sidebar = None
return dict(message=msg, sidebar=sidebar)
inneraction.__name__ = page.link_name
globals()[page.link_name] = inneraction
add_actions(top_pages)
A exec example - FAILS in web2py:
ACTION_TEMPLATE = """
def NEW_ACTION():
sidebar = None
return dict(message='s', sidebar=sidebar)
"""
top_pages = db(db.page.id > 0).select()
def makePages(pages):
for page in top_pages:
exec ACTION_TEMPLATE
NEW_ACTION.__name__ = page.link_name
globals()[page.link_name] = NEW_ACTION
makePages(pages)
Miguel