135 lines
5.2 KiB
Python
135 lines
5.2 KiB
Python
from flask import Flask
|
|
from flask import render_template
|
|
from flask import request
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
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,
|
|
db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
|
|
db.Column('author_id', db.Integer, db.ForeignKey('author.id'))
|
|
)
|
|
|
|
book_publisher_link = db.Table('book_publisher_link', db.Model.metadata,
|
|
db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
|
|
db.Column('publisher_id', db.Integer, db.ForeignKey('publisher.id'))
|
|
)
|
|
|
|
class Book_Sub_Book_Link(db.Model):
|
|
__tablename__ = 'book_sub_book_link'
|
|
book_id = db.Column( db.Integer, db.ForeignKey('book.id'), primary_key=True)
|
|
sub_book_id = db.Column( db.Integer, db.ForeignKey('book.id'), primary_key=True)
|
|
sub_book_num = db.Column( db.Integer )
|
|
|
|
def __repr__(self):
|
|
return "<book_id: {}, sub_book_id: {}, sub_book_num>".format(self.book_id, self.sub_book_id, self.sub_book_num )
|
|
|
|
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)
|
|
publisher = db.relationship('Publisher', secondary=book_publisher_link)
|
|
# sub_book = db.relationship('Book', secondary="book_sub_book_link",
|
|
# primaryjoin="Book.id == Book_Sub_Book_Link.book_id",
|
|
# secondaryjoin="Book.id == Book_Sub_Book_Link.sub_book_id",
|
|
# backref="parent_book",
|
|
# )
|
|
|
|
def __repr__(self):
|
|
return "<id: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".format(self.title, self.id, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher )
|
|
|
|
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 )
|
|
|
|
def __repr__(self):
|
|
return "<firstnames: {}, surname: {}, id: {}>".format(self.firstnames, self.surname, self.id)
|
|
|
|
class Publisher(db.Model):
|
|
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
|
name = db.Column(db.String(50), unique=False, nullable=False)
|
|
|
|
def __repr__(self):
|
|
return "<name: {}, id: {}>".format(self.name, self.id)
|
|
|
|
### setup serializer schemas, to make returning books/authors easier
|
|
class AuthorSchema(ma.SQLAlchemyAutoSchema):
|
|
class Meta:
|
|
model = Author
|
|
include_relationships = True
|
|
load_instance = True
|
|
|
|
class PublisherSchema(ma.SQLAlchemyAutoSchema):
|
|
class Meta:
|
|
model = Publisher
|
|
include_relationships = True
|
|
load_instance = True
|
|
|
|
class Book_Sub_Book_Link(ma.SQLAlchemyAutoSchema):
|
|
class Meta:
|
|
model = Book_Sub_Book_Link
|
|
include_relationships = True
|
|
load_instance = True
|
|
|
|
#class BookSchema(ma.SQLAlchemyAutoSchema):
|
|
# class Meta:
|
|
# model = Book
|
|
# include_relationships = True
|
|
# load_instance = True
|
|
|
|
author_schema = AuthorSchema()
|
|
publisher_schema = PublisherSchema()
|
|
book_sub_book_link_schema = Book_Sub_Book_Link()
|
|
#book_schema = BookSchema()
|
|
|
|
####################################### ROUTES #######################################
|
|
@app.route("/books", methods=["GET"])
|
|
def books():
|
|
if request.form:
|
|
print(request.form)
|
|
# books = Book.query.all()
|
|
#### quick hack - lets just return all the books not in the first 6, but should be those that are not in list of book_id's from book_sub_book_link
|
|
books = Book.query.filter( ~ Book.id.in_([0, 1, 2, 3, 4, 5, 6]) ).all()
|
|
return render_template("books.html", books=books)
|
|
|
|
@app.route("/book/<id>", methods=["GET"])
|
|
def book(id):
|
|
book = Book.query.get(id)
|
|
book_s = book_schema.dump(book)
|
|
subs = db.engine.execute ( "select book_id, sub_book_id, sub_book_num from book_sub_book_link where book_id = " + id )
|
|
sub_book=[]
|
|
for row in subs:
|
|
sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num } )
|
|
book_s['sub_book'] = sub_book
|
|
|
|
return render_template("books.html", books=book_s )
|
|
|
|
@app.route("/authors", methods=["GET"])
|
|
def author():
|
|
authors = Author.query.all()
|
|
return render_template("author.html", authors=authors)
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", debug=True)
|
|
|