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
validators.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录