金字塔遍历 HTTP PUT 到不存在的 URI [英] Pyramid traversal HTTP PUT to URI that doesn't exist

查看:44
本文介绍了金字塔遍历 HTTP PUT 到不存在的 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个金字塔遍历应用程序,我希望能够 PUT 到不存在的 URI.有没有办法在视图配置中做到这一点?

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?

例如我有这个

@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

当然 put 不起作用.上下文在 doesnotexist 上抛出一个 keyerror,但在这种情况下如何让遍历器匹配视图?

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?

推荐答案

这听起来像是 Group 对象的单独类,带有 Group 上下文和 UndefinedGroup 上下文.大多数视图都在 Group 上工作,但是你可以有一个特殊的方法来响应对 UndefinedGroup 对象的 PUT 请求.请注意,UndefinedGroup 不应子类化 Group.

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

如果找不到 Group,你的遍历树将负责创建一个 UndefinedGroup 对象.

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

这篇关于金字塔遍历 HTTP PUT 到不存在的 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆