From 363cc315176efa35474805dcd217acfb65516c95 Mon Sep 17 00:00:00 2001 From: MJ Rossetti Date: Fri, 26 Apr 2024 11:17:47 -0400 Subject: [PATCH] Example Customizations - Inclass Online --- app/models/book.py | 67 +++++++++++------------ web_app/__init__.py | 2 + web_app/routes/book_routes.py | 10 ++++ web_app/templates/about.html | 1 + web_app/templates/books.html | 26 +++++++++ web_app/templates/bootstrap_5_layout.html | 9 ++- web_app/templates/home.html | 2 + 7 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 web_app/routes/book_routes.py create mode 100644 web_app/templates/books.html diff --git a/app/models/book.py b/app/models/book.py index f139442..8fb3c2a 100644 --- a/app/models/book.py +++ b/app/models/book.py @@ -1,35 +1,34 @@ -#from app.db import BaseModel -# -#class Book(BaseModel): -# -# SHEET_NAME = "books" -# -# COLUMNS = ["title", "author", "year"] -# -# SEEDS = [ -# {"title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960}, -# {"title": "1984", "author": "George Orwell", "year": 1949}, -# {"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925}, -# {"title": "The Catcher in the Rye", "author": "J.D. Salinger", "year": 1951}, -# {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}, -# {"title": "To the Lighthouse", "author": "Virginia Woolf", "year": 1927}, -# {"title": "The Hobbit", "author": "J.R.R. Tolkien", "year": 1937}, -# {"title": "Moby-Dick", "author": "Herman Melville", "year": 1851}, -# {"title": "Brave New World", "author": "Aldous Huxley", "year": 1932}, -# {"title": "Alice's Adventures in Wonderland", "author": "Lewis Carroll", "year": 1865}, -# {"title": "Harry Potter and the Philosopher's Stone", "author": "J.K. Rowling", "year": 1997}, -# {"title": "Harry Potter and the Chamber of Secrets", "author": "J.K. Rowling", "year": 1998}, -# ] -# -# -#if __name__ == "__main__": -# -# books = Book.all() -# print("FOUND", len(books), "BOOKS") -# if any(books): -# for book in books: -# print(book.title, book.author, book.year) -# else: -# Book.seed() -# +from app.db import BaseModel + +class Book(BaseModel): + + SHEET_NAME = "books" + + COLUMNS = ["title", "author", "year"] + + SEEDS = [ + {"title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960}, + {"title": "1984", "author": "George Orwell", "year": 1949}, + {"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925}, + {"title": "The Catcher in the Rye", "author": "J.D. Salinger", "year": 1951}, + {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}, + {"title": "To the Lighthouse", "author": "Virginia Woolf", "year": 1927}, + {"title": "The Hobbit", "author": "J.R.R. Tolkien", "year": 1937}, + {"title": "Moby-Dick", "author": "Herman Melville", "year": 1851}, + {"title": "Brave New World", "author": "Aldous Huxley", "year": 1932}, + {"title": "Alice's Adventures in Wonderland", "author": "Lewis Carroll", "year": 1865}, + {"title": "Harry Potter and the Philosopher's Stone", "author": "J.K. Rowling", "year": 1997}, + {"title": "Harry Potter and the Chamber of Secrets", "author": "J.K. Rowling", "year": 1998}, + ] + + +if __name__ == "__main__": + + books = Book.all() + print("FOUND", len(books), "BOOKS") + if any(books): + for book in books: + print(book.title, book.author, book.year) + else: + Book.seed() diff --git a/web_app/__init__.py b/web_app/__init__.py index cc2a7cf..072c58c 100644 --- a/web_app/__init__.py +++ b/web_app/__init__.py @@ -15,6 +15,7 @@ from web_app.routes.user_routes import user_routes from web_app.routes.product_routes import product_routes from web_app.routes.order_routes import order_routes +from web_app.routes.book_routes import book_routes load_dotenv() @@ -70,6 +71,7 @@ def create_app(): app.register_blueprint(user_routes) app.register_blueprint(product_routes) app.register_blueprint(order_routes) + app.register_blueprint(book_routes) return app diff --git a/web_app/routes/book_routes.py b/web_app/routes/book_routes.py new file mode 100644 index 0000000..151ae16 --- /dev/null +++ b/web_app/routes/book_routes.py @@ -0,0 +1,10 @@ +from flask import Blueprint, render_template, current_app #, session + +from app.models.book import Book + +book_routes = Blueprint("book_routes", __name__) + +@book_routes.route("/books") +def books(): + books = Book.all() + return render_template("books.html", books=books) diff --git a/web_app/templates/about.html b/web_app/templates/about.html index f479cfc..291878b 100644 --- a/web_app/templates/about.html +++ b/web_app/templates/about.html @@ -10,4 +10,5 @@

About

Here is some more info!

+

More stuff

{% endblock %} diff --git a/web_app/templates/books.html b/web_app/templates/books.html new file mode 100644 index 0000000..66d0f81 --- /dev/null +++ b/web_app/templates/books.html @@ -0,0 +1,26 @@ +{% extends "bootstrap_5_layout.html" %} +{% set page_title = "Books Page" %} +{% set active_page = "books" %} + +{% block content %} + +

Books

+ +

This is the books page

+ + {% if books|length > 0 %} + + + + + {% else %} + +

Oops, no books found.

+ + {% endif %} + +{% endblock %} diff --git a/web_app/templates/bootstrap_5_layout.html b/web_app/templates/bootstrap_5_layout.html index 4ba1dd1..f486b94 100644 --- a/web_app/templates/bootstrap_5_layout.html +++ b/web_app/templates/bootstrap_5_layout.html @@ -74,14 +74,13 @@ --> {% set public_nav = [ ('/about', 'about', 'About'), - ('/products', 'products', 'Products'), + ('/books', 'books', 'Books'), ('/login', 'login', 'Login'), ] -%} {% set protected_nav = [ ('/about', 'about', 'About'), - ('/products', 'products', 'Products'), - ('/user/orders', 'user_orders', 'Orders'), + ('/books', 'books', 'Books'), ] -%} -