MoveBookInSeries now works and moves sub books too if needed, added better comments as the code is a bit hairy. Most importantly, this works for all known cases

This commit is contained in:
2020-11-29 16:01:53 +11:00
parent b1e6089276
commit 99f0a9fdbf
2 changed files with 25 additions and 10 deletions

2
BUGs
View File

@@ -1,6 +1,4 @@
moving a book:
with sub-books: up/down in series will fail as the book_id is not in the book_series_link
likely logic... for each sub-book... swap new book with sub-book#1, then for all other sub-books, up/down by 1
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)

33
main.py
View File

@@ -124,8 +124,18 @@ class Book(db.Model):
else:
return 1
def MoveBookInSeries( self, amt ):
def MoveBookInSeries( self, series_id, amt ):
print( "Moving {} by {}".format( self.title, amt ) )
if self.IsParent():
tmp_book=book_schema.dump(self)
for book in tmp_book['child_ref']:
tmp_bid=GetBookIdFromBookSubBookLinkByIdAndSubBookNum( self.id, book['sub_book_num'] )
bsl=Book_Series_Link.query.filter( Book_Series_Link.book_id==tmp_bid, Book_Series_Link.series_id==series_id ).all()
bsl[0].book_num=bsl[0].book_num+amt
else:
bsl=Book_Series_Link.query.filter( Book_Series_Link.book_id==self.id, Book_Series_Link.series_id==series_id ).all()
bsl[0].book_num=bsl[0].book_num+amt
db.session.commit()
return
def __repr__(self):
@@ -212,10 +222,11 @@ def books_for_loan(id):
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'] )
# 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('-')
print( "dir="+dir+", bid="+bid+", id="+id)
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 book1.IsParent():
if dir == "up":
tmp_bid = GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book1.id, book1.FirstSubBookNum() )
@@ -224,19 +235,22 @@ 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)
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)
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 really
# 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 )
print( "notionally swapping: {} with {}".format( book1.title, book2.title ) )
# 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":
@@ -246,9 +260,12 @@ def books_for_series(id):
b1_move_by=b2_numsb
b2_move_by=-b1_numsb
# MoveBookInSeries -> this will need to move the book and its sub_books
book1.MoveBookInSeries( b1_move_by )
book2.MoveBookInSeries( 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)