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

4

1 回答 1

0

我通过创建一个附加view_idrequest. 根据激活的视图配置调用此装饰器。这是一个例子:

    @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

它为我完成了工作。

于 2013-09-01T09:03:06.943 回答