本文整理汇总了Python中validator.Validator.callback方法的典型用法代码示例。如果您正苦于以下问题:Python Validator.callback方法的具体用法?Python Validator.callback怎么用?Python Validator.callback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类validator.Validator
的用法示例。
在下文中一共展示了Validator.callback方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: api
# 需要导入模块: from validator import Validator [as 别名]
# 或者: from validator.Validator import callback [as 别名]
def api(method_name):
db = Connection(MONGODB_SERVERS).riverid
api = API(db)
method = getattr(api, method_name, False)
if method == False:
abort(404)
method_parameters = getargspec(method).args
method_parameters.remove('self')
if request.method == 'POST':
request_parameters = request.form.to_dict()
else:
request_parameters = request.args.to_dict()
if 'callback' in request_parameters:
callback = request_parameters['callback']
Validator.callback(callback)
else:
callback = False
if 'lang' in request_parameters:
if request_parameters['lang'] in languages:
languages[request_parameters['lang']].install()
else:
languages['en_ZA'].install()
else:
languages['en_ZA'].install()
if 'cookies' in method_parameters:
request_parameters['cookies'] = request.cookies
for key in method_parameters:
if key not in request_parameters:
abort(400)
unused_parameters = []
for key in request_parameters:
if key not in method_parameters:
unused_parameters.append(key)
for key in unused_parameters:
del request_parameters[key]
result = dict(method=method_name, request=request_parameters)
try:
result['response'] = method(**request_parameters)
result['success'] = True
except RiverException as (error,):
示例2: api
# 需要导入模块: from validator import Validator [as 别名]
# 或者: from validator.Validator import callback [as 别名]
def api(method_name):
db = Connection(MONGODB_SERVERS).riverid
api = API(db)
# Get a reference to the actual method in the API object to call
method = getattr(api, method_name, False)
# If the method could not be returned, it does not exist in the API
if method == False:
abort(404)
# Use reflection to retrieve the list of defined parameters of the method
method_parameters = getargspec(method).args
method_parameters.remove('self')
# Read the given parameters out of the post body if available, otherwise use the querystring
# Use to_dict because every parameter key can only have one associated value
if request.method == 'POST':
request_parameters = request.form.to_dict()
else:
request_parameters = request.args.to_dict()
# Special treatment for the callback parameter as that is used for JSONP
if 'callback' in request_parameters:
callback = request_parameters['callback']
Validator.callback(callback)
else:
callback = False
# Special treatment for the lang parameter as that is used for GNU Gettext
if 'lang' in request_parameters:
if request_parameters['lang'] in languages:
languages[request_parameters['lang']].install()
else:
languages['en_ZA'].install()
else:
languages['en_ZA'].install()
# Pass the cookies object to the method if the method has a parameter called "cookies"
if 'cookies' in method_parameters:
request_parameters['cookies'] = request.cookies
# Check if there are any missing parameters
for key in method_parameters:
if key not in request_parameters:
if key is not "mailfrom" and key is not "mailsubject":
abort(400)
# Remove extra parameters in the request that are not needed by the method
unused_parameters = []
for key in request_parameters:
if key not in method_parameters:
unused_parameters.append(key)
for key in unused_parameters:
del request_parameters[key]
# Start building the object to be serialised and returned to the client
result = dict(method=method_name, request=request_parameters)
# Attempt to execute the API method with the given parameters
try:
result['response'] = method(**request_parameters)
result['success'] = True
except RiverException as (error,):