我有这样的金字塔服务和代码:
@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)
我通过在view_id
上创建附加request
的自定义视图装饰器来解决此问题。根据哪个视图配置被激活来调用此装饰器。这是一个例子:
@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
它为我完成了工作。