示例#1
0
def callback(sitename):
    """
    OAuth认证
    :param sitename:
    :return:
    """
    if sitename in ['weibo_app', 'qq_app', 'facebook_app']:
        site, msg = parse_token_response(sitename, request.args)
        app = 'IOS'
    else:
        code = request.args.get('code')
        site, msg = get_oauth_token(sitename, code)
        app = sitename != 'wechat_app' and 'MOBILEWEB' or 'IOS'

    if site is None:
        return jsonify(message='Failed', error=msg)

    if sitename in ['wechat', 'wechat_app']:
        oauth = SocialOAuth.objects(unionid=site.unionid).first()
    else:
        oauth = SocialOAuth.objects(site_uid=site.uid).first()

    if not oauth:
        oauth = SocialOAuth.create(site.site_name,
                                   site.uid,
                                   site.name,
                                   site.access_token,
                                   site.expires_in,
                                   site.refresh_token,
                                   app=app,
                                   unionid=getattr(site, 'unionid', None),
                                   gender=site.gender)
        path = 'avatar/{}/{}.jpeg'.format(oauth.user.id,
                                          str(time.time()).replace('.', ''))
        jobs.image.save_avatar('maybe-img',
                               path,
                               url=site.avatar_large,
                               save_original=True)
        url = 'http://assets.maybe.cn/%s' % path
        oauth.update_avatar(url)
        user_id = str(oauth.user.id)
        login_user(oauth.user, remember=True)
        return jsonify(message='OK', login=False, user_id=user_id)
    else:
        oauth.re_auth(site.access_token, site.expires_in, site.refresh_token,
                      getattr(site, 'unionid', None))
        if oauth.user.account.is_email_verified:
            login_user(oauth.user, remember=True)
            return jsonify(message='OK',
                           login=True,
                           remember_token=oauth.user.generate_auth_token(),
                           user=json_templ.get_user_info(oauth.user))

        else:
            user_id = str(oauth.user.id)
            return jsonify(message='OK', login=False, user_id=user_id)
示例#2
0
def user_info():
    """

    :return:
    """
    if not current_user.is_authenticated:
        return jsonify(message='Failed', logged_in=False)

    info = json_templ.get_user_info(current_user)
    return jsonify(message='OK', logged_in=True, user=info)
示例#3
0
def update_avatar():
    """
    上传头像
    :return:
    """
    path = request.json.get('avatar_url')
    if path:
        url = 'http://assets.maybe.cn/%s' % path
        jobs.image.make_thumbnails('maybe-img', path, url)

        user = current_user._get_current_object()
        user.avatar_url = url
        user.save()
    return jsonify(message='OK', user=json_templ.get_user_info(user))
示例#4
0
def login_with_token():
    """

    :return:
    """
    data = request.json
    token = data.get('token', '')
    user = User.verify_auth_token(token)
    if not user:
        return jsonify(message='Failed')
    login_user(user, remember=True)
    return jsonify(message='OK',
                   user=json_templ.get_user_info(user),
                   remember_token=user.generate_auth_token())
示例#5
0
def update_username():
    """
    更新用户昵称
    :return:
    """
    username = request.json.get('username')
    if username:
        if len(username) > 16:
            return jsonify(message='Failed',
                           error=gettext('username is too long'))
        user = current_user._get_current_object()
        user.name = username
        user.save()
        return jsonify(message='OK', user=json_templ.get_user_info(user))
    return jsonify(message='Failed', error='参数不对')
示例#6
0
def login_email():
    """

    :return:
    """
    data = request.json
    email = data.get('email', '')
    user, authenticated = User.authenticate(email=email,
                                            password=data.get('password', ''))
    if not authenticated:
        return jsonify(message='Failed')
    login_user(user, remember=True)
    return jsonify(message='OK',
                   user=json_templ.get_user_info(user),
                   remember_token=user.generate_auth_token())
示例#7
0
def bind_email():
    """

    :return:
    """
    email = request.json.get('email')
    user_id = request.json.get('user_id')
    if not email:
        return jsonify(message='Failed', error=gettext('no email'))
    if User.objects(account__email=email):
        return jsonify(message='Failed',
                       error=gettext('the email alreadly exists'))
    user = User.objects(id=user_id).first()
    user.account.email = email
    user.account.is_email_verified = True
    user.save()
    login_user(user, remember=True)
    return jsonify(message='OK',
                   user=json_templ.get_user_info(user),
                   remember_token=user.generate_auth_token())
示例#8
0
def email_signup():
    """
    邮箱注册
    :return:
    """
    data = request.json
    name = data.get('name')
    email = data.get('email', '')
    password = data.get('password', '')
    if not password:
        return jsonify(message='Failed', error=gettext('please fill in.'))
    if User.objects(account__email=email):
        return jsonify(message='Failed',
                       error=gettext('this name has been registered.'))
    if not name:
        name = 'maybe' + str(time.time()).replace('.', '')
    user = User.create(name=name, email=email, password=password)
    login_user(user, remember=True)
    return jsonify(message='OK',
                   user=json_templ.get_user_info(user),
                   remember_token=user.generate_auth_token())