allow Edit Author to show list of books for author

This commit is contained in:
2023-07-04 23:00:39 +10:00
parent caf65be74c
commit 1e7a47f43e
3 changed files with 57 additions and 3 deletions

View File

@@ -44,7 +44,7 @@ class AuthorForm(FlaskForm):
@app.route("/authors", methods=["GET"])
@login_required
def authors():
authors = Author.query.all()
authors = Author.query.order_by(Author.surname, Author.firstnames).all()
return render_template("authors.html", authors=authors, alert=st.GetAlert(), message=st.GetMessage() )
@@ -97,10 +97,49 @@ def author(id):
st.SetMessage( f"<b>Failed to modify Author:</b>&nbsp;{e.orig}" )
return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
else:
print(id)
author = Author.query.get(id)
form = AuthorForm(request.values, obj=author)
return render_template("edit_id_name.html", object=author, form=form, page_title = page_title, alert=st.GetAlert(), message=st.GetMessage() )
@app.route("/author/<author_id>/books", methods=["GET"])
@login_required
def authors_books(author_id):
""" show all the books written by the specified author """
from main import Book, Book_Author_Link
books=Book.query.join(Book_Author_Link,Author).filter(Author.id==author_id).order_by(Book.title).all()
# now need to re-order books to cater for sub-book ordering
ordered_books=[]
processed=[]
for b in books:
if b.id not in processed:
if b.IsParent():
ordered_books.append(b)
processed.append( b.id )
for bb in b.child_ref:
tmp=Book.query.get(bb.sub_book_id)
ordered_books.append( tmp )
processed.append( bb.sub_book_id )
elif b.IsChild():
ordered_books.append(b.parent[0])
processed.append( b.parent[0].id )
cnt=1
for bb in b.parent[0].child_ref:
tmp=Book.query.get(bb.sub_book_id)
# need to set parent_id and sub_book_num for indenting to work in html
tmp.parent_id=b.parent[0].id
tmp.sub_book_num=cnt
ordered_books.append( tmp )
processed.append( bb.sub_book_id )
cnt+=1
else:
ordered_books.append(b)
processed.append( b.id )
a=Author.query.get(author_id)
return render_template("books.html", books=ordered_books, page_title=f"All {a.surname}, {a.firstnames} books:", no_navbar=True )
################################################################################
# helper fund to GetAuthors -> author_list -> jinja2 for author drop-down in book.html
################################################################################