def validate_email(self, email): count = 0 users = db.get_collection("users").find({"email": email.data}) for user in users: count = count + 1 if count > 0: raise ValidationError( 'That email is taken. Please choose a different email.')
def validate_username(self, username): count = 0 users = db.get_collection("users").find({"username": username.data}) for user in users: count = count + 1 if count > 0: raise ValidationError( 'That username is taken. Please choose a different username.')
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') userDict = { "username": form.username.data, "email": form.email.data, "password": hashed_password } db.get_collection("users").insert_one(userDict) flash('Your account has been created! You are now able to log in.', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('home')) form = LoginForm() if form.validate_on_submit(): user = db.get_collection("users").find_one( {"username": form.username.data}) if user and bcrypt.check_password_hash(user['password'], form.password.data): login_user(User(user), remember=form.remember.data) next_page = request.args.get('next') return redirect(next_page) if next_page else redirect( url_for('home')) else: flash('Login Unsuccessful. Please check username and password.', 'danger') return render_template('login.html', title='Login', form=form)
def home(): page = request.args.get('page', 1, type=int) posts = db.get_collection("posts").find() return render_template('home.html', posts=posts)