本文整理汇总了Python中accounts.models.User.get_by_api_key方法的典型用法代码示例。如果您正苦于以下问题:Python User.get_by_api_key方法的具体用法?Python User.get_by_api_key怎么用?Python User.get_by_api_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类accounts.models.User
的用法示例。
在下文中一共展示了User.get_by_api_key方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decorated
# 需要导入模块: from accounts.models import User [as 别名]
# 或者: from accounts.models.User import get_by_api_key [as 别名]
def decorated(*args, **kwargs):
api_key = None
if 'apikey' in request.form:
api_key = request.form.get('apikey')
elif 'X-Api-Key' in request.headers.keys():
api_key = request.headers.get('X-Api-Key')
# validate
if not api_key:
data = {'error': messages.NO_API_KEY}
return generate_api_response(data, 401)
user = User.get_by_api_key(api_key=api_key)
organization = Organization.get_by_api_key(api_key=api_key)
if not user and not organization:
data = {'error': messages.INVALID_API_KEY}
return generate_api_response(data, 401)
requested_org = kwargs.get('organization','')
# check that user is active
if user:
session['user'] = user
# allow admins to see all orgs
if user.is_admin():
session['organization'] = Organization.get_by_name(requested_org)
else:
session['organization'] = Organization.get_by_uuid(user.organization)
if not user.active:
data = {'error': messages.ACCOUNT_INACTIVE}
return generate_api_response(data, 403)
if organization:
session['organization'] = organization
# check that user is authorized for the desired organization
if requested_org and requested_org != session.get('organization').name.lower():
data = {'error': messages.ACCESS_DENIED}
return generate_api_response(data, 403)
return f(*args, **kwargs)