0

So, during my work on CS50 WEB programming course I came up with this issue.

In my Books application (Project 1), when user tries to leave a second review to the same book, the code doesn't let him to do this, and redirects user to book info page. The issue is that I want some message to occur, informing that it is imposible to create a second review. I am trying to use flask flash messages for this purpose. Yet, somehow the flah message disappears from session object, when it is redirected. My Google research results say that everythong should work just fine.

I`m using VS Code for this project and did all the preparation steps as described here https://docs.cs50.net/ocw/web/projects/1/project1.html

Here is my code regarding the issue:

@app.route("/book/<id>")
@login_required
def book(id):
    bookInfo ={}
    print("after redirect: ", session)

    # get general book info from database
    dbResopnseGeneralInfo = db.execute("select * from books where id=:id", {"id": id})
    if not dbResopnseGeneralInfo:
        error = "NO INFORMATION ABOUT THIS BOOK!!!"
        render_template("book.html", error=error)

    # make a general book onfo dictionary from request result
    for key, value in zip(dbResopnseGeneralInfo.keys(), dbResopnseGeneralInfo.fetchall()[0]):
        bookInfo[key] = value

    # get book reviews and rating from database
    dbResponseReveiws = db.execute("select reviews.review, reviews.rating, users.user_name from reviews join books on reviews.book_id=books.id join users on reviews.user_id=users.id where books.id=:id", 
                            {"id": id})
    reviews = dbResponseReveiws.fetchall()

    # calculate the avarage rating and rating count of the book
    if reviews:
        ratings = []
        for review in reviews:
            ratings.append(review.rating)

        # add the results to book Info
        bookInfo['avg_rating'] = mean(ratings)
        bookInfo['count_ratings'] = len(ratings)

    # search for the book on Goodreads database
    response = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": goodReadsKey, "isbns": bookInfo['isbn']})
    if response.status_code == 200:
        goodReadStats = response.json()

        # add goodReads data to book info
        bookInfo['goodreads_reviews_count'] = goodReadStats['books'][0]['work_text_reviews_count']
        bookInfo['goodreads_avg_rating'] = goodReadStats['books'][0]['average_rating']

    return render_template("book.html", bookInfo = bookInfo, reviews=reviews, error=error)


# Submit review to the book
@app.route("/submit-review/<id>", methods=["POST"])
@login_required
def submitReview(id):

    # collect info aobut review
    review = ReviewSubmitionForms(id)

    if review.isNotFirstInDatabase(db):
        flash("YOU HAVE ALREADY LEFT REVIEW TO THIS BOOK!!!")
        print("before redirect: ", session)
        return redirect(f'/book/{id}')

    # add review to the database
    db.execute("insert into reviews (user_id, book_id, rating, review) values (:user_id, :book_id, :rating, :review);", 
                                    {'user_id': review.user_id, 'book_id': review.book_id, 'rating': review.rating, 'review':review.review})
    db.commit()
    return redirect(url_for('book', id = id))

Thank you in advance for any help!

0

Even i faced the same issue, try using cookies.Check this

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .