On Wed, Sep 30, 2009 at 1:51 AM, Mike Orr <[email protected]> wrote:
>
> On Tue, Sep 29, 2009 at 4:17 PM, Gael Pasgrimaud <[email protected]> wrote:
>>
>> Hi,
>>
>> I wonder if there is a way to add routes after initialization time
>>
>> Here is my use case. I've created a rest controller[1] in FormAlchemy
>> to generate a CRUD interface for a model (SA mapper or whatever).
>>
>> I like to use this controller for more than one model. The goal is to
>> generate an admin interface. I don't know which models since they are
>> loaded after map initialisation.
>>
>> So I like to have something like:
>>
>> from pylons import config
>>
>> def init_routes(models):
>> for m in models:
>> config['routes.map'].resource(...)
>>
>> Is it safe ?
>> And if yes, where is the best place to put this stuff ? Ideally I like
>> to have this in the controller's module itself
>
> It should be safe to call map.resource() or map.connect() after the
> map has been initialized, but there are two problems with doing it in
> controllers.
>
That's what I thought. Let's forget this.
> First, it's not easy to tell whether the route has already been set.
> You can iterate through map.matchlist to see if you can find it, but
> beware that the internal structure of Route objects is undocumented
> and subject to change. You can also do something like
> pylons.app_globals.models_seen = set(), and then update the set
> whenever you add a route. You can put the model itself or the connect
> args in the set, anything that will tell you whether you've added this
> route before..
>
> Second, new routes will be added to the end of the matchlist. That
> means if you've set a more general route earlier that matches the URL,
> such as /{controller}/{action}, it will be chosen instead of your
> desired route.
>
Yeah I've forget this... That's a real problem.
> Third and most important, actions may be running simultaneously in
> mulitple threads. map.resource() and map.connect() are not thread
> safe. map.connect() updates several data structures, and
> map.resource() calls map.connect() several times. So you should
> probably use a mutex when calling them in a controller (see Python's
> threading module). You can put the mutex on pylons.app_globals.
>
> It at all possible, it would be better to put the route definitions in
> middleware.py rather than in the controllers That way they'll be set
> up before the threads are started.
>
> Are you sure you really need to add dynamic resource routes? I think
> you can define a resource route with a {model_id} in the path prefix.
> That way you could use the same resource route for all models.
>
Yes, I was thinking about that too. The problem is that I need to take
care of model_id in routes generation (and in methods arguments I
guess) and this will break the current implementation (using the
controller as a standalone resource). But you're right it's probably
the best solution.
I'm just thinking that I can add some **kw to my methods and wrap
url() to see if I have a model_id in routing args.
Thanks for pointing me to the best solution.
Regards,
--
Gael
> --
> Mike Orr <[email protected]>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---