def register(): if request.method == "POST": username = request.form.get("username") password = request.form.get("password") repeat = request.form.get("repeat") db = get_db() error = None if not username: error = "Username is required." elif not password: error = "Password is required." elif (not repeat) or (repeat != password): error = "Passwords don't match." elif db.execute("SELECT id FROM user WHERE username = ?", (username, )).fetchone() is not None: error = "User {} is already registered.".format(username) if error is None: db.execute("INSERT INTO user (username, password) VALUES (?, ?)", (username, generate_password_hash(password))) db.commit() # next log in to the new account user = db.execute("SELECT * FROM user WHERE username = ?", (username, )).fetchone() session.clear() session["user_id"] = user["id"] return redirect(url_for("index")) flash(error) # fall through return render_template("auth/register.html")
def bookshelf(): db = get_db() if (request.method == "GET"): book_ids = db.execute( "SELECT book_id FROM bookshelf WHERE user_id = ?", (g.user["id"], )).fetchall() books = [catalog.get_info(b["book_id"]) for b in book_ids] return render_template("bookshelf.html", books=books) action = request.form.get("action") book_id = request.form.get("book_id") user_id = g.user["id"] if not (action in ["add", "delete"] and book_id and user_id): abort(400) # client error: bad request prev_bookmark = db.execute( "SELECT book_id FROM bookshelf WHERE book_id = ? AND user_id = ?", (book_id, user_id)).fetchone() if action == "delete" and prev_bookmark is not None: db.execute("DELETE FROM bookshelf WHERE book_id = ? AND user_id = ?", (book_id, user_id)) if action == "add" and prev_bookmark is None: db.execute("INSERT INTO bookshelf (book_id, user_id) VALUES (?, ?)", (book_id, user_id)) db.commit() return redirect(url_for("books.bookshelf"))
def load_logged_in_user(): user_id = session.get("user_id") if user_id is None: g.user = None else: g.user = get_db().execute("SELECT * FROM user WHERE id = ?", (user_id, )).fetchone()
def info(id): db = get_db() res = catalog.get_info(id) logged_in = g.user and g.user["id"] bookmarked = None if (logged_in): # if logged in, check if the user has a bookmark already bookmarked = db.execute( "SELECT book_id FROM bookshelf WHERE book_id = ? AND user_id = ?", (id, g.user["id"])).fetchone() is not None else: bookmarked = False # Pull book data to compare to related books book_title = res["title"] book_author = res["authors"][0]["name"] related_books_data = [] related_book_ids = [] # Get other books by author from API related_books_data = catalog.search("title-author", book_author) # Calculate number of other books by author to no get error trying to display numerous books num_books = 0 for book in related_books_data: num_books += 1 #Get related books while ensuring they are not same title or book ID as original book count = 0 book_num = 0 while count < 5 and count < num_books: related_book_id = related_books_data[book_num]["id"] related_book_title = related_books_data[book_num]["title"] if related_book_id == id: if (book_num + 1) < num_books: book_num += 1 related_book_id = related_books_data[book_num]["id"] related_book_ids.insert(count, related_book_id) elif related_book_title == book_title: if (book_num + 1) < num_books: book_num += 1 related_book_id = related_books_data[book_num]["id"] related_book_ids.insert(count, related_book_id) elif related_book_id != id and related_book_title != book_title: related_book_ids.insert(count, related_book_id) #If book_num was already increased, bypass this increase so that we don't overstep index if (book_num + 1) >= num_books: count += 1 else: book_num += 1 count += 1 return render_template("bookInfo.html", book=res, bookmarked=bookmarked, relatedbooks=related_book_ids)
def getBookmark(id, page): db = get_db() if not g: abort(400) db.execute( "INSERT INTO bookmark (book_id, user_id, page, implicit) VALUES(?, ?, ?, ?)", (id, g.user["id"], page, 1)) newURL = url_for("books.info", id=id) db.commit() return redirect(newURL)
def send_to_bookmark(id): db = get_db() if not g: abort(400) # Retrieve bookmark info from row with highest bookmark_id thisBook = db.execute( "SELECT * FROM bookmark WHERE user_id = ? AND book_id = ? AND bookmark_id = (SELECT MAX(bookmark_id) FROM bookmark)", (g.user["id"], id)).fetchone() # If case for no bookmarks if thisBook is None: newURL = url_for("books.readPage", id=id, page=1) return redirect(newURL) #Send user to the correct page page = thisBook[3] pageURL = url_for("books.readPage", id=id, page=page) return redirect(pageURL)
def login(): if request.method == "POST": username = request.form.get("username") password = request.form.get("password") db = get_db() error = None user = db.execute("SELECT * FROM user WHERE username = ?", (username, )).fetchone() if user is None: error = "Incorrect username." elif not check_password_hash(user["password"], password): error = "Incorrect password." if error is None: session.clear() session["user_id"] = user["id"] return redirect(url_for("index")) flash(error) # fall through return render_template("auth/login.html")