def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): # bcrypt.generate_password_hash(form.password.data) - returns bytes # bcrypt.generate_password_hash(form.password.data).decode('utf-8') - returns string hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(email=form.email.data, username=form.username.data, password=hashed_password) # TODO delete user.active = True db.session.add(user) db.session.commit() profile = Profile(user_id=user.id, user=user) db.session.add(profile) db.session.commit() user.send_verification_email() # 'success' is the name of the BootStrap class for message. flash(f'A confirmation email has been sent to {form.email.data}', 'success') return redirect(url_for('users.login')) return render_template('users/register.html', title='Register', form=form)
def test_user1_following_user2(self): hashed_password = bcrypt.generate_password_hash( self.TEST_USER_PASSWORD).decode('utf-8') self.user1 = User(email='*****@*****.**', username='******', password=hashed_password) self.user2 = User(email='*****@*****.**', username='******', password=hashed_password) self.user1.active = True self.user2.active = True db.session.add(self.user1) db.session.add(self.user2) db.session.commit() self.profile1 = Profile(user_id=self.user1.id, user=self.user1) self.profile2 = Profile(user_id=self.user2.id, user=self.user2) db.session.add(self.profile1) db.session.add(self.profile2) db.session.commit() self.profile1.follow(self.profile2) self.assertIn(self.profile2, self.profile1.followed.all()) self.assertIn(self.profile1, self.profile2.followers.all()) self.assertNotIn(self.profile2, self.profile1.followers.all()) self.assertNotIn(self.profile1, self.profile2.followed.all()) self.assertTrue(self.profile1.is_following(self.profile2)) self.assertFalse(self.profile2.is_following(self.profile1)) self.assertTrue(self.profile2.is_followed_by(self.profile1)) self.assertFalse(self.profile1.is_followed_by(self.profile2))
def setUp(self): """ Will be called before every test """ db.create_all() self.user1 = User( email='*****@*****.**', username='******', password=self.TEST_HASHED_PASSWORD, ) self.user2 = User( email='*****@*****.**', username='******', password=self.TEST_HASHED_PASSWORD, ) self.user1.active = True self.user2.active = True db.session.add(self.user1) db.session.add(self.user2) db.session.commit() self.profile1 = Profile(user_id=self.user1.id, user=self.user1) self.profile2 = Profile(user_id=self.user2.id, user=self.user2) db.session.add(self.profile1) db.session.add(self.profile2) db.session.commit() self.post1 = Post( author_id=self.profile1.id, content='Post1', location='Tomaszów Mazowiecki', ) self.post2 = Post( author_id=self.profile2.id, content='Post2', location='Tomaszów Mazowiecki', ) db.session.add(self.post1) db.session.add(self.post2) db.session.commit()
def setUp(self): """ Will be called before every test """ db.create_all() user = User( email=self.TEST_USER_EMAIL, username=self.TEST_USER_USERNAME, password=self.TEST_HASHED_PASSWORD, ) user.active = True db.session.add(user) db.session.commit() profile = Profile(user_id=user.id, user=user) db.session.add(profile) db.session.commit()
def confirm_email(token): user = User.verify_token(token, current_app.config['SECURITY_VERIFY_EMAIL_SALT']) if user is None: flash('That is an invalid or expired token', 'warning') elif user.active: flash('Account already confirmed. Please login.', 'success') else: user.active = True db.session.add(user) db.session.commit() flash('You have confirmed your account. You can log in now.', 'success') return redirect(url_for('users.login'))
def signup(): 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('Account created for %s!' % form.username.data, 'success') return redirect(url_for('login')) return render_template('signup.html', title='Sign Up', form=form)
def reset_password_token(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_token( token, current_app.config['SECURITY_RESET_PASSWORD_SALT']) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('users.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your password has been updated! You are now able to log in.', 'success') return redirect(url_for('users.login')) return render_template('users/reset_password_token.html', title='Set New Password', form=form)