def book_delete(id):
    if users_functions.admin():
        book = books_functions.get_book(id)

        if request.method == "POST":
            users_functions.check_csrf()
            books_functions.delete_book(id)
            return redirect("/")

        return render_template("bookdelete.html", book=book)

    return redirect(f"/book/{id}")
def book(id):
    in_list = False
    read = False
    error = ""

    if request.method == "POST":
        if request.form["submit"] == "Add to booklist":
            users_functions.check_csrf()
            status = request.form["booklist"]
            booklist_functions.add_to_booklist(id, status)

        if request.form["submit"] == "Rate book":
            users_functions.check_csrf()
            rate = request.form["rating"]
            review_functions.add_rating(id, rate)

        if request.form["submit"] == "Write a comment":
            users_functions.check_csrf()
            comment = request.form["comment"]
            if comment != "":
                if len(comment) < 500:
                    review_functions.add_comment(id, comment)
                else:
                    error = "Keep comments under 500 characters, please"

        if request.form["submit"] == "Delete comments":
            users_functions.check_csrf()
            comment_ids = request.form.getlist("comment_id")
            for comment_id in comment_ids:
                review_functions.delete_comment(comment_id)

    if users_functions.get_user():
        if booklist_functions.book_in_list(id):
            in_list = True

        if booklist_functions.book_read(id):
            read = True

    book = books_functions.get_book(id)
    rating = review_functions.get_rating(id)[0]

    if rating == None:
        rating = "–"

    comments = review_functions.get_comments(id)
    genres = genre_functions.get_genres(id)
    recommendations = booklist_functions.similar_books(id)

    return render_template("book.html",
                           book=book,
                           in_list=in_list,
                           read=read,
                           rating=rating,
                           comments=comments,
                           genres=genres,
                           error=error,
                           recommendations=recommendations)
def add_book():
    if users_functions.admin():
        error = ""
        year_now = datetime.datetime.now().year

        if request.method == "POST":
            users_functions.check_csrf()

            title = request.form.get("title")
            author = request.form.get("author")
            year = request.form.get("year")
            description = request.form.get("description")
            genres = request.form.getlist("genre")
            if 1 < len(title) < 100:
                if 9 < len(description) < 1000:
                    if genres != []:

                        book_added = books_functions.add_book(
                            author, title, year, description)
                        if book_added:
                            author_id = books_functions.author_exists(author)
                            book_id = books_functions.book_exists(
                                author_id[0], title)[0]
                            for genre in genres:
                                genre_functions.assign_genre(book_id, genre)

                            return redirect("/")

                        else:
                            error = "Book not added"
                    else:
                        error = "You must choose at least 1 genre!"
                else:
                    error = "Description must be 10-999 characters long."
            else:
                error = "Title must be 2-99 characters long."

        authors = books_functions.get_all_authors()
        genres = genre_functions.get_all_genres()

        return render_template("addbook.html",
                               authors=authors,
                               genres=genres,
                               error=error,
                               year_now=year_now)
    else:
        return redirect("/")
def profile_booklist_update():
    if users_functions.get_user():

        if request.method == "POST":
            users_functions.check_csrf()
            new_currently_reading = request.form.getlist("reading")
            new_read = request.form.getlist("read")
            for id in new_currently_reading:
                booklist_functions.mark_as_currently_reading(id)
            for id in new_read:
                booklist_functions.mark_as_read(id)
            return redirect("/profile/booklist")

        booklist = booklist_functions.get_booklist()
        return render_template("booklist_update.html", booklist=booklist)
    else:
        return redirect("/")
def book_modify(id):
    if users_functions.admin():
        error = ""
        year_now = datetime.datetime.now().year

        if request.method == "POST":
            users_functions.check_csrf()
            title = request.form.get("title")
            author = request.form.get("author")
            year = request.form.get("year")
            description = request.form.get("description")
            genres = request.form.getlist("genre")
            if 1 < len(title) < 100:
                if 9 < len(description) < 1000:
                    if genres != []:
                        books_functions.modify_book(id, author, title, year,
                                                    description)
                        for genre in genres:
                            genre_functions.assign_genre(id, genre)
                        return redirect(f"/book/{id}")
                    else:
                        error = "You must choose at least 1 genre!"
                else:
                    error = "Description must be 10-999 characters long."
            else:
                error = "Title must be 2-99 characters long."

        book = books_functions.get_book(id)
        og_author = book[2]
        authors = books_functions.get_all_authors()
        genres = genre_functions.get_all_genres()
        og_genres = genre_functions.get_genres(id)
        og_genres = [row[1] for row in og_genres]

        return render_template("bookmodify.html",
                               book=book,
                               authors=authors,
                               genres=genres,
                               og_author=og_author,
                               og_genres=og_genres,
                               error=error,
                               year_now=year_now)

    return redirect(f"/book/{id}")
def user(id):
    if users_functions.admin():

        if request.method == "POST":
            users_functions.check_csrf()
            if request.form["submit"] == "Return rights to comment":
                users_functions.return_rights(id)

            if request.form["submit"] == "Revoke rights to comment":
                users_functions.revoke_rights(id)

            if request.form["submit"] == "Delete user":
                users_functions.delete_user(id)
                return redirect("/users")

        user = users_functions.get_user_by_id(id)

        return render_template("user.html", user=user)
    else:
        return redirect("/")
def add_genre():
    if users_functions.admin():
        error = ""

        if request.method == "POST":
            users_functions.check_csrf()

            name = request.form["name"]
            if 1 < len(name) < 51:
                genre_added = genre_functions.add_genre(name)
                if genre_added:
                    return redirect("/")

                else:
                    error = "Genre already exists."
            else:
                error = "Genre's name must be 2-50 characters long."

        return render_template("addgenre.html", error=error)
    else:
        return redirect("/")
def add_author():
    if users_functions.admin():
        error = ""

        if request.method == "POST":
            users_functions.check_csrf()

            name = request.form["name"]
            if 3 < len(name) < 100:
                author_added = books_functions.add_author(name)
                if author_added:
                    return redirect("/")

                else:
                    error = "Author already exists."
            else:
                error = "Author's name must be 4-99 characters long."

        return render_template("addauthor.html", error=error)
    else:
        return redirect("/")