using font awesome, datatables, bootstrap and fixed so class attributes are serialized via marshmallow and it all works with jinja2 now
This commit is contained in:
46
main.py
46
main.py
@@ -2,13 +2,14 @@ from flask import Flask
|
||||
from flask import render_template
|
||||
from flask import request
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
import logging
|
||||
from flask_marshmallow import Marshmallow
|
||||
|
||||
DB_URL = 'postgresql+psycopg2://ddp:NWNlfa01@127.0.0.1:5432/library'
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db = SQLAlchemy(app)
|
||||
ma = Marshmallow(app)
|
||||
|
||||
####################################### CLASSES / DB model #######################################
|
||||
book_author_link = db.Table('book_author_link', db.Model.metadata,
|
||||
@@ -19,22 +20,51 @@ book_author_link = db.Table('book_author_link', db.Model.metadata,
|
||||
class Book(db.Model):
|
||||
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
||||
title = db.Column(db.String(100), unique=True, nullable=False)
|
||||
year_published = db.Column(db.Integer)
|
||||
rating = db.Column(db.String(20))
|
||||
condition = db.Column(db.String(20))
|
||||
owned = db.Column(db.String(20))
|
||||
covertype = db.Column(db.String(20))
|
||||
notes = db.Column(db.Text)
|
||||
blurb = db.Column(db.Text)
|
||||
created = db.Column(db.Date)
|
||||
modified = db.Column(db.Date)
|
||||
|
||||
author = db.relationship('Author', secondary=book_author_link)
|
||||
|
||||
def __repr__(self):
|
||||
return "<title: {}, id: {} author: {} author.firstnames {}>".format(self.title, self.id, self.author, self.id )
|
||||
# return "<title: {}, id: {} author: {} author.firstnames {}>".format(self.title, self.id, self.author, self.author.firstnames )
|
||||
return "<title: {}, id: {} author: {}>".format(self.title, self.id, self.author )
|
||||
|
||||
class Author(db.Model):
|
||||
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
||||
firstnames = db.Column(db.String(50), unique=False, nullable=False)
|
||||
surname = db.Column(db.String(30), unique=False, nullable=False)
|
||||
|
||||
# book = db.relationship('Book', secondary=book_author_link )
|
||||
book = db.relationship('Book', secondary=book_author_link )
|
||||
|
||||
def __repr__(self):
|
||||
return "<firstnames: {}, surname: {}>".format(self.firstnames, self.surname )
|
||||
# return "<firstnames: {}, surname: {}, book: {}>".format(self.firstnames, self.surname, self.book)
|
||||
return "<firstnames: {}, surname: {}, book: {}>".format(self.firstnames, self.surname, self.book)
|
||||
|
||||
|
||||
|
||||
### setup serializer schemas, to make returning books/authors easier
|
||||
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Author
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
author = ma.Nested(AuthorSchema, many=True)
|
||||
class Meta:
|
||||
model = Book
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
book_schema = BookSchema()
|
||||
author_schema = AuthorSchema()
|
||||
|
||||
print( book_schema )
|
||||
|
||||
####################################### ROUTES #######################################
|
||||
@app.route("/books", methods=["GET"])
|
||||
@@ -47,8 +77,8 @@ def books():
|
||||
@app.route("/book/<id>", methods=["GET"])
|
||||
def book(id):
|
||||
book = Book.query.get(id)
|
||||
print( book )
|
||||
return render_template("books.html", books=book )
|
||||
book_s = book_schema.dump(book)
|
||||
return render_template("books.html", books=book_s )
|
||||
|
||||
@app.route("/authors", methods=["GET"])
|
||||
def author():
|
||||
|
||||
Reference in New Issue
Block a user