Reputation: 2135
Having understood Traversal and the whole resource and context concepts from my question, I tried some tutorial examples that played with the Hybrid routing as stated in the documentation. I kind of understand it if not for some minor problems:
If I were to traverse the following URL: http://example.com/product/123/edit
with the following add_route
configuration:
config = Configurator(settings=**settings, root_factory=RootFactory)
config.add_route('product_edit', '/product/{pid}/edit', factory=ProductFactory, traverse='/{pid}/edit')
config.add_view(route_name='product_edit', name='edit', renderer='edit.mako')
Does it mean that when I supply a product factory to the add_route function, the root resource factory is changed to the product factory (hence product factory is now the new Root resource)?
If the root resource is indeed changed to the ProductFactory for traversal, what would I set the __parent__
& __name__
attributes of the ProductFactory to? Because it looks like the __parent__
will be None
, am I correct?
Here's my ProductFactory code:
class ProductFactory(object):
__name__ = 'product'
__parent__ = None
def __getitem__(self, key):
if key.isnumber():
try:
p = sess_.query(model.Product).filter(pid=key).one()
except:
raise DBException()
if p:
return p
else:
return KeyError
Upvotes: 2
Views: 1555
Reputation: 5303
the "factory" param tells pyramid to use that to determine the context(and indirectly the acl) for that route.
root resources by definition do not have a parent. so parent would indeed be none. Now looking at your code, I'm not sure it would work but this should accomplish what you want.
config = Configurator(settings=**settings, root_factory=RootFactory)
config.add_route('product_edit', '/product/*traverse', factory=ProductFactory)
config.add_view(route_name='product_edit', name='edit', renderer='edit.mako', context=model.Product)
Upvotes: 3