2012-03-07 42 views
2

參透遍歷,整個資源和上下文的概念從我question,我試圖與混合路由扮演了文檔中所述的一些實例教程。如果不是我有點明白了一些小問題:金字塔遍歷和URL調度和羅茨別共

如果我要遍歷以下網址:http://example.com/product/123/edit具有以下add_route配置:

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') 
  1. 這是否意味着,當我提供一個產品出廠到add_route功能,根資源工廠變更爲產品出廠(因此產品出廠是現在新的根資源)?

  2. 如果根資源確實改變了ProductFactory的穿越,我會怎麼設置ProductFactory的__parent__ & __name__屬性?因爲它看起來像__parent__None,我是正確的?

這裏是我的ProductFactory代碼:

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 

回答

3

「工廠」參數告訴金字塔用它來確定該路線的情況下(和間接的ACL)。

根定義資源沒有父。所以確實是沒有。現在看你的代碼,我不確定它會工作,但這應該完成你想要的。

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)