def account(): form = UpdateUserForm() if form.validate_on_submit(): if form.picture.data: username = current_user.username pic = add_profile_pic(form.picture.data, username) current_user.profile_image = pic current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('User Account Updated!') return redirect(url_for('users.account')) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email profile_image = url_for('static', filename='profile_pics/' + current_user.profile_image) return render_template('account.html', profile_image=profile_image, form=form)
def account(): form = UpdateUserForm() if form.validate_on_submit(): # Check if there is a picture upload if form.picture.data: username = current_user.username #pass to the add_profile_pic function in picture handler # where the image is saved to the static folder pic = add_profile_pic(form.picture.data, username) #set the image to the profile image attribute in the User current_user.profile_image = pic #set the username and email from the form data current_user.username = form.username.data current_user.email = form.email.data # save to the database db.session.commit() flash('User account updated') return redirect(url_for('core.index')) # First time page is hit get populate with the current data elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email # The profile image will either be the unchanged image if it hasn't been updated # or the updated image saved to the static/profile_pic profile_image = url_for('static', filename='profile_pics/' + current_user.profile_image) return render_template('account.html', form=form, profile_image=profile_image)
def account(): form = UpdateUserForm() if form.validate_on_submit(): if form.picture.data: username = current_user.username pic = add_profile_pic(form.picture.data, username) current_user.profile_image = pic incorrect_user_data = False user_with_email = User.query.filter_by(email=form.email.data).first() user_with_username = User.query.filter_by( username=form.username.data).first() if user_with_email and user_with_email != current_user: incorrect_user_data = True form.email.errors = ['This email is already used.'] if user_with_username and user_with_username != current_user: incorrect_user_data = True form.username.errors = ['This username is already used.'] if incorrect_user_data: profile_image = url_for('static', filename='profile_pics/' + current_user.profile_image) flash('Please correct all errors.', 'danger') return render_template('account.html', profile_image=profile_image, form=form) current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('User account updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email profile_image = url_for('static', filename='profile_pics/' + current_user.profile_image) return render_template('account.html', profile_image=profile_image, form=form)