different way to do the moving in series, more complex, but will handle subbooks in series, etc.
This commit is contained in:
96
main.py
96
main.py
@@ -104,13 +104,29 @@ class Book(db.Model):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def FirstSubBookId(self):
|
def FirstSubBookNum(self):
|
||||||
# need to work out the first sub book and return an id?
|
# need to work out the first sub book and return an id?
|
||||||
return False
|
if self.IsParent():
|
||||||
|
return self.child_ref[0].sub_book_num
|
||||||
|
else:
|
||||||
|
return 1
|
||||||
|
|
||||||
def LastSubBookId(self):
|
def LastSubBookNum(self):
|
||||||
# need to work out the last sub book and return an id?
|
# need to work out the last sub book and return an id?
|
||||||
return False
|
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):
|
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 )
|
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()
|
book_schema = BookSchema()
|
||||||
books_schema = BookSchema(many=True)
|
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 #######################################
|
####################################### ROUTES #######################################
|
||||||
@app.route("/books", methods=["GET"])
|
@app.route("/books", methods=["GET"])
|
||||||
def books():
|
def books():
|
||||||
@@ -185,37 +214,42 @@ def books_for_series(id):
|
|||||||
if 'move_button' in request.form:
|
if 'move_button' in request.form:
|
||||||
print( 'we are moving a book up or down in series, we pressed: ' + request.form['move_button'] )
|
print( 'we are moving a book up or down in series, we pressed: ' + request.form['move_button'] )
|
||||||
dir, bid = request.form['move_button'].split('-')
|
dir, bid = request.form['move_button'].split('-')
|
||||||
print( "dir="+dir)
|
print( "dir="+dir+", bid="+bid+", id="+id)
|
||||||
print( "bid="+bid)
|
|
||||||
print( "id="+id)
|
|
||||||
book1 = Book.query.get(bid)
|
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():
|
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":
|
if dir == "up":
|
||||||
other_bn=bsl1[0].book_num-1
|
tmp_bid = GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book1.id, book1.FirstSubBookNum() )
|
||||||
else:
|
else:
|
||||||
other_bn=bsl1[0].book_num+1
|
tmp_bid = GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book1.id, book1.LastSubBookNum() )
|
||||||
bsl2=Book_Series_Link.query.filter(Book_Series_Link.series_id==id, Book_Series_Link.book_num==other_bn).all()
|
moving_series_book_num = GetBookNumBySeriesAndBookId( id, tmp_bid )
|
||||||
print( bsl1[0].book_id )
|
else:
|
||||||
print( "swap with book: " )
|
moving_series_book_num = GetBookNumBySeriesAndBookId( id, book1.id )
|
||||||
print( bsl2[0].book_id )
|
|
||||||
book2 = Book.query.get(bsl2[0].book_id)
|
if dir == "up":
|
||||||
book2_s = book_schema.dump( book2 )
|
swapping_with_book_num = moving_series_book_num-1
|
||||||
print( book2_s )
|
else:
|
||||||
if len(book2_s['parent_ref']):
|
swapping_with_book_num = moving_series_book_num+1
|
||||||
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
|
tmp_bid = GetBookIdFromSeriesByBookNum( id, swapping_with_book_num )
|
||||||
# bsl1[0].book_num=other_bn
|
book2 = Book.query.get( tmp_bid )
|
||||||
# db.session.commit()
|
# 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()
|
books = Book.query.join(Book_Series_Link).filter(Book_Series_Link.series_id==id).all()
|
||||||
series = Series.query.get(id)
|
series = Series.query.get(id)
|
||||||
return render_template("books_for_series.html", books=books, series=series)
|
return render_template("books_for_series.html", books=books, series=series)
|
||||||
|
|||||||
Reference in New Issue
Block a user