From 07d713da15e6740b962907c59e7dca76a6100821 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sat, 10 Jun 2023 23:58:12 +1000 Subject: [PATCH] now handle series book numbering in the books on shelf page. Fixes BUG-22 --- BUGs | 1 - main.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/BUGs b/BUGs index fd2da0c..5f36f48 100644 --- a/BUGs +++ b/BUGs @@ -2,7 +2,6 @@ #### BUGS (next-23) BUG-21: parent series now showing all books (thomas covenant series) -BUG-22: books on shelf, is not ordered by series/title, or anything I can see :( ### DB/back-end diff --git a/main.py b/main.py index 7e9f908..b5ba09f 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,7 @@ from flask_login import LoginManager, login_user, login_required, UserMixin, cur from flask_ldap3_login.forms import LDAPLoginForm import re import os +import contextlib ####################################### Flask App globals ####################################### app = Flask(__name__) @@ -767,9 +768,51 @@ def rem_parent_books_from_series(pid): @app.route("/books_on_shelf", methods=["GET"]) @login_required def books_on_shelf(): + # start with all books owned, but it includes sub-books, so remove them... books = Book.query.join(Owned).filter(Owned.name=='Currently Owned').all() RemSubs(books) - return render_template("books.html", books=books, page_title="Books on Shelf", order_by="Author(s)", show_cols='', hide_cols='' ) + + # now we order them (they are in author order, but not inside that authors list) + + # because a book can be in multiple series, without any ordering we need to find + # any book that is in a series, then go through each series to find the one with + # the most books in it. THEN we need to get all those books and put them in the + # ordered_books array, and remove them from the books array + ordered_books=[] + handle_parent=[] + currently_owned = Owned.query.filter(Owned.name=='Currently Owned').one() + print( f"co={currently_owned}" ) + for b in books: + if b.series: + # find biggest Series + max_num_books=0 + for s in b.series: + if max_num_books < s.num_books: + max_num_books = s.num_books + max_series_id= s.id + + #order all books (sub-books too here!) in this series with the most books + bsl=Book_Series_Link.query.filter( Book_Series_Link.series_id==max_series_id ).order_by(Book_Series_Link.book_num).all() + for tmp in bsl: + tmp_b = Book.query.get(tmp.book_id) + # skip any books that are not owned sneaking back in + if tmp_b.owned != currently_owned.id: + continue + if tmp_b.IsChild(): + # this child book wont be in books, but its parent will -> use it instead + if tmp_b.parent[0].id not in handle_parent: + books.remove(tmp_b.parent[0]) + ordered_books.append(tmp_b.parent[0]) + handle_parent.append(tmp_b.parent[0].id) + else: + if tmp_b.id not in handle_parent: + books.remove(tmp_b) + ordered_books.append(tmp_b) + else: + # book not in a series or a sub-book, so just add this book to the ordered list + ordered_books.append(b) + + return render_template("books.html", books=ordered_books, page_title="Books on Shelf", order_by="Author(s)", show_cols='', hide_cols='' ) @app.route("/unrated_books", methods=["GET"]) @login_required