different way to do the moving in series, more complex, but will handle subbooks in series, etc.

This commit is contained in:
2020-11-29 15:36:12 +11:00
parent 5b3ee20fc2
commit b1e6089276

102
main.py
View File

@@ -104,13 +104,29 @@ class Book(db.Model):
else:
return False
def FirstSubBookId(self):
# need to work out the first sub book and return an id?
return False
def FirstSubBookNum(self):
# need to work out the first sub book and return an id?
if self.IsParent():
return self.child_ref[0].sub_book_num
else:
return 1
def LastSubBookId(self):
# need to work out the last sub book and return an id?
return False
def LastSubBookNum(self):
# need to work out the last sub book and return an id?
if self.IsParent():
return self.child_ref[-1].sub_book_num
else:
return 1
def NumSubBooks(self):
if self.IsParent():
return len(self.child_ref)
else:
return 1
def MoveBookInSeries( self, amt ):
print( "Moving {} by {}".format( self.title, amt ) )
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 )
@@ -159,6 +175,19 @@ app.jinja_env.globals['SeriesBookNum'] = SeriesBookNum
book_schema = BookSchema()
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
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()
return tmp_book[0].book_num
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
####################################### ROUTES #######################################
@app.route("/books", methods=["GET"])
def books():
@@ -185,37 +214,42 @@ def books_for_series(id):
if 'move_button' in request.form:
print( 'we are moving a book up or down in series, we pressed: ' + request.form['move_button'] )
dir, bid = request.form['move_button'].split('-')
print( "dir="+dir)
print( "bid="+bid)
print( "id="+id)
print( "dir="+dir+", bid="+bid+", id="+id)
book1 = Book.query.get(bid)
book1_s = book_schema.dump( book1 )
# only have to check if we are a parent, as if we were a child, we
# do not have a button to even press to cause this condition
if book1.IsParent():
print( book1_s )
bsl_of_first_sub_of_b1=Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==bid,Book_Sub_Book_Link.sub_book_num==book1_s['child_ref'][0]['sub_book_num'] ).all()
print( bsl_of_first_sub_of_b1 )
bid=bsl_of_first_sub_of_b1[0].sub_book_id
print( "we want to swap a parent book - we have {} subbooks, and subbook in series is: {}".format( len(book1_s['child_ref']), bid ))
bsl1=Book_Series_Link.query.filter(Book_Series_Link.series_id==id, Book_Series_Link.book_id==bid).all()
print( bsl1[0].book_num )
if dir == "up":
other_bn=bsl1[0].book_num-1
if dir == "up":
tmp_bid = GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book1.id, book1.FirstSubBookNum() )
else:
tmp_bid = GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book1.id, book1.LastSubBookNum() )
moving_series_book_num = GetBookNumBySeriesAndBookId( id, tmp_bid )
else:
other_bn=bsl1[0].book_num+1
bsl2=Book_Series_Link.query.filter(Book_Series_Link.series_id==id, Book_Series_Link.book_num==other_bn).all()
print( bsl1[0].book_id )
print( "swap with book: " )
print( bsl2[0].book_id )
book2 = Book.query.get(bsl2[0].book_id)
book2_s = book_schema.dump( book2 )
print( book2_s )
if len(book2_s['parent_ref']):
print( "swapping with a parent book! (as the next book in the series is a child book)" )
# bsl2[0].book_num=bsl1[0].book_num
# bsl1[0].book_num=other_bn
# db.session.commit()
moving_series_book_num = GetBookNumBySeriesAndBookId( id, book1.id )
if dir == "up":
swapping_with_book_num = moving_series_book_num-1
else:
swapping_with_book_num = moving_series_book_num+1
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.IsChild():
book2 = Book.query.get( book2.parent_ref[0].book_id )
print( "notionally swapping: {} with {}".format( book1.title, book2.title ) )
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 -> this will need to move the book and its sub_books
book1.MoveBookInSeries( b1_move_by )
book2.MoveBookInSeries( b2_move_by )
books = Book.query.join(Book_Series_Link).filter(Book_Series_Link.series_id==id).all()
series = Series.query.get(id)
return render_template("books_for_series.html", books=books, series=series)