Пример #1
0
def delete(id):
    found_user = User.query.get(id)
    form = DeleteForm(request.form)
    if request.method == b'DELETE':
        if form.validate():
            if bcrypt.check_password_hash(found_user.password,
                                          form.password.data):
                flash({
                    'text':
                    "You have successfully deleted '{}'".format(
                        found_user.username),
                    'status':
                    'danger'
                })
                flash({
                    'text': "Thank you for using our app!",
                    'status': 'success'
                })
                db.session.delete(found_user)
                db.session.commit()
                logout_user()
                return redirect(url_for('root'))
        flash({
            'text': "Wrong password, please try again.",
            'status': 'danger'
        })
    return render_template('users/delete.html',
                           user=User.query.get(id),
                           form=form)
Пример #2
0
def show(id):
    user = User.query.get(id)
    delete_form = DeleteForm(request.form)
    form = UserForm(request.form)
    if request.method == b'PATCH':
        if form.validate():
            user.email = form.data['email']
            user.password = bcrypt.generate_password_hash(
                form.data['password']).decode('UTF-8')
            db.session.add(user)
            db.session.commit()
            flash("You have successfully changed your account details!")
            return redirect(url_for('lists.index', user_id=user.id))
        else:
            flash(
                "Something went wrong in editing your account details. Please try again."
            )
            return render_template('users/edit.html',
                                   user=user,
                                   form=form,
                                   delete_form=delete_form)
    if request.method == b'DELETE':
        if delete_form.validate():
            db.session.delete(user)
            db.session.commit()
            flash("You have successfully deleted your account.")
            logout_user()
            return redirect(url_for('users.index'))
        else:
            flash(
                "Something went wrong in deleting your account. Please try again."
            )
            return redirect(url_for('users.edit', id=user.id))
    return render_template('users/show.html', user=user)
Пример #3
0
def show(id):
    selected_user = User.query.get(id)
    # Use b"PATCH" because Flask Modus makes request.method a bytes literal
    if request.method == b"PATCH":
        user_form = UserForm(request.form)
        if user_form.validate():
            selected_user.first_name = request.form['first_name']
            selected_user.last_name = request.form['last_name']
            selected_user.username = request.form['username']
            selected_user.password = bcrypt.generate_password_hash(
                request.form['password']).decode('UTF-8')
            db.session.add(selected_user)
            db.session.commit()
            flash("User Updated!")
            return redirect(url_for('users.users'))
        else:
            flash("Form Error: User Not Updated")
            return render_template('users/edit.html', user=selected_user, user_form=user_form, delete_form=DeleteForm())
    if request.method == b"DELETE":
        delete_form = DeleteForm(request.form)
        if delete_form.validate():
            db.session.delete(selected_user)
            db.session.commit()
            flash("User Deleted!")
            return redirect(url_for('users.users'))
        else:
            flash("Form Error: User Not Deleted")
            return redirect(url_for('users.users'))
    return render_template('users/show.html', user=selected_user)
Пример #4
0
def edit(id):
    selected_user = User.query.get(id)
    user_form = UserForm(obj=selected_user)
    return render_template('edit.html',
                           user=selected_user,
                           user_form=user_form,
                           delete_form=DeleteForm())
Пример #5
0
def show(id):
    found_user = User.query.get_or_404(id)
    admin_user = User.query.get(current_user.id)

    if request.method == b'PATCH':
        form = UpdateUserForm(request.form)
        if form.validate():
            found_user.username = request.form.get('username')
            found_user.first_name = request.form.get('first_name')
            found_user.last_name = request.form.get('last_name')
            admin_user = User.query.get(current_user.id)
            if admin_user.admin:
                found_user.admin = request.form.get('admin')

            db.session.add(found_user)
            db.session.commit()

            return redirect(url_for('users.index'))

        return render_template('users/edit.html', form=form, user=found_user)
    if request.method == b'DELETE':
        form = DeleteForm(request.form)
        if form.validate():
            db.session.delete(found_user)
            db.session.commit()
            logout_user()
            flash('User Deleted')
            return redirect(url_for('users.login'))
        return render_template('users/edit.html', form=form, user=found_user)

    if found_user.id == current_user.id:
        return render_template('users/show.html', user=found_user, admin=admin_user)
    else:
        flash('Not Authorized')
        return render_template('users/index.html', users=User.query.order_by(User.username).all())
Пример #6
0
def edit(id):
    user = User.query.get(id)
    form = UserForm(obj=user)
    delete_form = DeleteForm()
    return render_template('users/edit.html',
                           user=user,
                           form=form,
                           delete_form=delete_form)
Пример #7
0
def show(id):
    form = UserForm(request.form)
    user = User.query.get(id)
    delete_form = DeleteForm(request.form)
    if request.method == b"PATCH":
        if form.validate():
            user.first_name = request.form.get("first_name")
            user.last_name = request.form.get("last_name")
            db.session.add(user)
            db.session.commit()
            return redirect(url_for("users.index"))
        else:
            return render_template("users/edit.html", user=user, form=form)
    if request.method == b"DELETE":
        if delete_form.validate():
            db.session.delete(user)
            db.session.commit()
            return redirect(url_for("users.index"))
    return render_template("users/show.html",
                           user=user,
                           delete_form=delete_form)
Пример #8
0
def show(id):
    found_user = User.query.get_or_404(id)
    if request.method == b"PATCH":
        form = UserForm(request.form)
        if form.validate():
            found_user.first_name = request.form['first_name']
            found_user.last_name = request.form['last_name']
            time = db.func.now()
            found_user.updated_on = time
            db.session.add(found_user)
            db.session.commit()
            flash('User Updated!')
            return redirect(url_for('users.index'))
        return render_template('/users/edit.html', user=found_user, form=form)
    if request.method == b"DELETE":
        delete_form = DeleteForm(request.form)
        if delete_form.validate():
            db.session.delete(found_user)
            db.session.commit()
            flash('User Deleted!')
            return redirect(url_for('users.index'))
    return render_template('users/show.html', user=found_user)
Пример #9
0
def index():
    delete_form = DeleteForm()
    if request.method == "POST":
        form = UserForm(request.form)
        if form.validate():
            new_user = User(request.form['first_name'],
                            request.form['last_name'])
            db.session.add(new_user)
            db.session.commit()
            flash('User Created!')
            return redirect(url_for('users.index'))
        else:
            return render_template('/users/new.html', form=form)
    return render_template('users/index.html',
                           users=User.query.all(),
                           delete_form=delete_form)