def put(self, request, format=None): post_vars = request.DATA.copy() email = post_vars.get('email') phone_number = post_vars.get('phone') validate = post_vars.get('validate') user = request.user if email: if not user.email: raise error.Error(error.EMAIL_NOT_BIND, u'未绑定邮箱') check_email(post_vars) post_vars.setdefault('new_email', email) response = _change_email_request(request.user, post_vars, need_password=False) result = json.loads(response.content) success = result.get('success') if not success: raise error.Error(error.EMAIL_CHANGE_FAILED, result.get('error')) profile = user.profile profile = self.set_common_profile(request, profile) if phone_number: if not profile.phone_number: raise error.Error(error.PHONE_NOT_BIND, u'未绑定手机号') check_phone_number(post_vars) check_validate_used(post_vars) profile.phone_number = phone_number try: profile.save() except IntegrityError: raise error.Error(error.PHONE_NUMBER_EXIST, u'手机号已存在') return Response(get_user_info(request.user), status=status.HTTP_202_ACCEPTED)
def post(self, request): post_vars = request.DATA check_phone_number(post_vars) check_validate_used(post_vars) user = request.user phone_number = post_vars.get('phone') password = post_vars.get('password') if password: if user.password != '!': server_track(request, 'api.user.phone.bind_failure', { 'bind_type': 'phone', 'provider': '', 'uid': request.user.id, 'error': { 'msg': 'PASSWORD_ALREADY_BIND', 'detail': phone_number, } }) raise error.Error(error.PASSWORD_ALREADY_BIND, u'之前已绑定密码') user.set_password(password) profile = user.profile if profile.phone_number: server_track(request, 'api.user.phone.bind_failure', { 'bind_type': 'phone', 'provider': '', 'uid': request.user.id, 'error': { 'msg': 'PASSWORD_ALREADY_BIND', 'detail': phone_number, } }) raise error.Error(error.PASSWORD_ALREADY_BIND, u'该手机已被注册或绑定') check_validate_used(post_vars) profile.phone_number = phone_number try: profile.save() except IntegrityError: server_track(request, 'api.user.phone.bind_failure', { 'bind_type': 'phone', 'provider': '', 'uid': request.user.id, 'error': { 'msg': 'PHONE_NUMBER_EXIST', 'detail': phone_number, } }) raise error.Error(error.PHONE_NUMBER_EXIST, u'该手机已被注册或绑定') user.save() server_track(request, 'api.user.phone.bind_success', { 'bind_type': 'phone', 'provider': '', 'uid': user.id, 'phone_number': phone_number }) return Response(status=status.HTTP_204_NO_CONTENT)
def create_user_with_phone(self, post_vars, request=None): ''' use email password register return user, profile, registration ''' # Confirm we have a properly formed request self.check_common_params(post_vars) check_phone_number(post_vars) check_validate_used(post_vars) if not post_vars.get('name'): post_vars['name'] = '' post_vars.setdefault('phone_number', post_vars['phone']) return _register(post_vars, use_phone_number=True, request=request)
def reset_password_with_phone(self, request): ''' 只有手机验证验证码成功后才能调用此接口 ''' post_vars = request.DATA check_phone_number(post_vars) if 'password' not in post_vars: raise error.Error(error.MISSING_PARAMETER, u'缺少参数password') check_validate_used(post_vars) phone_number = post_vars['phone'] password = post_vars['password'] try: profile = UserProfile.objects.get(phone_number=phone_number) user = profile.user user.set_password(password) user.save() except UserProfile.DoesNotExist: raise error.Error(error.PHONE_NUMBER_DONT_EXIST, u'手机号码不存在')