def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('users.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='Account', image_file=image_file, form=form)
def update_teacher(teacher_id): teacher = Teacher.query.get_or_404(teacher_id) form = UpdateTeacherForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) teacher.image_file = picture_file teacher.name = form.name.data teacher.first_name = form.first_name.data teacher.email = form.email.data db.session.commit() flash('This teacher has been updated!', 'success') return redirect(url_for('intra.teacher', teacher_id=teacher.id)) elif request.method == 'GET': form.name.data = teacher.name form.first_name.data = teacher.first_name form.email.data = teacher.email image_file = url_for('static', filename='profile_pics/' + teacher.image_file) return render_template('intra/update_teacher.html', title='Update teacher', image_file=image_file, form=form, legend='Update teacher')
def account(): # Set form variable to account page form form = UpdateAccountForm() # Only run following code if form response passes checks (in users/forms.py) if form.validate_on_submit(): # Only run following code if new picture uploaded if form.picture.data: delete_picture() # delete old picture picture_file = save_picture( form.picture.data) # upload new picture current_user.image_file = picture_file # update user display flash('Profile picture updated!', 'success') # Remove old user info if exists (and unvalidated) olduser = User.query.filter( User.username.ilike( f'%{form.username.data}%')).first() # check for username in db if olduser and olduser.confirm_account is False: # remove username db.session.delete(olduser) # delete row entry db.session.commit() # save changes oldemail = User.query.filter(User.email.ilike( f'%{form.email.data}%')).first() # check for email in db if oldemail and oldemail.confirm_account is False: # remove email db.session.delete(oldemail) # delete row entry db.session.commit() # save changes # Update username if form.username.data != current_user.username: current_user.username = form.username.data db.session.commit() flash('Username updated!', 'success') # Send email to verify change if form.email.data != current_user.email: current_user.temp_email = form.email.data # update temp email db.session.commit() newemail = User.query.filter_by(temp_email=form.email.data).first() sendemail_emailreset(newemail) # send email to new change flash('Email reset request sent!', 'success') # Save changes db.session.commit() # save changes return redirect(url_for('users.account')) # Return original account page, if no form/changes submitted (i.e. before update) elif request.method == 'GET': form.username.data = current_user.username # display current username form.email.data = current_user.email # display current email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) # display user profile pic return render_template('account.html', title='Account', image_file=image_file, form=form) # key variables for .html