2013-08-28 241 views
0

匹配的觀點我有金字塔的服務和這樣的代碼:獲取金字塔

@view_defaults(route_name="result", context=Result, renderer="json") 
class ResultView(RESTView): 
    """ 
    Rest view for exercise results 
    """ 

    @view_config(request_method='GET', permission='view', role="owner") 
    @view_config(request_method='GET', permission='view', role="admin") 
    @view_config(request_method='GET', permission='view', role="gadmin") 
    @schema(Result) 
    @log("get result") 
    def get(self): 
     """ 
     Gets result 
     """ 

     return self.get.__schema__.dictirialize(self.context, prepare_json=True) 

...... 

ResultView.get裏面?我怎樣才能匹配看法?

+0

你在這裏找什麼?匹配的角色? –

+0

沒有調用函數時匹配的視圖配置對象。我開始意識到我可能需要採取不同的方法。 – offlinehacker

+0

沒有「視圖配置」對象,這感覺就像一個XY問題 –

回答

0

我通過創建一個自定義視圖裝飾器來解決此問題,該裝飾器在request上附加view_id。這個裝飾器根據哪個視圖配置被激活而被調用。這裏有一個例子:

@view_config(request_method='GET', role="owner", permission="view", decorator=view_id("owner")) 
    @view_config(request_method='GET', role="admin", permission="view", decorator=view_id("admin")) 
    @view_config(request_method='GET', permission="view") 
    @schema(User, excludes=('password', 'deleted'), key="owner") 
    @schema(User, key="admin") 
    @log("get user") 
    def get(self): 
     return self.get.__schema__[self.request.id].dictify(self.context) 

而且裝飾:

def view_id(id): 
    """ 
    Pyramid view decorator that remembers the view id and assigns it to request 
    """ 

    def _view_id(view): 
     def view_callable(context, request): 
      request.id = id 
      return view(context, request) 

     return view_callable 

    return _view_id 

它讓我完成工作。