fix BUG-23 (subbook/series confusion), also remove warnings about sql alchemy overlaps, and had to fix bug I introduced when editing a book where the drop-downs no longer had content based on data in form, and stopped messing with books list when iterating over it, python != c :) so using another list called processed to know if the books do not need to be processed (again) - this finally fixed books on shelf

This commit is contained in:
2023-06-11 14:22:08 +10:00
parent c23df4dac3
commit 9f5e9ff3c2
2 changed files with 28 additions and 14 deletions

1
BUGs
View File

@@ -2,7 +2,6 @@
#### BUGS (next-23)
BUG-21: parent series now showing all books (thomas covenant series)
BUG-23: saving a book with a sub? or maybe just saving more than once? is complaining about removing from a series that we have never added
### DB/back-end

41
main.py
View File

@@ -99,8 +99,12 @@ class Book_Series_Link(db.Model):
return "<book_id: {}, series_id: {}, book_num: {}>".format(self.book_id, self.series_id, self.book_num)
def SeriesBookNum(series_id, book_id):
bsl=Book_Series_Link.query.filter( Book_Series_Link.book_id==book_id, Book_Series_Link.series_id==series_id ).all()
return bsl[0].book_num
bsl=Book_Series_Link.query.filter( Book_Series_Link.book_id==book_id, Book_Series_Link.series_id==series_id ).first()
if bsl:
return bsl.book_num
else:
print( f"WARNING: tried to find book number in this series for: book_id={book_id}, series_id={series_id} but no db data for it" )
return 0
class Book_Sub_Book_Link(db.Model):
__tablename__ = "book_sub_book_link"
@@ -119,7 +123,7 @@ class Book(db.Model):
genre = db.relationship('Genre', secondary=book_genre_link )
loan = db.relationship('Loan', secondary=Book_Loan_Link.__table__);
series = db.relationship('Series', secondary=Book_Series_Link.__table__);
bsl = db.relationship('Book_Series_Link' )
bsl = db.relationship('Book_Series_Link', overlaps="series" )
year_published = db.Column(db.Integer)
condition = db.Column(db.Integer, db.ForeignKey('condition.id'))
covertype = db.Column(db.Integer, db.ForeignKey('covertype.id'))
@@ -133,7 +137,7 @@ class Book(db.Model):
# take actual parent book as there is no real associated sub_book_num data and can just use it
parent = db.relationship('Book', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" )
# but use child_ref as sub_book_num is per book, and I can't connect an empty array of sub_book_nums to a child book array in "child"
child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", order_by="Book_Sub_Book_Link.sub_book_num" )
child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", order_by="Book_Sub_Book_Link.sub_book_num", overlaps="parent" )
def IsParent(self):
@@ -590,6 +594,11 @@ def new_book():
@login_required
def book(id):
book_form = BookForm(request.form)
book_form.publisher.choices = [(c.id, c.name) for c in Publisher.query.order_by('name')]
book_form.owned.choices=[(c.id, c.name) for c in Owned.query.order_by('id')]
book_form.covertype.choices=[(c.id, c.name) for c in Covertype.query.order_by('id')]
book_form.condition.choices=[(c.id, c.name) for c in Condition.query.order_by('id')]
book_form.rating.choices=[(c.id, c.name) for c in Rating.query.order_by('id')]
page_title='Edit Book'
CheckSeriesChange=None
if request.method == 'POST':
@@ -670,9 +679,11 @@ def book(id):
child_book.condition=book.condition
child_book.blurb=book.blurb
if book.IsChild() or (book.IsParent() and not still_in_series):
# if removing_series has something in it, then handle it
if (len(removing_series) > 0) and (book.IsChild() or (book.IsParent() and not still_in_series)):
print ("okay should raise DBox")
print ("{}".format( removing_series ))
print (f"still_in_series={still_in_series}" )
if book.IsParent():
CheckSeriesChange={'type':'parent', 'pid': book.id, 'bid': book.id, 'removing_series': removing_series }
else:
@@ -717,6 +728,11 @@ def book(id):
return render_template("base.html", alert=st.GetAlert(), message=st.GetMessage())
book_form=BookForm(obj=book)
book_form.publisher.choices = [(c.id, c.name) for c in Publisher.query.order_by('name')]
book_form.owned.choices=[(c.id, c.name) for c in Owned.query.order_by('id')]
book_form.covertype.choices=[(c.id, c.name) for c in Covertype.query.order_by('id')]
book_form.condition.choices=[(c.id, c.name) for c in Condition.query.order_by('id')]
book_form.rating.choices=[(c.id, c.name) for c in Rating.query.order_by('id')]
author_list = GetAuthors()
genre_list = GetGenres()
@@ -802,14 +818,12 @@ def books_on_shelf():
books = Book.query.join(Owned).filter(Owned.name=='Currently Owned').all()
RemSubs(books)
# now we order them (they are in author order, but not inside that authors list)
# because a book can be in multiple series, without any ordering we need to find
# any book that is in a series, then go through each series to find the one with
# the most books in it. THEN we need to get all those books and put them in the
# ordered_books array, and remove them from the books array
ordered_books=[]
handle_parent=[]
processed=[]
currently_owned = Owned.query.filter(Owned.name=='Currently Owned').one()
for b in books:
if b.series:
@@ -829,14 +843,15 @@ def books_on_shelf():
continue
if tmp_b.IsChild():
# this child book wont be in books, but its parent will -> use it instead
if tmp_b.parent[0].id not in handle_parent:
books.remove(tmp_b.parent[0])
# mark parent from books so we dont process it twice
if tmp_b.parent[0].id not in processed:
processed.append(tmp_b.id)
processed.append(tmp_b.parent[0].id)
ordered_books.append(tmp_b.parent[0])
handle_parent.append(tmp_b.parent[0].id)
else:
if tmp_b.id not in handle_parent:
books.remove(tmp_b)
if tmp_b.id not in processed:
ordered_books.append(tmp_b)
processed.append(tmp_b.id)
else:
# book not in a series or a sub-book, so just add this book to the ordered list
ordered_books.append(b)