Pyramid traversal HTTP PUT to URI that doesn't exist

六月ゝ 毕业季﹏ 提交于 2019-12-11 04:48:51

问题


So I have a pyramid traversal app and I'd like to be able to PUT to URIs that don't exist. Is there a way to do this in the view config?

So for example I have this

@view_defaults(context=models.Groups, renderer='json')
@view_config(request_method='GET')
class GroupsView(object):

    def __call__(self):
        ''' This URI corresponds to GET /groups '''
        pass

    @view_config(request_method='PUT')
    def put(self):
        ''' This URI should correspond to PUT /groups/doesnotexist '''
        pass

Of course the put doesn't work. The context throws a keyerror on doesnotexist, but how do I get the traverser to match a view in this case?


回答1:


This sounds like a separate class for Group objects with a Group context and an UndefinedGroup context. Most views work on Group, but you could have a special method responding to PUT requests for UndefinedGroup objects. Note that UndefinedGroup should not subclass Group.

@view_defaults(context=Group, renderer='json')
class GroupView(object):
    def __init__(self, request):
        self.request = request

    @view_config(request_method='GET')
    def get(self):
        # return information about the group

    @view_config(context=UndefinedGroup, request_method='PUT')
    def put_new(self):
        # create a Group from the UndefinedGroup

    @view_config(request_method='PUT')
    def put_overwrite(self):
        # overwrite the old group with a new one

Your traversal tree would then be responsible for creating an UndefinedGroup object if it cannot find a Group.




回答2:


My view class doesn't raise the error with PUT or GET method (pyramid version: 1.4.1):

@view_defaults(context='.Group', renderer='json')
@view_config(request_method='GET')
class GroupView(object):
    def __init__(self, context, request):
        self.context = context
        self.request = request

    def __call__(self):
        return {'method':'GET'}

    @view_config(request_method='POST')
    def post(self):
        return {'method':'POST'}

    @view_config(request_method='PUT')
    def put(self):
        return {'method':'PUT'}

    @view_config(request_method='DELETE')
    def delete(self):
        return {'method':'DELETE'}

Maybe the __parent__ and the __name__ attributes has problem. Btw I prefer use Configurator method to bind view functions:

config.add_view('app.views.GroupView', context='app.resources.Group',
                 request_method='GET', renderer='json') # attr=__call__
config.add_view('app.views.GroupView', context='app.resources.Group',
                 attr='put', request_method='PUT', renderer='json')

Use imperative configuration, no extra class but arguments. You can also combine view_defaults decorator and Configurator.add_view() method.



来源:https://stackoverflow.com/questions/14739993/pyramid-traversal-http-put-to-uri-that-doesnt-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!