Hi list

This message is about using pyramid, rather than about its development. I hope 
that's okay.

I'm new to the traversal mechanism for managing resources and views. I really 
like the idea of a resource tree. I thought I'd write a quick note on how I'm 
using it in the hope of drawing comments and comparisons. Particularly, I'll 
describe what I've done to make any instance of a tree resource location-aware, 
even if it wasn't reached by traversal.

Using traversal with a resource tree that looks like /foo-1/bar-2/baz-3 I find 
it very convenient to build URLs using the request.resource_url interface. I 
often want to build lists of URLs like this:

for baz in bar_instance.bazzes:
        request.resource_url(baz)

I don't want to say request.resource_url(bar_instance, 'baz-%s' % baz.id) 
because I don't want to spread around responsibility for representing the 
resource tree and because, in the real case, the required call to resource_url 
would be even uglier.

For all of my tree resources there is a natural parent (and it makes sense to 
assume that if they exist then they can be located on the resource graph; I 
never have to deal with resources that are disconnected in principle) so I can 
say, for example

class Baz:
        @property
        def __name__(self):
                return 'baz-%s' % self.id
        
        @property
        def __parent__(self):
                return self.bar

which neatly enforces the structure of the resource graph in general. The 
trouble is with the first layer of children in the tree. They need to point at 
the application root.

If I have an instance of one of these first layer children that wasn't loaded 
by traversal, then the only way I can think of to get at the application root 
directly is via pyramid.threadlocal.get_current_request. That would make my 
domain models dependent on pyramid which doesn't seem very natural. 
Conceptually, I suppose the resources should sort out their own tree root 
anyway, rather than trying to look somewhere else for it. And so my resource 
package grew two new functions, set_tree_root and get_tree_root.

The application root factory contains

class AppRoot:
        def __init__(self, request):
                set_tree_root(self)

and top-level resources contain

class Foo:
        @property
        def __parent__(self):
                return get_tree_root()

Behind the scenes, those two functions modify and access a thread-local 
variable.

Does this sound broadly reasonable? Am I going to run into problems later? How 
do you usually implement and use the resource tree?

Regards
Norman

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to