added html for subbooks, made up/down buttons that work too. Also put back reference from subbook to parent book, via ORM data - need to investigate more, but it all works

This commit is contained in:
2020-11-30 21:59:50 +11:00
parent 5cc03fe7e7
commit ce34f68254
3 changed files with 78 additions and 27 deletions

54
main.py
View File

@@ -92,6 +92,8 @@ class Book(db.Model):
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" )
parent = db.relationship('Book', 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" )
def IsParent(self):
if len(self.child_ref):
return True
@@ -139,7 +141,7 @@ class Book(db.Model):
return
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 )
return "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}, parent: {}>".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, self.parent )
class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema):
class Meta: model = Book_Sub_Book_Link
@@ -283,26 +285,50 @@ def books_for_series(id):
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)
@app.route("/subbooks_for_book/<id>", methods=["GET", "POST"])
def subbooks_for_book(id):
print( "called subbooks_for_book: {}".format(id) )
if request.method == 'POST':
if 'move_button' in request.form:
# split form's pressed move_button to yield: dir ("up" or "down") and bid1 = book_id of subbook
dir, bid1 = request.form['move_button'].split('-')
bsbl1 = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==id, Book_Sub_Book_Link.sub_book_id==bid1 ).all()
if dir == "up":
swap_with_num = bsbl1[0].sub_book_num-1
else:
swap_with_num = bsbl1[0].sub_book_num+1
bid2=GetBookIdFromBookSubBookLinkByIdAndSubBookNum( id, swap_with_num )
bsbl2 = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==id, Book_Sub_Book_Link.sub_book_id==bid2 ).all()
# swap the 2 books (by switching sub_book_nums)
tmp=bsbl1[0].sub_book_num
bsbl1[0].sub_book_num = bsbl2[0].sub_book_num
bsbl2[0].sub_book_num = tmp
db.session.commit()
####################################
# force sub books for jinja2 to be able to use (ORM is not giving all the details
####################################
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 ) )
subs = db.engine.execute ( "select bsb.book_id, bsb.sub_book_id, bsb.sub_book_num, book.title, \
r.name as 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, rating r\
where bsb.book_id = {} and book.id = bsb.sub_book_id and book.id = bal.book_id and \
bal.author_id = author.id and r.id = book.rating \
order by bsb.sub_book_num".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( { 'book_id': row.book_id, '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 } )
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 } )
return render_template("subbooks_for_book.html", sub_books=sub_book, s2=subs )
book_s['sub_book'] = sub_book
@app.route("/book/<id>", methods=["GET"])
def book(id):
book = Book.query.get(id)
book_s = book_schema.dump(book)
book_form=BookForm(request.form)
# set defaults for drop-down's based on this book
@@ -314,7 +340,7 @@ def book(id):
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 )
return render_template("book.html", b=book, books=book_s, book_form=book_form, author_list=author_list, publisher_list=publisher_list, genre_list=genre_list )
@app.route("/", methods=["GET"])
def main_page():