def register(): # user already logged in if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): # hash the password hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password, is_male=form.is_male.data, type_user=form.type_user, description=form.description.data) # add to database db.session.add(user) db.session.commit() flash(f'Account created for {form.username.data}!', 'success') # redirect user to login page return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def accountupdate(): 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 current_user.about_me = form.about_me.data if form.old_pass.data: if bcrypt.check_password_hash(current_user.password, form.old_pass.data): hashed_password = bcrypt.generate_password_hash( form.new_pass.data).decode('utf-8') current_user.password = hashed_password elif bcrypt.check_password_hash(current_user.password, form.old_pass.data) == False: flash('Old password is wrong!', 'danger') return redirect('account') db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email form.about_me.data = current_user.about_me image_file = url_for('static', filename='images/profile_pics/' + current_user.image_file) return render_template('_account-update.html', title='Account', image_file=image_file, form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') user = User(username = form.username.data,email = form.email.data,password = hashed_password) db.session.add(user) db.session.commit() flash(f'Your account has been created! Now You Can Login!','success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def user_create_admin(): form = AdminUserCreateForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) user.role = form.role.data db.session.add(user) db.session.commit() flash('User has been created!', 'success') return redirect(url_for('users_list_admin')) if form.errors: flash(form.errors, 'danger') return render_template('user-create-admin.html', title='Create User', form=form)