Ejemplo n.º 1
0
def manage():
    form = UserInfoForm(data=dict(username=current_user.user.username,
                                  email=current_user.user.email,
                                  description=current_user.user.description))
    if request.method == 'POST':
        form = UserInfoForm(request.form)
        if form.validate():
            current_user.user.username = form.username.data
            current_user.user.description = form.description.data
            current_user.user.save()
            flash(u"用户信息已修改", category='info')
            return redirect(url_for('user_app.manage'))
    return render_template('users/manage.html',
                           form=form)
Ejemplo n.º 2
0
def manage():
    form = UserInfoForm(request.forms)
    if form.validate():
        if request.user:
            request.user.username = form.username.data
            return redirect('/')
        else:
            user = User(email=form.email.data, username=form.username.data,
                        is_email_confirmed=True)
            user.save()
            login.login_user(str(user.id))
            session = get_session(request)
            if 'email' in session:
                del(session['email'])
            return redirect('/')
    return {'form': form, 'token': request.csrf_token}
Ejemplo n.º 3
0
def manage():
    form = UserInfoForm(request.form)
    if form.validate():
        if current_user.is_authenticated:
            current_user.user.username = form.username.data
            return redirect('/')
        else:
            user = User(email=form.email.data, username=form.username.data,
                        is_email_confirmed=True)
            user.save()
            user_mixin = LoginManagerUser(user)
            login_user(user_mixin)
            if 'email' in session:
                del(session['email'])
            return redirect('/')
    return render_template('users/manage.html',
                           form=form)
Ejemplo n.º 4
0
def manage():
    form = UserInfoForm(request.forms)
    if form.validate():
        if request.user:
            request.user.username = form.username.data
            return redirect('/')
        else:
            user = User(email=form.email.data,
                        username=form.username.data,
                        is_email_confirmed=True)
            user.save()
            login.login_user(str(user.id))
            session = get_session(request)
            if 'email' in session:
                del (session['email'])
            return redirect('/')
    return {'form': form, 'token': request.csrf_token}
Ejemplo n.º 5
0
def finish_signup():
    form = UserInfoForm(request.form)
    if form.validate():
        if current_user.is_authenticated:
            current_user.user.username = form.username.data
            return redirect('/')
        else:
            user = User(email=form.email.data,
                        username=form.username.data,
                        is_email_confirmed=True)
            user.save()
            bookmark = Bookmark(user=user,
                                title=u"%s 的收藏夹" % user.username,
                                is_default=True)
            bookmark.save()
            user_mixin = LoginManagerUser(user)
            login_user(user_mixin)
            flash(u"登录成功", category='info')
            if 'email' in session:
                del (session['email'])
            return redirect('/')
    return render_template('users/finish_signup.html', form=form)
Ejemplo n.º 6
0
def finish_signup():
    form = UserInfoForm(request.form)
    if form.validate():
        if current_user.is_authenticated:
            current_user.user.username = form.username.data
            return redirect('/')
        else:
            user = User(email=form.email.data, username=form.username.data,
                        is_email_confirmed=True)
            user.save()
            bookmark = Bookmark(user=user,
                                title=u"%s 的收藏夹" % user.username,
                                is_default=True)
            bookmark.save()
            user_mixin = LoginManagerUser(user)
            login_user(user_mixin)
            flash(u"登录成功", category='info')
            if 'email' in session:
                del (session['email'])
            return redirect('/')
    return render_template('users/finish_signup.html',
                           form=form)
Ejemplo n.º 7
0
def oauth_callback(provider):
    current_app.logger.info("Oauth callback for %s" % provider)
    redirect_uri = current_app.config['OAUTH'][provider]['callback_url']
    oauth_service = get_oauth_services()[provider]

    data = dict(code=request.args.get('code'),
                grant_type='authorization_code',
                redirect_uri=redirect_uri)

    if provider == 'google':
        oauth_session = oauth_service.get_auth_session(data=data,
                                                       decoder=json.loads)
        user_info = oauth_session.get('userinfo').json()
        email = session['email'] = user_info['email']
        username = user_info['given_name']
    elif provider == 'github':
        oauth_session = oauth_service.get_auth_session(data=data)
        user_info = oauth_session.get('user').json()
        email = session['email'] = user_info['email']
        username = user_info['login']

    access_token = oauth_session.access_token
    user_info['id'] = str(user_info['id'])

    current_app.logger.info("%s oauth access token is: %s" %
                            (provider, access_token))
    current_app.logger.info("%s oauth user info is %s" % (provider, user_info))

    user = User.find_by_oauth(provider, user_info['id'])
    if user:
        # TODO: 直接登录时更新 token.
        user_mixin = LoginManagerUser(user)
        login_user(user_mixin)
        flash(u"登录成功", category='info')
        return redirect('/')
    else:
        user = User.objects(email=email).first()
        if user:
            user_oauth = UserOauth(provider=provider,
                                   openid=user_info['id'],
                                   token=access_token)
            user_oauth.save()
            user_mixin = LoginManagerUser(user)
            login_user(user_mixin)
            flash(u"登录成功", category='info')
            return redirect('/')
        else:
            return render_template('users/finish_signup.html',
                                   form=UserInfoForm(email=email,
                                                     username=username))
Ejemplo n.º 8
0
def oauth_callback(provider):
    logger.info("Oauth callback for %s" % provider)
    redirect_uri = app.config['oauth.%s.callback_url' % provider]
    oauth_service = oauth_services[provider]
    session = get_session(request)

    data = dict(code=request.params.get('code'),
                grant_type='authorization_code',
                redirect_uri=redirect_uri)

    if provider == 'google':
        oauth_session = oauth_service.get_auth_session(data=data,
                                                       decoder=json.loads)
        user_info = oauth_session.get('userinfo').json()
        email = session['email'] = user_info['email']
        username = user_info['given_name']
    elif provider == 'github':
        oauth_session = oauth_service.get_auth_session(data=data)
        user_info = oauth_session.get('user').json()
        email = session['email'] = user_info['email']
        username = user_info['login']

    access_token = oauth_session.access_token
    user_info['id'] = str(user_info['id'])

    logger.info("%s oauth access token is: %s" % (provider, access_token))
    logger.info("%s oauth user info is %s" % (provider, user_info))

    user = User.find_by_oauth(provider, user_info['id'])
    if user:
        # TODO: 直接登录时更新 token.
        login.login_user(str(user.id))
        return redirect('/')
    else:
        user = User.objects(email=email).first()
        if user:
            user_oauth = UserOauth(provider=provider,
                                   openid=user_info['id'],
                                   token=access_token)
            user_oauth.save()
            login.login_user(str(user.id))
            return redirect('/')
        else:
            return {
                'form': UserInfoForm(email=email, username=username),
                'token': request.csrf_token
            }