Esempio n. 1
0
def oauth_callback(provider):
    target = providers[provider]
    response = target.authorized_response()
    if response is not None:
        access_token = response.get('access_token')
        print('access_token', access_token)
    else:
        access_token = None
    if access_token is None:
        print('access denied')
        return redirect('/')
    r = target.get('user', token=access_token)
    username= r.data.get('login')
    if username:
        u = User.find_by_identity(username)
        if u:
            print(u.username, u.emial)
            login_user(u, remember= True)
            return 'loged in'
        else:
            u = User(username=username)
            u.save()
            login_user(u, remember=True)
            return 'saved and loged in'

    return jsonify(r.data)
Esempio n. 2
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        identity = form.identity.data
        u = User.find_by_identity(identity)
        if u:
            return redirect(url_for('blog.index', user=u.username))
    return render_template('login.html', form=form)
Esempio n. 3
0
def firstuser():
    '''create the first user for test'''
    if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
        print('already  exist initial user')
        return None

    params = {
        'email': app.config['SEED_ADMIN_EMAIL'],
        'password': User.encryptpassword(app.config['SEED_ADMIN_PASSWORD'])
    }

    return User(**params).save()
Esempio n. 4
0
def update_pwd():
    form = update_pwdForm()
    if form.validate_on_submit():
        u = User.find_by_identity(form.identity.data)
        if u and u.passwordmatch(password=form.curent_password.data):
            u.password = User.encryptpassword(form.curent_password.data)
            u.save()
            return redirect(url_for('user.settings'))
        else:
            return '密码或邮箱不正确'

    return render_template('update_pwd.html', form=form)
Esempio n. 5
0
def update_email():
    form = update_emailForm()
    if form.validate_on_submit():
        print('received data')
        u = User.find_by_identity(form.email.data)
        if u and u.passwordmatch(password=form.curent_password.data):
            print('passed validation')
            u.email = form.new_email.data
            u.save()
            return redirect(url_for('user.settings'))
        else:
            return '密码不对'
    return render_template('update_email.html', form=form)
Esempio n. 6
0
def login():
    form = LoginForm(next=request.args.get('next'))
    if form.validate_on_submit():
        next = request.form.get('next')
        identity = form.identity.data
        password = form.password.data
        u = User.find_by_identity(identity)
        if u and u.passwordmatch:
            login_user(u, remember=False)
            track_activity(u, request.remote_addr)
            if next:
                return redirect(urljoin(request.host_url, next))
            else:
                return redirect(url_for('user.settings'))
        else:
            return '密码不正确'

    return render_template('login.html', form=form)
Esempio n. 7
0
def firstuser():
    '''create the first user for test'''
    if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
        print('already  exist initial user')
        return None

    params = {
        'email': app.config['SEED_ADMIN_EMAIL'],
        'password': User.encryptpassword(app.config['SEED_ADMIN_PASSWORD'])
    }
    u= User(**params)
    admin = Role(name= 'Admin')
    db.session.add(admin)
    db.session.add(u)
    admin.users.append(u)
    db.session.commit()

    return None
Esempio n. 8
0
def deserializer(token):
    serializer = TimedJSONWebSignatureSerializer(current_app.config.get('SECRET_KEY'))
    user_email = serializer.loads(token).get('user_email')
    return User.find_by_identity(user_email)
Esempio n. 9
0
 def check(form, field):
     if User.find_by_identity(field.data):
         raise ValidationError(message)
Esempio n. 10
0
def check_email(form, field):
    if not User.find_by_identity(field.data):
        raise ValidationError('邮箱未注册')
Esempio n. 11
0
def index(user):
    u = User.find_by_identity(str(user))
    blogs = u.blogs
    return render_template('blog_index.html', blogs=blogs)