validators.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:territoriali-backend 作者: algorithm-ninja 项目源码 文件源码
def validate_id(param, name, getter, required=True):
        """
        Ensure that in the request "param" is present and it's valid. If it is present `getter` is called with the param
        and the return values is sent to the handler as "name". If the getter returns None an error is thrown
        It provides the handler with a `name` argument with the return value of getter.
        """
        def closure(handler):
            def handle(*args, **kwargs):
                if param in kwargs:
                    thing = getter(kwargs[param])
                    if thing is None:
                        BaseHandler.raise_exc(Forbidden, "FORBIDDEN", "No such " + name)
                    del kwargs[param]
                else:
                    thing = None
                kwargs[name] = thing
                return handler(*args, **kwargs)
            HandlerParams.initialize_handler_params(handle, handler)
            HandlerParams.add_handler_param(handle, param, str, required=required)
            # the case when the name of the model corresponds with the param
            if name != param:
                HandlerParams.remove_handler_param(handle, name)
            return handle
        return closure
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号