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:
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
return 'baz-%s' % self.id
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
def __init__(self, request):
and top-level resources contain
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?