2016-05-13 1 views
6

トラバーサルピラミッドアプリでは、ビューの名前と一致する__name__のリソースをどのように扱いますか?ピラミッドトラバーサル__name__ビュー名に一致

リソースのビュー呼び出し可能なビューにアクセスしたい場合は、/foo/bar/viewのようなURLパスを使用します。それはそうとresource_treeを横断:

RootFactory(request) => RootFactory 
RootFactory['foo'] => Foo 
Foo['bar']   => Bar 
Bar['view']   => KeyError 

...そして、それは過去のバーを通過することはできませんので&「ビュー」が残され、それが「ビューは、」ビュー名であることを前提としていて、私の見解と一致します呼び出し可能

@view_config(name='view') 
def view(context, request): 
    return Response(context.__name__) 

このパスのURLを取得するには、request.resource_url(resource, "view"を使用します)。

しかし、リソースがBar.__name__ = "view"の場合、Fooの「表示」のURLをどのように解決できますか?

# path: /foo/view 
RootFactory(request) => RootFactory 
RootFactory['foo'] => Foo # ideally stop here with view_name="view" 
Foo['view']   => Bar.__name__ = "view" 
# all parts of path matched so view_name="" and context=Bar 

理想的には、このような状況では、/foo/viewview(Foo, request)を指すことになり、そして/foo/view/viewview(Bar, request)Bar.__name__ == "view"を指します。

__name__とビュー名の間の衝突を検出しないでこれを処理する方法はありますか?

答えて

0

解決策は、コンテナリソースの任意のレイヤーを追加することです。以下は

リソースの構成例である:

class Foo(object): 

    def __init__(self, parent, name): 
     self.__parent__ = parent 
     self.__name__ = name 

    def __getitem__(self, key): 
     if key == "bar": 
      return BarContainer(self, "bar") 
     else: 
      raise KeyError() 

class BarContainer(object): 

    def __init__(self, parent, name): 
     self.__parent__ = parent 
     self.__name__ = name 

    def __getitem__(self, key): 
     return Bar(self, key) 

class Bar(object): 

    def __init__(self, parent, name): 
     self.__parent__ = parent 
     self.__name__ = name 

質問は、ビュー呼び出し可能と題しビューへのリソースURLを生成したい言及しています。 BarContainerを使用すると、スラッシュが追加されます。

/{foo}/bar/{bar}/<view_name>  
/a/view   => view(context=Foo(name='a'), request) 
/a/bar/view  => view(context=BarContainer(name='bar'), request) 
/a/bar/b/view => view(context=Bar(name='b'), request) 
関連する問題