Ejemplo n.º 1
0
def main():
    """This method is used to load all of the book data
    initially from the data file."""

    # Load all of the data into the database
    f = open("project1/books.csv")
    reader = csv.reader(f)

    # Cycle through the rest of the file and load data into DB
    for id, title, author, year in reader:
        #print(f"{id},  {title},  {author},  {year}")
        book = Book(isbn=id,
                    title=title,
                    author=author,
                    year=year,
                    count_ratings=0,
                    overall_rating=0)
        db.session.add(book)

    temp_pass = bcrypt.generate_password_hash("password").decode('utf-8')

    ## Add some users
    user1 = User(name="Jaroke", password=temp_pass)
    user2 = User(name="BenW", password=temp_pass)
    user3 = User(name="EricKatz", password=temp_pass)
    # Add users to the database
    db.session.add(user1)
    db.session.add(user2)
    db.session.add(user3)

    # Add some reviews
    r1 = Review(book_id=1,
                user_id=1,
                username=user1.name,
                bookname="Krondor: The Betrayal",
                review="Good Book",
                rating=4.5)
    r2 = Review(book_id=1,
                user_id=2,
                username=user2.name,
                bookname="Krondor: The Betrayal",
                review="OK book",
                rating=3)
    r3 = Review(book_id=2,
                user_id=1,
                username=user1.name,
                bookname="The Dark Is Rising",
                review="Not good",
                rating=2)
    # Add reviews to the database
    db.session.add(r1)
    db.session.add(r2)
    db.session.add(r3)

    #Commit Changes
    db.session.commit()
Ejemplo n.º 2
0
def signup():
    username=request.form.get("username")
    password=bcrypt.generate_password_hash(request.form.get("password")).decode('utf-8')
    # create new user with the form data. Hash the password so plaintext version isn't saved.
    new_user = User(username=username, password=password)

    # add the new user to the database
    try:
        db.session.add(new_user)
        db.session.commit()
        return redirect(url_for('index'))
    except IntegrityError:
        return redirect(url_for('register'))
Ejemplo n.º 3
0
def reset_password(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash("That is an invalid or expired token", 'warning')
        return redirect(url_for('users.request_reset'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = password
        db.session.commit()
        flash(f'Your password has been updated!', 'success')
        return redirect(url_for('users.login'))
    return render_template("pwd_reset.html", form=form)
Ejemplo n.º 4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('user'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(email=form.email.data,
                    first_name=form.first_name.data,
                    last_name=form.last_name.data,
                    passwrd=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Your account has been created!', ' success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Ejemplo n.º 5
0
def register():

    if current_user.is_authenticated:
        return redirect(url_for('main.index'))

    form = RegistrationForm()
    if form.validate_on_submit():
        password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=password)
        db.session.add(user)
        db.session.commit()
        flash(f'account created for {form.username.data}!', 'success')
        return redirect(url_for('users.login'))
    return render_template("signup.html", form=form)
Ejemplo n.º 6
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        name = form.name.data
        email = form.email.data
        password = form.password.data
        password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
        user = User(name = name , email = email , password = password_hash)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in', 'success')
        return redirect(url_for('login'))
    else:
        print('wrong')
    return render_template('registration.html',form = form)
Ejemplo n.º 7
0
def register():
    """Used to register a new user"""

    if current_user.is_authenticated:
        redirect(url_for('index'))

    # Create the form
    form = RegistrationForm()
    # Validate the form
    if form.validate_on_submit():
        # Grab the username the user entered
        username = request.form.get("username")
        # Query database for username
        check_name = User.query.filter_by(name=username).first()
        # Check if username is taken
        if check_name is None:
            # Grab the password and create the user
            pass1 = bcrypt.generate_password_hash(
                request.form.get("password")).decode('utf-8')
            user = User(name=username, password=pass1)
            db.session.add(user)
            db.session.commit()

            # Create success flash message and send user to home screen
            flash(f"Successfully Created Account For {username}", 'success')
            return redirect(url_for('login'))

        else:
            flash(f'Username is taken. Please choose a different one.',
                  'danger')
            return render_template('register.html',
                                   title="Register",
                                   form=form)

    else:
        return render_template('register.html', title="Register", form=form)