def edit_profile(): """Update profile for current user.""" if not g.user: flash("Access unauthorized.", "danger") return redirect("/") form = UserEditForm(obj=g.user) if form.validate_on_submit(): if User.authenticate(g.user.username, form.static_password.data): form.populate_obj(g.user) try: db.session.commit() return redirect(f"/users/{g.user.id}") except IntegrityError: db.session.rollback() flash("That username or email is already taken") return render_template('users/edit.html', form=form, user_id=g.user.id) flash("Wrong password, please try again.", 'danger') return render_template('users/edit.html', form=form, user_id=g.user.id)
def edit_profile(): """Update profile for current user.""" if not g.user: flash("Access unauthorized.", "danger") return redirect("/") form = UserEditForm() if form.validate_on_submit(): if not User.authenticate(g.user.username, form.password.data): flash('Password Incorrect Try Again', 'warning') return redirect(url_for('homepage')) form = user_form_defaults_logic(form) form.populate_obj(g.user) try: db.session.add(g.user) db.session.commit() except IntegrityError as e: db.session.rollback() flash(e.message) return redirect(url_for('edit_profile')) flash('Profile Updated', 'success') return redirect(url_for('users_show', user_id=g.user.id)) form = populate_user_edit_form(form, g.user) return render_template('users/edit.html', form=form)
def update_account(): user = app.current_user update_form = UserEditForm(request.POST, user) if update_form.validate(): # user.update(**update_form.data).execute() update_form.populate_obj(user) print(update_form.data) user.save() print(user.picture) app.flash(u'Дані успішно оновлено') else: app.flash(u'Incorrect somtethisd') redirect('/account') # without return redirect because of raise inside
def user_edit(id): user = User.query.filter(User.id==id).first() form = UserEditForm(request.form, user) form.team_id.choices = [(1, u'管理'), (2, u'运维监控'), (3, u'信号采集'), (4, u'网络存储'), \ (5, u'机房运维'), (6, u'直播流运营')] form.team_id.choices.insert(0, (0, u'- 指定团队 -')) if form.validate_on_submit(): form.populate_obj(user) if form.password.data != '': user.set_password(form.password.data) db.session.commit() return redirect(url_for('user_list')) return render_template('user_edit.html', form=form)
def admin_edit_user(user_id): """ edit user profile, allow making user admin """ if not g.user.admin: flash("You're not an admin!", "danger") return redirect('/') user_to_edit = User.query.get_or_404(user_id) form = UserEditForm(obj=user_to_edit) if form.validate_on_submit(): form.populate_obj(user_to_edit) db.session.commit() return redirect(url_for('admin_show_user', user_id=user_to_edit.id)) return render_template('admin/edit_user.html', user=user_to_edit, form=form)
def user_edit(id): user = User.query.filter(User.id == id).first() form = UserEditForm(request.form, user) form.team_id.choices = [(1, u'管理'), (2, u'运维监控'), (3, u'信号采集'), (4, u'网络存储'), \ (5, u'机房运维'), (6, u'直播流运营')] form.team_id.choices.insert(0, (0, u'- 指定团队 -')) if form.validate_on_submit(): form.populate_obj(user) if form.password.data != '': user.set_password(form.password.data) db.session.commit() return redirect(url_for('user_list')) return render_template('user_edit.html', form=form)
def user_edit(id): user = User.query.get(id) form = UserEditForm(obj=user) if request.method == 'POST': try: form = UserEditForm(request.form, obj=user) if form.validate(): form.populate_obj(user) db.session.commit() flash("Notanda var breytt", category='success') except Exception as error: app.logger.error('Error updating a user : {}\n{}'.format( error, traceback.format_exc())) return render_template('forms/model.jinja', user=user, form=form, type='edit', action=url_for('user_edit', id=id), section='user')
def profile(): """Update profile for current user. When username or email taken, renders template again with errors shown. When password incorrect, also show errors on re -rendered template Otherwise, successfully edits user profile and redirects to user detail page """ form = UserEditForm(obj=g.user) searched_username_user = User.query \ .filter_by(username=form.username.data).first() if searched_username_user and searched_username_user is not g.user: form.username.errors = ["Username is taken."] searched_email_user = User.query.filter_by(email=form.email.data).first() if searched_email_user and searched_email_user is not g.user: form.email.errors = ["Email is taken."] if form.email.errors or form.username.errors: form.password.data = None return render_template("users/edit.html", form=form) if form.validate_on_submit(): if not User.authenticate(username=g.user.username, password=form.password.data): form.password.data = None form.password.errors = ["Password is incorrect."] return render_template("users/edit.html", form=form) form.password.data = g.user.password form.populate_obj(g.user) db.session.commit() flash(f"Successfully updated: {g.user.username}", "success") return redirect(url_for("show_user", user_id=g.user.id)) else: form.password.data = None return render_template('users/edit.html', form=form)