当前位置: 首页>>代码示例>>Python>>正文


Python Profile.query方法代码示例

本文整理汇总了Python中models.profile.Profile.query方法的典型用法代码示例。如果您正苦于以下问题:Python Profile.query方法的具体用法?Python Profile.query怎么用?Python Profile.query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.profile.Profile的用法示例。


在下文中一共展示了Profile.query方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: getUserId

# 需要导入模块: from models.profile import Profile [as 别名]
# 或者: from models.profile.Profile import query [as 别名]
def getUserId(user, id_type="email"):
    """
    Retrieve User id from a given user object
    :param user: A current user object from API endpoint.
        Example: user = endpoints.get_current_user()
    :param id_type: define return type of this function.
        - email: return user email address
        - oauth: return user information from google+ oauth.
        - custom: return current user id or generate unique id
    :return: email, oauth user id, or user id of the corresponding entity
    """
    if id_type == "email":
        return user.email()

    if id_type == "oauth":
        """A workaround implementation for getting userid."""
        auth = os.getenv('HTTP_AUTHORIZATION')
        bearer, token = auth.split()
        token_type = 'id_token'
        if 'OAUTH_USER_ID' in os.environ:
            token_type = 'access_token'
        url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?%s=%s'
               % (token_type, token))
        user = {}
        wait = 1
        for i in range(3):
            resp = urlfetch.fetch(url)
            if resp.status_code == 200:
                user = json.loads(resp.content)
                break
            elif resp.status_code == 400 and 'invalid_token' in resp.content:
                url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?%s=%s'
                       % ('access_token', token))
            else:
                time.sleep(wait)
                wait = wait + i
        return user.get('user_id', '')

    if id_type == "custom":
        # implement your own user_id creation and getting algorythm
        # this is just a sample that queries datastore for an existing profile
        # and generates an id if profile does not exist for an email
        profile = Profile.query(Profile.mainEmail == user.email())
        if profile:
            return profile.id()
        else:
            return str(uuid.uuid1().get_hex())
开发者ID:SsureyMoon,项目名称:Python-GoogleAppEngine,代码行数:49,代码来源:utils.py


注:本文中的models.profile.Profile.query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。