def _do_create_account(post_vars): """ Given cleaned post variables, create the User and UserProfile objects, as well as the registration for this user. Returns a tuple (User, UserProfile, Registration). Note: this function is also used for creating test users. """ user = User(username=post_vars['username'], email=post_vars['email'], is_active=False) user.set_password(post_vars['password']) registration = Registration() # TODO: Rearrange so that if part of the process fails, the whole process fails. # Right now, we can have e.g. no registration e-mail sent out and a zombie # account try: user.save() except IntegrityError: js = {'success': False} # Figure out the cause of the integrity error if len(User.objects.filter(username=post_vars['username'])) > 0: js['value'] = "An account with the Public Username '" + post_vars[ 'username'] + "' already exists." js['field'] = 'username' return HttpResponse(json.dumps(js)) if len(User.objects.filter(email=post_vars['email'])) > 0: js['value'] = "An account with the Email '" + post_vars[ 'email'] + "' already exists." js['field'] = 'email' return HttpResponse(json.dumps(js)) raise registration.register(user) profile = UserProfile(user=user) profile.name = post_vars['name'] profile.level_of_education = post_vars.get('level_of_education') profile.gender = post_vars.get('gender') profile.mailing_address = post_vars.get('mailing_address') profile.goals = post_vars.get('goals') try: profile.year_of_birth = int(post_vars['year_of_birth']) except (ValueError, KeyError): # If they give us garbage, just ignore it instead # of asking them to put an integer. profile.year_of_birth = None try: profile.save() except Exception: log.exception("UserProfile creation failed for user {0}.".format( user.id)) return (user, profile, registration)
def _do_create_account(post_vars): """ Given cleaned post variables, create the User and UserProfile objects, as well as the registration for this user. Returns a tuple (User, UserProfile, Registration). Note: this function is also used for creating test users. """ user = User(username=post_vars['username'], email=post_vars['email'], is_active=False) user.set_password(post_vars['password']) registration = Registration() # TODO: Rearrange so that if part of the process fails, the whole process fails. # Right now, we can have e.g. no registration e-mail sent out and a zombie # account try: user.save() except IntegrityError: js = {'success': False} # Figure out the cause of the integrity error if len(User.objects.filter(username=post_vars['username'])) > 0: js['value'] = "An account with the Public Username '" + post_vars[ 'username'] + "' already exists." js['field'] = 'username' return HttpResponse(json.dumps(js)) if len(User.objects.filter(email=post_vars['email'])) > 0: js['value'] = "An account with the Email '" + post_vars[ 'email'] + "' already exists." js['field'] = 'email' return HttpResponse(json.dumps(js)) raise registration.register(user) profile = UserProfile(user=user) profile.name = post_vars['name'] profile.level_of_education = post_vars.get('level_of_education') profile.gender = post_vars.get('gender') profile.mailing_address = post_vars.get('mailing_address') profile.goals = post_vars.get('goals') try: profile.year_of_birth = int(post_vars['year_of_birth']) except (ValueError, KeyError): # If they give us garbage, just ignore it instead # of asking them to put an integer. profile.year_of_birth = None try: profile.save() except Exception: log.exception( "UserProfile creation failed for user {0}.".format(user.id)) return (user, profile, registration)
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) _created = True # Track this user register event in oauth if not mobile_client: # do not track api client log 2015.5.26 event_type = ( "weixinapp.user.register_success" if created_on == "weixinapp" else "oauth.user.register_success" ) # track_log(request, event_type, { # 'success': True, # 'uid': user.id, # 'provider': backend.name, # }) user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) return (user, _created)
js['field'] = 'username' return HttpResponse(json.dumps(js)) if len(User.objects.filter(email=post_vars['email'])) > 0: js['value'] = _("An account with the Email '{email}' already exists.").format(email=post_vars['email']) js['field'] = 'email' return HttpResponse(json.dumps(js)) raise registration.register(user) profile = UserProfile(user=user) profile.name = post_vars['name'] profile.level_of_education = post_vars.get('level_of_education') profile.gender = post_vars.get('gender') profile.mailing_address = post_vars.get('mailing_address') profile.goals = post_vars.get('goals') try: profile.year_of_birth = int(post_vars['year_of_birth']) except (ValueError, KeyError): # If they give us garbage, just ignore it instead # of asking them to put an integer. profile.year_of_birth = None try: profile.save() except Exception: log.exception("UserProfile creation failed for user {id}.".format(id=user.id)) return (user, profile, registration)
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) _created = True # Track this user register event in oauth if not mobile_client: # do not track api client log 2015.5.26 event_type = 'weixinapp.user.register_success' if created_on == 'weixinapp' else 'oauth.user.register_success' track_log(request, event_type, { 'success': True, 'uid': user.id, 'provider': backend.name, }) user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) return (user, _created)