I need to be able to dispatch to a different controller based on a database 
lookup. So a user will go to a url (say '/dispatch'), and we'll look up in 
the database some information on that user, choose a new controller and 
function, and call that controller and function with its view.

I've almost got this working below, but the models are not being loaded 
into the new controller. Is there a way to fix that?

In default.py:
def dispatch():
    controller,function = ... load these from the database ...
    response.view = '%s/%s.html' % (controller,
                                    function)
    
    if not os.path.exists(request.folder + '/views/' + response.view):
        response.view = 'generic.html'

    from gluon.shell import exec_environment 
    controller = exec_environment('%s/controllers/%s.py'
                                  % (request.folder,
                                     controller),
                                  request=request,
                                  response=response,
                                  session=session)
    return controller[request.task_function]()

Unfortunately, the controller being called has access to request, response, 
and session, but none of the global variables defined in my models. Is 
there a way to get exec_environment() to run a function in another 
controller WITHOUT losing all the model definitions?

Or is there a better way to do this?

Reply via email to