Files
books/main.py

246 lines
12 KiB
Python

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bootstrap import Bootstrap
from wtforms import SubmitField, StringField, HiddenField, SelectField, IntegerField, TextAreaField, validators
from flask_wtf import FlaskForm
app = Flask(__name__)
### what is this value? I gather I should chagne it?
DB_URL = 'postgresql+psycopg2://ddp:NWNlfa01@127.0.0.1:5432/library'
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config.from_mapping( SECRET_KEY=b'\xd6\x04\xbdj\xfe\xed$c\x1e@\xad\x0f\x13,@G')
db = SQLAlchemy(app)
ma = Marshmallow(app)
Bootstrap(app)
from author import Author, AuthorForm, AuthorSchema, GetAuthors
from publisher import Publisher, PublisherForm, PublisherSchema, GetPublishers
from genre import Genre, GenreForm, GenreSchema, GetGenres
from condition import Condition, ConditionForm, ConditionSchema, GetConditionById
from covertype import Covertype, CovertypeForm, CovertypeSchema, GetCovertypeById
from owned import Owned, OwnedForm, OwnedSchema, GetOwnedById
from rating import Rating, RatingForm, RatingSchema
from loan import Loan, LoanForm, LoanSchema
from series import Series, SeriesForm, SeriesSchema
####################################### 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'))
)
book_genre_link = db.Table('book_genre_link', db.Model.metadata,
db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
db.Column('genre_id', db.Integer, db.ForeignKey('genre.id'))
)
class Book_Loan_Link(db.Model):
__tablename__ = "book_loan_link"
book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
loan_id = db.Column(db.Integer, db.ForeignKey('loan.id'), unique=True, nullable=False, primary_key=True)
def __repr__(self):
return "<book_id: {}, loan_id: {}>".format(self.book_id, self.loan_id)
class Book_Series_Link(db.Model):
__tablename__ = "book_series_link"
book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
series_id = db.Column(db.Integer, db.ForeignKey('series.id'), unique=True, nullable=False, primary_key=True)
book_num = db.Column(db.Integer)
def __repr__(self):
return "<book_id: {}, series_id: {}, book_num: {}>".format(self.book_id, self.series_id, self.book_num)
def SeriesBookNum(series_id, book_id):
bsl=Book_Series_Link.query.filter( Book_Series_Link.book_id==book_id, Book_Series_Link.series_id==series_id ).all()
return bsl[0].book_num
class Book_Sub_Book_Link(db.Model):
__tablename__ = "book_sub_book_link"
book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
sub_book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, 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)
author = db.relationship('Author', secondary=book_author_link)
publisher = db.relationship('Publisher', secondary=book_publisher_link)
genre = db.relationship('Genre', secondary=book_genre_link )
loan = db.relationship('Loan', secondary=Book_Loan_Link.__table__);
series = db.relationship('Series', secondary=Book_Series_Link.__table__);
year_published = db.Column(db.Integer)
condition = db.Column(db.Integer, db.ForeignKey('condition.id'))
covertype = db.Column(db.Integer, db.ForeignKey('covertype.id'))
owned = db.Column(db.Integer, db.ForeignKey('owned.id'))
rating = db.Column(db.Integer, db.ForeignKey('rating.id'))
notes = db.Column(db.Text)
blurb = db.Column(db.Text)
created = db.Column(db.Date)
modified = db.Column(db.Date)
parent_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" )
child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id" )
def __repr__(self):
return "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".format(self.id, self.author, self.title, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher )
class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Book_Sub_Book_Link
class Book_Series_LinkSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Book_Series_Link
# Note not ordering this in the code below as, I can't work out how to use
# jinja2 to iterate over orderedDict - seems it doesnt support it?
# so I just hacked a list of keys in book.html
class BookSchema(ma.SQLAlchemyAutoSchema):
author = ma.Nested(AuthorSchema, many=True)
publisher = ma.Nested(PublisherSchema, many=True)
genre = ma.Nested(GenreSchema, many=True)
loan = ma.Nested(LoanSchema, many=True)
series = ma.Nested(SeriesSchema, many=True)
parent_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
class Meta: model = Book
#
# To be completed
#
class BookForm(FlaskForm):
id = HiddenField()
title = StringField('Title:', [validators.DataRequired()])
# author built by hand
# publiser built by hand
# genre built by hand
owned = SelectField( 'owned', choices=[(c.id, c.name) for c in Owned.query.order_by('id')] )
covertype = SelectField( 'covertype', choices=[(c.id, c.name) for c in Covertype.query.order_by('id')] )
condition = SelectField( 'condition', choices=[(c.id, c.name) for c in Condition.query.order_by('id')] )
year_published = IntegerField('Year Published:', [validators.NumberRange(min=1900, max=2100)] )
rating = SelectField( 'rating', choices=[(c.id, c.name) for c in Rating.query.order_by('id')] )
notes = TextAreaField('Notes:')
blurb = TextAreaField('Blurb:')
# allow jinja2 to call this python function
app.jinja_env.globals['GetCovertypeById'] = GetCovertypeById
app.jinja_env.globals['GetOwnedById'] = GetOwnedById
app.jinja_env.globals['GetConditionById'] = GetConditionById
app.jinja_env.globals['SeriesBookNum'] = SeriesBookNum
### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?)
book_schema = BookSchema()
books_schema = BookSchema(many=True)
####################################### ROUTES #######################################
@app.route("/books", methods=["GET"])
def books():
books = Book.query.all()
# ignore ORM, its too slow. Just select sub_book data and hand add it to
# the books object, and use it in jinja2 to indent/order the books/sub books
subs = db.engine.execute ( "select * from book_sub_book_link" )
for row in subs:
index = next((i for i, item in enumerate(books) if item.id == row.sub_book_id), -1)
books[index].parent_id = row.book_id
books[index].sub_book_num = row.sub_book_num
return render_template("books.html", books=books )
@app.route("/books_for_loan/<id>", methods=["GET"])
def books_for_loan(id):
books = Book.query.join(Book_Loan_Link).filter(Book_Loan_Link.loan_id==id).order_by(Book.id).all()
return render_template("books_for_loan.html", books=books)
@app.route("/books_for_series/<id>", methods=["GET", "POST"])
def books_for_series(id):
if request.method == 'POST':
if 'move_button' in request.form:
print( 'we are moving a book up or down in series, we pressed: ' + request.form['move_button'] )
dir, bid = request.form['move_button'].split('-')
print( "dir="+dir)
print( "bid="+bid)
print( "id="+id)
book1 = Book.query.get(bid)
book1_s = book_schema.dump( book1 )
# only have to check if we are a parent, as if we were a child, we
# do not have a button to even press to cause this condition
if len(book1_s['child_ref']):
print( "we want to swap a parent book" )
bsl1=Book_Series_Link.query.filter(Book_Series_Link.series_id==id, Book_Series_Link.book_id==bid).all()
print( bsl1[0].book_num )
if dir == "up":
other_bn=bsl1[0].book_num-1
else:
other_bn=bsl1[0].book_num+1
bsl2=Book_Series_Link.query.filter(Book_Series_Link.series_id==id, Book_Series_Link.book_num==other_bn).all()
print( bsl1[0].book_id )
print( "swap with book: " )
print( bsl2[0].book_id )
book2 = Book.query.get(bsl2[0].book_id)
book2_s = book_schema.dump( book2 )
print( book2_s )
if len(book2_s['child_ref']):
print( "swapping with a parent book!" )
if len(book2_s['parent_ref']):
print( "swapping with a parent book! (and the 'next' book is a child book)" )
bsl2[0].book_num=bsl1[0].book_num
bsl1[0].book_num=other_bn
db.session.commit()
books = Book.query.join(Book_Series_Link).filter(Book_Series_Link.series_id==id).all()
series = Series.query.get(id)
return render_template("books_for_series.html", books=books, series=series)
@app.route("/book/<id>", methods=["GET"])
def book(id):
book = Book.query.get(id)
book_s = book_schema.dump(book)
######
###
### okay, this manual hacking of sub_book is currently going to be needed, because in the jinja2 I want to list the book, and more than just the id number of the sub_book, I want the details... (sub_book_schema needs a book relationship BUT, dependencies mean I can't define a book schema inside sub_book schema, and I am definitely not sure how to join it anyway... for another time.
###
#####
# force sub books for jinja2 to be able to use
subs = db.engine.execute ( "select bsb.book_id, bsb.sub_book_id, bsb.sub_book_num, book.title, book.rating, book.year_published, book.notes, bal.author_id as author_id, author.surname||', '||author.firstnames as author from book_sub_book_link bsb, book, book_author_link bal, author where bsb.book_id = {} and book.id = bsb.sub_book_id and book.id = bal.book_id and bal.author_id = author.id".format( id ) )
sub_book=[]
for row in subs:
# get genres for sub book and add by hand first
tmp_g = []
genres = db.engine.execute ( "select genre.id, genre.name from genre, book_genre_link bgl where genre.id = bgl.genre_id and bgl.book_id = {}".format( row.sub_book_id ) )
for genre in genres:
tmp_g.append( { 'id': genre.id, 'name': genre.name } )
sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num, 'title' : row.title, 'rating': row.rating, 'year_published' : row.year_published, 'notes' : row.notes, 'author_id' : row.author_id, 'author' : row.author, 'genres' : tmp_g } )
book_s['sub_book'] = sub_book
book_form=BookForm(request.form)
# set defaults for drop-down's based on this book
book_form.condition.default = book.condition
book_form.covertype.default = book.covertype
book_form.owned.default = book.owned
book_form.rating.default = book.rating
book_form.process()
author_list = GetAuthors()
genre_list = GetGenres()
publisher_list = GetPublishers()
return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form, author_list=author_list, publisher_list=publisher_list, genre_list=genre_list )
@app.route("/", methods=["GET"])
def main_page():
return render_template("base.html")
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)