allow Edit Author to show list of books for author
This commit is contained in:
41
author.py
41
author.py
@@ -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> {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
|
||||
################################################################################
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{% if not no_navbar %}
|
||||
|
||||
<!-- Modal Dialog Box, jquery used to show / set content -->
|
||||
<div id="dbox" class="modal fade" tabindex="-1" role="dialog">
|
||||
<div class="modal-dialog mw-100 w-100">
|
||||
@@ -129,6 +131,7 @@
|
||||
</div class="collapse navbar-collapse">
|
||||
</div class="container-fluid">
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
{% if message is defined and message|length %}
|
||||
<div class="row alert alert-{{alert}}">
|
||||
@@ -141,7 +144,7 @@
|
||||
{% block main_content %}
|
||||
{% endblock main_content %}
|
||||
|
||||
{% if not InDBox %}
|
||||
{% if not InDBox and not no_navbar %}
|
||||
<!-- code to get bootstrap & bootstrap datatable to work -->
|
||||
<script src="{{ url_for( 'static', filename='upstream/jquery-3.6.0.min.js')}}"></script>
|
||||
<script src="{{ url_for( 'static', filename='upstream/bootstrap-5.0.2-dist/js/bootstrap.bundle.min.js')}}"></script>
|
||||
|
||||
@@ -24,4 +24,16 @@
|
||||
</div class="form">
|
||||
</div class="row">
|
||||
</div class="container">
|
||||
{% if page_title == 'Edit Author' %}
|
||||
<div class="row mt-5">
|
||||
<div class="offset-1 col-10" id="books_by_author">
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock main_content %}
|
||||
|
||||
{% block script_content %}
|
||||
<script>
|
||||
$.get( "/author/{{object.id}}/books", function(data) { $('#books_by_author').html(data) } );
|
||||
</script>
|
||||
{% endblock script_content %}
|
||||
|
||||
Reference in New Issue
Block a user