示例#1
0
def register():
    form = RegistrationForm()
    table_name = ""
    if form.validate_on_submit():
        if form.role.data == "1":
            role = Customer()
            table_name = "Customer"
            table = Customer

        elif form.role.data == "2":
            role = Supplier()
            table_name = "Supplier"
            table = Supplier

        elif form.role.data == "3":
            role = Crew()
            table_name = "Crew"
            table = Crew

        hashed_password = bcrypt.generate_password_hash(
            password=form.password.data).decode("utf-8")
        role.username = form.username.data
        role.email = form.email.data
        role.password = hashed_password
        db.session.add(role)
        db.session.commit()
        table_id = table.query.filter_by(email=form.email.data).first().id
        user = User()
        user.table_name = table_name
        user.table_id = table_id
        user.username = form.username.data
        user.email = form.email.data
        db.session.add(user)
        db.session.commit()

        flash('Your account has been created,now you can login in!', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
示例#2
0
def update_password():
    if current_user.table_name == "Customer":
        form = UpdatePasswordForm()
        role = Customer.query.filter_by(
            id=current_user.table_id).first_or_404()
        if form.validate_on_submit():
            hashed_password = bcrypt.generate_password_hash(
                password=form.password.data).decode("utf-8")
            role.password = hashed_password
            role.confirm_password = form.confirm_password.data
            db.session.add(role)
            db.session.commit()

            user = User.query.filter_by(id=current_user.id).first_or_404()
            user.password = hashed_password
            user.confirm_password = form.confirm_password.data
            db.session.add(user)
            db.session.commit()
            flash('你的密码已经更新了', 'success')
            return redirect(url_for("home"))

        return render_template("update_password.html", form=form)

    elif current_user.table_name == "Crew":
        form = UpdatePasswordForm()
        role = Crew.query.filter_by(id=current_user.table_id).first_or_404()

        if form.validate_on_submit():
            hashed_password = bcrypt.generate_password_hash(
                password=form.password.data).decode("utf-8")
            role.password = hashed_password
            role.confirm_password = form.confirm_password.data
            db.session.add(role)
            db.session.commit()

            user = User.query.filter_by(id=current_user.id).first_or_404()
            user.password = hashed_password
            user.confirm_password = form.confirm_password.data
            db.session.add(user)
            db.session.commit()
            flash('你的密码已经更新了', 'success')
            return redirect(url_for("home"))

        return render_template("update_password.html", form=form)

    elif current_user.table_name == "Supplier":
        form = UpdatePasswordForm()
        role = Supplier.query.filter_by(
            id=current_user.table_id).first_or_404()

        if form.validate_on_submit():
            hashed_password = bcrypt.generate_password_hash(
                password=form.password.data).decode("utf-8")
            role.password = hashed_password
            role.confirm_password = form.confirm_password.data
            db.session.add(role)
            db.session.commit()

            user = User.query.filter_by(id=current_user.id).first_or_404()
            user.password = hashed_password
            user.confirm_password = form.confirm_password.data
            db.session.add(user)
            db.session.commit()
            flash('你的密码已经更新了', 'success')
            return redirect(url_for("home"))

        return render_template("update_password.html", form=form)