From 8ab3b36dc0dd79f6b7f2419a2860455028453be5 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sat, 5 Dec 2020 20:45:44 +1100 Subject: [PATCH] make AddSub so we can call it in search and books, and dont use -1 array index as it breaks AddSubs - remember it is the last element in dict, so it was overwriting data --- main.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index c965d95..e152c53 100644 --- a/main.py +++ b/main.py @@ -192,8 +192,20 @@ def GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book_id, sub_book_num ): tmp_bsbl = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==book_id, Book_Sub_Book_Link.sub_book_num==sub_book_num ).all() return tmp_bsbl[0].sub_book_id +# ignore ORM, the sub_book_num comes from the link, but does not have any attached data, so can't tell which book it connects to. +# 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 +# This data is used to sort/indent subbooks +def AddSubs(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) + if index == -1: + continue + books[index].parent_id = row.book_id + books[index].sub_book_num = row.sub_book_num + ####################################### GLOBALS ####################################### -# allow jinja2 to call this python function +# allow jinja2 to call these python functions directly app.jinja_env.globals['GetCovertypeById'] = GetCovertypeById app.jinja_env.globals['GetOwnedById'] = GetOwnedById app.jinja_env.globals['GetConditionById'] = GetConditionById @@ -201,30 +213,19 @@ app.jinja_env.globals['GetPublisherById'] = GetPublisherById app.jinja_env.globals['SeriesBookNum'] = SeriesBookNum app.jinja_env.globals['ClearStatus'] = st.ClearMessage -### DDP: do I need many=True on Author as books have many authors? (or in BookSchema declaration above?) book_schema = BookSchema() ####################################### ROUTES ####################################### @app.route("/search", methods=["POST"]) def search(): - print( "term={}".format( request.form['term']) ) books = Book.query.filter(Book.title.ilike("%{}%".format(request.form['term']))).all() - print( books ) + AddSubs(books) return render_template("books.html", books=books) @app.route("/books", methods=["GET"]) def books(): books = Book.query.all() - - # ignore ORM, the sub_book_num comes from the link, but does not have any attached data, so can't tell which book it connects to. - # 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 - # This data is used to sort/indent subbooks - 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 - + AddSubs(books) return render_template("books.html", books=books ) @app.route("/books_for_loan/", methods=["GET"])