From 8d64b95d9f1050063bd9ebb8385db70c3e99ac98 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sun, 29 Nov 2020 17:40:04 +1100 Subject: [PATCH] fixed bug with swapping book in series when we dont have details on book to swap with, also really disabled up/down buttons, rather than just made them look disabled --- BUGs | 5 --- main.py | 60 ++++++++++++++++++++------------- templates/books_for_series.html | 8 ++--- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/BUGs b/BUGs index 0b2fd3b..891f8ed 100644 --- a/BUGs +++ b/BUGs @@ -1,8 +1,3 @@ -moving a book: - with book 6 of 11, and there is no book 7: see series (Wars of Light and Shadow) - likely logic... if no bsl2, then just increase/decrease bsl1.book_num --> - (it would also break if we had say books 1-5 & 8 and tried to move 8 down to 7) - series does not deal with calcd_rating... (on edit could, recalc as a catch-all, and obviously if we change a single book's rating, we should re-calc) diff --git a/main.py b/main.py index 7d55204..10963c5 100644 --- a/main.py +++ b/main.py @@ -188,7 +188,10 @@ books_schema = BookSchema(many=True) ################################# helper functions ################################### def GetBookIdFromSeriesByBookNum( series_id, book_num ): tmp_book = Book_Series_Link.query.filter(Book_Series_Link.series_id==series_id,Book_Series_Link.book_num==book_num).all() - return tmp_book[0].book_id + if len(tmp_book): + return tmp_book[0].book_id + else: + return None def GetBookNumBySeriesAndBookId( series_id, book_id ): tmp_book = Book_Series_Link.query.filter(Book_Series_Link.series_id==series_id,Book_Series_Link.book_id==book_id).all() @@ -222,11 +225,11 @@ def books_for_loan(id): def books_for_series(id): if request.method == 'POST': if 'move_button' in request.form: - # split form's pressed move_button to yield: dir ("up" or "down") and bid = book_id of book + # split form's pressed move_button to yield: dir ("up" or "down") and bid = book_id of book dir, bid = request.form['move_button'].split('-') book1 = Book.query.get(bid) - # if parent book, then up needs first sub book, if down then need last sub book (as parent book does not have a book_num in a series, its sub books do) + # if parent book, then up needs first sub book, if down then need last sub book (as parent book does not have a book_num in a series, its sub books do) if book1.IsParent(): if dir == "up": tmp_bid = GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book1.id, book1.FirstSubBookNum() ) @@ -235,37 +238,46 @@ def books_for_series(id): moving_series_book_num = GetBookNumBySeriesAndBookId( id, tmp_bid ) else: moving_series_book_num = GetBookNumBySeriesAndBookId( id, book1.id ) - # moving_series_book_num is the book_num of the book in the series we are really moving (adjusted for parent/sub book if needed) + # moving_series_book_num is the book_num of the book in the series we are really moving (adjusted for parent/sub book if needed) if dir == "up": swapping_with_book_num = moving_series_book_num-1 else: swapping_with_book_num = moving_series_book_num+1 - # swapping_with_book_num is the book_num of the book in the series we are going to swap with (notionally next/prev book in series) + # swapping_with_book_num is the book_num of the book in the series we are going to swap with (notionally next/prev book in series) tmp_bid = GetBookIdFromSeriesByBookNum( id, swapping_with_book_num ) - book2 = Book.query.get( tmp_bid ) - - # if book2 is a sub book, then we need its parent as book2 instead, as we have to move the whole book as one - if book2.IsChild(): - book2 = Book.query.get( book2.parent_ref[0].book_id ) - - # By here: book1 is parent or normal book we are swapping with book2 which is parent or normal book - b1_numsb = book1.NumSubBooks() - b2_numsb = book2.NumSubBooks() - if dir == "up": - b1_move_by=-b2_numsb - b2_move_by=b1_numsb + # okay, if tmp_bid is None, then there is no book we are swapping + # with (rare case of moving say book 7 of a series of 10, and we + # don't have details on book 6 or 8... + if tmp_bid == None: + if dir == "up": + book1.MoveBookInSeries( id, -1 ) + else: + book1.MoveBookInSeries( id, 1 ) else: - b1_move_by=b2_numsb - b2_move_by=-b1_numsb + book2 = Book.query.get( tmp_bid ) + + # if book2 is a sub book, then we need its parent as book2 instead, as we have to move the whole book as one + if book2.IsChild(): + book2 = Book.query.get( book2.parent_ref[0].book_id ) - # By here: b{1,2}_move_by is the sign & amount we are moving the relevant book (and its sub-books by). If it is a normal book, - # then its by +/-1, but if it is a parent book, then its +/- the num of sub_books as they all have to move too + # By here: book1 is parent or normal book we are swapping with book2 which is parent or normal book + b1_numsb = book1.NumSubBooks() + b2_numsb = book2.NumSubBooks() + if dir == "up": + b1_move_by=-b2_numsb + b2_move_by=b1_numsb + else: + b1_move_by=b2_numsb + b2_move_by=-b1_numsb - # MoveBookInSeries -> moves the book and its sub_books - book1.MoveBookInSeries( id, b1_move_by ) - book2.MoveBookInSeries( id, b2_move_by ) + # By here: b{1,2}_move_by is the sign & amount we are moving the relevant book (and its sub-books by). If it is a normal book, + # then its by +/-1, but if it is a parent book, then its +/- the num of sub_books as they all have to move too + + # MoveBookInSeries -> moves the book and its sub_books + book1.MoveBookInSeries( id, b1_move_by ) + book2.MoveBookInSeries( id, b2_move_by ) books = Book.query.join(Book_Series_Link).filter(Book_Series_Link.series_id==id).all() series = Series.query.get(id) diff --git a/templates/books_for_series.html b/templates/books_for_series.html index f56442d..1088c6f 100644 --- a/templates/books_for_series.html +++ b/templates/books_for_series.html @@ -45,18 +45,18 @@ {% if SeriesBookNum( series.id, book.id ) == 1 %} {% endif %} {% if SeriesBookNum( series.id, book.id ) == series.num_books %} {% endif %}