Beispiel #1
0
def regist():
    form = RegistForm(prefix='regist')

    if form.validate_on_submit():

        user_username = User.query.filter_by(
            username=form.username.data.strip()).first()
        user_email = User.query.filter_by(
            email=form.email.data.strip()).first()
        if user_username is not None:
            flash({"error": "用户名已存在!"})
        elif user_email is not None:
            flash({"error": "邮箱已被注册,请更换邮箱注册!"})
        else:
            if form.password.data.strip() != form.password2.data.strip():
                flash({"error": "两次密码不一致,请重新重复密码."})
            else:
                user = User(username=form.username.data.strip(),
                            email=form.email.data.strip(),
                            status=True,
                            role=False)
                user.password = form.password.data.strip()
                db.session.add(user)
                db.session.commit()
                login_user(user=user)
                flash({'success': '欢迎{}注册成功'.format(user.username)})
                # TODO: 多用户登录.待做......
                return redirect(request.args.get('next',
                                                 url_for('auth.login')))
    return render_template('registe.html', form=form)
Beispiel #2
0
def fake_user():
    admin = User(username='******',
                 email='*****@*****.**',
                 is_super_administrator=True)
    admin.password = "******"
    db.session.add(admin)
    db.session.commit()
Beispiel #3
0
def reset_token():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        # hasing New Users password
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        # adding New User to the Datbase
        user.password = hashed_password
        db.session.commit()
        # success is a bootstrap message
        flash('Your password has been Updated! You are now able to log in',
              'success')
        return redirect(url_for('login'))
    return render_template('reset_token.html',
                           title='Reset Password',
                           form=form)
Beispiel #4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User()
        user.username = form.username.data
        user.email = form.email.data
        user.password = hashed_password
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #5
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for(HOME))
    user = User().verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        flash(f'Your password has been updated!', 'success')
        return redirect(url_for(LOGIN))

    return render_template('reset_token.html', title="Reset Password", form=form)
Beispiel #6
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User()
        user.username = form.username.data
        user.email = form.email.data
        user.password = hashed_password
        db.session.add(user)
        db.session.commit()
        flash(f"Your account has been created, you are now able to log in!",
              "success")
        return redirect(
            url_for('login')
        )  # url_for(arg), here arg is function name, not route name.
    return render_template('register.html', title="Register", form=form)
Beispiel #7
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('users.home'))
    user = User.verify_reset_token(token)
    if not user:
        flash('That id an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        user.password = hashed_password
        db.session.commit()
        flash('Your password has been updated! You are now able to log in',
              'success')
        return redirect(url_for('users.login'))
    return render_template('reset_token.html',
                           title='Reset Password',
                           form=form)