This is working great! It's exactly what I needed, and makes my code much
simpler.
Thank you very much! I love it!
On Friday, May 11, 2012 5:03:51 AM UTC-7, Anthony wrote:
>
> Or to avoid a redirect, you can change the function and controller in a
> model file:
>
> db = DAL(...)
>
> if request.function == 'dispatch':
> request.controller, request.function = [fetch from db]
> response.view = '%s/%s.%s' % (request.controller, request.function,request
> .extension)
> response.generic_patterns = ['html'] # to enable the generic.html
> view if needed
>
> Anthony
>
> On Friday, May 11, 2012 6:07:56 AM UTC-4, simon wrote:
>>
>> You can do:
>>
>> def dispatch():
>> controller,function = ... load these from the database ...
>> redirect(URL(c=controller, f=function, vars=request.vars,
>> args=request.args))
>>
>>
>> On Friday, 11 May 2012 10:17:19 UTC+1, Michael Toomim wrote:
>>>
>>> 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?
>>>
>>