def add_read_book(): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("add_read_book.html", admin=admin) if request.method == "POST": title = str(request.form["title"]) author = str(request.form["author"]) comment = str(request.form.get("comment")) rating = request.form.get("rating") genre = str(request.form.get("comment")) pages = request.form.get("pages") summary = request.form.get("summary") user_id = users.user_id() if not title: return render_template("error.html", message="'Title' missing.") row = books_read.check_book(user_id, title) if row.rowcount == 1: return render_template("error.html", message="You've already entered this book") if not author: return render_template("error.html", message="'Author' missing.") if not pages: return render_template("error.html", message="'Pages' missing.") user_id = users.user_id() books_read.new_book(title, author, comment, rating, user_id, genre, pages, summary) return redirect("/my_books_read") else: return render_template("error.html", message="Error adding a book")
def add_current_book(): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("add_current_book.html", admin=admin) if request.method == "POST": title = str(request.form["title"]) author = str(request.form["author"]) user_id = users.user_id() if not title: return render_template("error.html", message="Title missing.") row = books_currently_reading.check_book(user_id, title) if row.rowcount == 1: return render_template("error.html", message="You've already entered this book") if not author: return render_template("error.html", message="Author missing.") plot_summary = str(request.form.get("plot_summary")) genre = str(request.form.get("genre")) try: current_page = request.form.get("current_page") if not current_page: return render_template("error.html", message="Current Page missing.") except ValueError: return render_template("error.html", message="Current Page must be a number.") try: pages = request.form.get("pages") if not pages: return render_template("error.html", message="Page Count missing.") except ValueError: return render_template("error.html", message="Page must be a number.") user_id = users.user_id() books_currently_reading.new_book(title, author, genre, plot_summary, current_page, pages, user_id) return redirect("/my_current_books") else: return render_template("error.html", message="Error adding a book")
def api_review(): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("search_by_title.html", admin=admin) if request.method == "POST": title = str(request.form.get("title")) endpoint2 = "https://api.nytimes.com/svc/books/v3/reviews.json?title=" + title + "&api-key=" + API_KEY response2 = requests.get(endpoint2) data2 = response2.text parsed2 = json.loads(data2) results = parsed2["results"] admin = users.is_admin(users.user_id()) if (results): length = len(results) list = [] links = [] for i in range(length): message = results[i]["book_title"] + ": " + results[i]["summary"] list.append(message) for i in range(length): message = results[i]["url"] links.append(message) return render_template("summary_by_title.html", title=title, items=list, links=links, admin=admin) else: return render_template("error_title.html", message=title + " not found")
def api_review2(): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("search_by_author.html", admin=admin) if request.method == "POST": firstname = str(request.form["firstname"]) lastname = str(request.form["lastname"]) author = str(firstname + "+" + lastname) authorname = str(firstname + " " + lastname) endpoint3 = "https://api.nytimes.com/svc/books/v3/reviews.json?author=" + author + "&api-key=" + API_KEY response3 = requests.get(endpoint3) data3 = response3.text parsed3 = json.loads(data3) results2 = parsed3["results"] admin = users.is_admin(users.user_id()) if results2: length = len(results2) list = [] links = [] for i in range(length): message = results2[i]["book_title"] + ": " + results2[i][ "summary"] list.append(message) for i in range(length): message = results2[i]["url"] links.append(message) return render_template("summary_by_author.html", author=authorname, items=list, links=links, admin=admin) else: return render_template("error_title.html", message=authorname + " not found")
def login(): if request.method == "GET": return render_template("login.html") if request.method == "POST": session["username"] = request.form.get("username") username = request.form.get("username") password = request.form.get("password") try: if not username: return render_template("error.html", message="username missing") if not password: return render_template("error.html", message="password missing") if users.login(username, password): if "user_id" in session: flash(username + " logged in", "success") admin = users.is_admin(users.user_id()) if admin: return redirect(url_for("home_admin")) else: return redirect("/home") else: flash("You are not logged in.", "danger") return render_template("error.html", message="Wrong username or password") except Exception as e: print(e)
def my_books_read_update_genre(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("genre_update.html", id=book_id, admin=admin) if request.method == "POST": genre = request.form.get("genre") books_read.update_genre(genre, book_id) return redirect("/my_books_read")
def my_books_read_update_comment(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("comment_update.html", id=book_id, admin=admin) if request.method == "POST": comment = request.form.get("comment") books_read.update_comment(comment, book_id) return redirect("/my_books_read")
def my_current_books_update_summary(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("summary_update.html", id=book_id, admin=admin) if request.method == "POST": summary = str(request.form.get("summary")) books_currently_reading.update_summary(summary, book_id) return redirect("/my_current_books")
def my_books_read_update_summary(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("summary_update_books_read.html", id=book_id, admin=admin) if request.method == "POST": plot_summary = request.form.get("summary") books_read.update_summary(plot_summary, book_id) return redirect("/my_books_read")
def my_books_read_update_rating(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("rating_update.html", id=book_id, admin=admin) if request.method == "POST": rating = request.form.get("rating") books_read.update_rating(rating, book_id) return redirect("/my_books_read")
def my_current_books_update_page_count(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("page_count_update.html", id=book_id, admin=admin) if request.method == "POST": try: pages = int(request.form.get("pages")) books_currently_reading.update_pages(pages, book_id) return redirect("/my_current_books") except ValueError: return render_template("error.html", message="Page count must be a number.")
def add_future_book(): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("add_future_book.html", admin=admin) if request.method == "POST": title = str(request.form["title"]) author = str(request.form["author"]) user_id = users.user_id() if not title: return render_template("error.html", message="Title missing.") row = future_books.check_book(user_id, title) if row.rowcount == 1: return render_template("error.html", message="You've already entered this book") if not author: return render_template("error.html", message="Author missing.") user_id = users.user_id() future_books.new(title, author, user_id) return redirect("/books_to_read_list") else: return render_template("error.html", message="Error adding a book")
def my_current_books_update_current_page(book_id): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("current_page_update.html", id=book_id, admin=admin) if request.method == "POST": try: current_page = int(request.form.get("current_page")) pages = int(books_currently_reading.page_count(book_id)[0]) if pages < current_page: return render_template("error.html", message="Page Count must be greater than or equal to current page.") books_currently_reading.update_page_number(current_page, book_id) return redirect("/my_current_books") except ValueError: return render_template("error.html", message="Current page must be a number.")
def add_link(): if request.method == "GET": admin = users.is_admin(users.user_id()) return render_template("add_link.html", admin=admin) if request.method == "POST": title = str(request.form.get("title")) url = str(request.form.get("url")) if not title: return render_template("error.html", message="Title missing.") if not url: return render_template("error.html", message="Url missing.") sql = "INSERT into links(title, url) VALUES (:title,:url)" db.session.execute(sql, {"title": title, "url": url}) db.session.commit() flash("Link added successfully!", "success") return redirect("/home")
def bestsellers(): if g.user is None: return redirect(url_for("index")) else: titles = [] images = [] admin = users.is_admin(users.user_id()) for i in range(len(book_list)): message = book_list[i]["title"] + ", " + book_list[i][ "author"] + ", Description: " + book_list[i][ "description"] + " " titles.append(message) images.append(book_list[i]["book_image"]) return render_template("bestseller_list.html", admin=admin, message="Current Bestsellers:", items=titles, images=images)
def community(): admin = users.is_admin(users.user_id()) user_count = User.query.count() count_list = books_read.count_books_read_by_user() title_list = books_currently_reading.books_currently_read_by_users() b_list = [] for i in range(len(title_list)): message = str(title_list[i])[1:-1] message2 = message.replace("'", "") message3 = message2.replace(",", "") b_list.append(message3) read_books = books_read.books_read_by_users() readb_list = [] for i in range(len(read_books)): message = str(read_books[i])[1:-1] message2 = message.replace("'", "") message3 = message2.replace(",", "") readb_list.append(message3) sql1 = "SELECT link_id, title, url from links" result1 = db.session.execute(sql1) link_list = result1.fetchall() sql2 = "SELECT book_id, title, string_agg(comment, ', 'ORDER BY comment) AS comment_list, rating, username, " \ "user_id FROM books_read LEFT JOIN users ON users.id = books_read.user_id WHERE books_read.is_public=TRUE GROUP BY 1, users.username, " \ "books_read.user_id, books_read.rating, books_read.book_id ORDER BY (rating IS NULL), rating DESC" result2 = db.session.execute(sql2) db.session.commit() read_books_comments = result2.fetchall() return render_template("community.html", admin=admin, items=count_list, books=b_list, read_books=readb_list, count=user_count, links=link_list, comments=read_books_comments)
def show_books(): user_id = users.user_id() my_list = future_books.show(user_id) admin = users.is_admin(users.user_id()) return render_template("future_reading_list.html", items=my_list, admin=admin)
def home(): admin = users.is_admin(users.user_id()) return render_template("home.html", admin=admin)
def show_my_current_books(): user_id = users.user_id() my_current_book_list = books_currently_reading.show(user_id) admin = users.is_admin(users.user_id()) return render_template("my_current_books.html", items=my_current_book_list, admin=admin)
def home_admin(): user_list = users.get_users() admin = users.is_admin(users.user_id()) return render_template("home_admin.html", user_list=user_list, admin=admin)