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


Python UserProfile.register_type方法代码示例

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


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

示例1: _get_or_create_oauth_user

# 需要导入模块: from student.models import UserProfile [as 别名]
# 或者: from student.models.UserProfile import register_type [as 别名]
def _get_or_create_oauth_user(strategy, detail, request=None, mobile_client=False, created_on="web"):
    """
    strategy -- strategy obj
    detail -- oauth登录拿到token时的response
    """
    backend = strategy.backend
    _created = False
    uid = get_uid(strategy, detail)
    # weibo新接口uid改名叫做id
    if not uid:
        uid = detail.get("id")
    # weixin
    if backend.name in ("weixin", "weixinapp"):
        weixin_unionid = detail.get("unionid")
        if weixin_unionid:
            weixin_users = UserSocialAuth.objects.filter(weixin_unionid=weixin_unionid).order_by("id")
            weixin_users_count = weixin_users.count()
            # 微信只有一个UserSocialAuth时,使用这个
            if weixin_users_count == 1:
                user = weixin_users[0].user
                user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
                return (user, False)
            elif weixin_users_count > 1:
                # 有web则永远返回第一个web用户
                for each in weixin_users:
                    if each.created_on and each.created_on.startswith("web"):
                        user = each.user
                        user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
                        return (user, False)
                # 否则返回mobile用户
                for each in weixin_users:
                    if each.created_on and each.created_on.startswith("mobile"):
                        user = each.user
                        user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
                        return (user, False)
                # 否则返回weixin app用户(微信服务号活动生成)
                for each in weixin_users:
                    if each.created_on and each.created_on.startswith("app"):
                        user = each.user
                        user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
                        return (user, False)
                # 没有第四种逻辑, 但是还是加上吧
                user = weixin_users[0].user
                user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
                return (user, False)
    if backend.name == "chinamobile":
        extra_data = backend.extra_data(None, uid, detail, {})
        phone_number = extra_data.get("phone_number", None)
        try:
            user_profile = UserProfile.objects.get(phone_number=phone_number)
            user = user_profile.user
            user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
            return (user, False)
        except:
            pass
    result = social_user(strategy, uid)
    # 已有账户,直接登录
    if result["user"]:
        user = result["user"]
    # 否则创建用户,之后登录
    else:
        user = User()
        user.username = str(uuid.uuid4()).replace("-", "")[:20]
        user.email = None
        # oauth的自动激活
        user.is_active = True
        user.set_unusable_password()
        user.save()
        extra_data = backend.extra_data(user, uid, detail, {})
        profile = UserProfile(user=user)
        nickname = get_validate_nickname(extra_data["username"])
        oauth_nickname = nickname
        # 重名加后缀,最多尝试10次
        MAX_TRY_TIMES = 10
        while MAX_TRY_TIMES:
            try:
                UserProfile.objects.get(nickname=nickname)
                suffix = str(uuid.uuid4().int)[:6]
                nickname = "{}{}".format(oauth_nickname, suffix)
                MAX_TRY_TIMES = MAX_TRY_TIMES - 1
            except UserProfile.DoesNotExist:
                break
        profile.phone_number = extra_data.get("phone_number", None)
        profile.nickname = nickname
        profile.unique_code = profile.get_unique_code()
        if request:
            profile.set_register_extra(request=request, cover_data={"channel": backend.name})
        if extra_data.get("profile_image_url"):
            profile.avatar = extra_data["profile_image_url"]
        if extra_data.get("gender"):
            profile.gender = extra_data["gender"]
        if extra_data.get("year_of_birth"):
            profile.year_of_birth = extra_data["year_of_birth"]
        if backend.name == "chinamobile":
            profile.register_type = "migu"
            profile.register_auto = 1
        profile.save()
        # TODO: AuthAlreadyAssociated
        # 此oauth账号之前已经绑定了学堂在线的账号
        new_associate_user(strategy, uid, user, detail, created_on=created_on)
#.........这里部分代码省略.........
开发者ID:blendedlearn,项目名称:blended_learning,代码行数:103,代码来源:views.py


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