/books

CS50web project1 - Books (Flask, SQL)

Primary LanguageJavaScript

Project 1 - Book

The project published on heroku

About the project on YouTube

Structure of Project

Content

  • The Directory static - css and js files (bootstrap, jquery, special styles for project in styles.css, which generated from sass). It is directory, which used Flask for loading static files.
  • The Directory templates with html files.
  • Python files.

Static

The general style and js is bootstrap framework. I used their grid and styles and also I used jquery for it and for rating stars animation in the review form form_review.html.

In style.css I added some styles for specific places (cards of book and rating).

Templates

Base

Base.html contains the base template and includes internal and external styles and js. I used Flask method url_for for including internal static files. Also it included navbar.html.

Navbar

Navbar.html contains the navigation and includes message.html. It is simple navigation: the main page, login, logout and register pages. The view and content of the navbar depending on user status. If the user is logged in, the username and logout are displayed, else log in and register are displayed.

Message

Message.html contains rules for messages displaying. I used bootstrap alerts for template and Flask method flash for forms of messages. I used the category for the definition of alerts class.

Index

Index.html is the template for main page with search and book catalog. Books list view is cards with title, author, year, isbn and cover. Cover for book adds from openlibrary.org. The list of books is paginated and pagination included from the template pagination.html.

Pagination

Pagination.html contains rules for creating page numbers for pagination.

Login and Register

Login.html and register.html are the simple templates with form for username and password. Password input needs two times on the registration page.

Book

Book.html contains card of the book with cover, title, author, rating and review count from goodreads.com, ISBN, year, reviews list, and includes form_review.html. Reviews list contains users reviews with the username, date, rating and text.

Review Form

Form_review.html contains form for review of the book (rating and text). The rating's input is five stars with a step in half star.

404

404.html is the simple page with text "Page not found".

Python files

Config

Environment variables for api key and DB:

os.getenv("GOOD_KEY")
os.getenv("DATABASE_URL")

Import

Import.py contains scripts for creating tables (books, review, users). You can see uml-scheme books.uml of the project database. The main function imports data from csv file. This script must be run first.

Goodreads

Goodreads.py contains the function get_rating(isbn). It works with api goodreads, gets the rating of the book, and returns dict with keys 'avg_rating' and 'reviews_count'. If the response was without data function returns the value "No data".

Application

Application.py contains functions:

  • _jinja2_filter_datetime(date, fmt=None). Custom filter for a date in templates. It used in Book.html for reviews list.
  • login_required(f). Method for restricting access and authorization verification. Used like decorated function for index() and book().
  • index(). Function for the main page of the site. Only Get method. Route ('/'). It contains logic for queries from search and for pagination. Returns render template index.html.
  • book(book_id). Function for the book page. Method Get is for rendering template book page book.html and Post is for saving review from form_review.html. Route ("/book/<int:book_id>").
  • logout() is a standard function for logout.
  • login(). Get method renders the template login.html. Post method checks user data and log in user. Also return error messages if the form is incorrect.
  • register(). Get method renders the template register.html. Post method checks and save user data in users table. Also return error messages if the form is incorrect.
  • api_isbn(isbn). Only Get method returns a JSON response containing the book’s title, author, publication date, ISBN number, review count, and average score. If ISBN is incorrect returns {"error": "Invalid ISBN"} and status code 422, if isbn don't found returns {"error": "Book is not found"} and status code 404. The example of correct resulting JSON:
{
  "ISBN": "0006551815", 
  "author": "Frank McCourt", 
  "average_score": 4.25, 
  "publication_date": 1999, 
  "review_count": 2, 
  "title": "'Tis: A Memoir"
}