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"]) @app.route("/authors", methods=["GET"])
@login_required @login_required
def authors(): 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() ) 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}" ) 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() ) return render_template("edit_id_name.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
else: else:
print(id)
author = Author.query.get(id) author = Author.query.get(id)
form = AuthorForm(request.values, obj=author) 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() ) 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 # helper fund to GetAuthors -> author_list -> jinja2 for author drop-down in book.html
################################################################################ ################################################################################

View File

@@ -18,6 +18,8 @@
</head> </head>
<body> <body>
{% if not no_navbar %}
<!-- Modal Dialog Box, jquery used to show / set content --> <!-- Modal Dialog Box, jquery used to show / set content -->
<div id="dbox" class="modal fade" tabindex="-1" role="dialog"> <div id="dbox" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog mw-100 w-100"> <div class="modal-dialog mw-100 w-100">
@@ -129,19 +131,20 @@
</div class="collapse navbar-collapse"> </div class="collapse navbar-collapse">
</div class="container-fluid"> </div class="container-fluid">
</nav> </nav>
{% endif %}
{% if message is defined and message|length %} {% if message is defined and message|length %}
<div class="row alert alert-{{alert}}"> <div class="row alert alert-{{alert}}">
{{message|safe}} {{message|safe}}
{{ ClearStatus() }} {{ ClearStatus() }}
</div> </div>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% block main_content %} {% block main_content %}
{% endblock main_content %} {% endblock main_content %}
{% if not InDBox %} {% if not InDBox and not no_navbar %}
<!-- code to get bootstrap & bootstrap datatable to work --> <!-- 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/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> <script src="{{ url_for( 'static', filename='upstream/bootstrap-5.0.2-dist/js/bootstrap.bundle.min.js')}}"></script>

View File

@@ -24,4 +24,16 @@
</div class="form"> </div class="form">
</div class="row"> </div class="row">
</div class="container"> </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 %} {% endblock main_content %}
{% block script_content %}
<script>
$.get( "/author/{{object.id}}/books", function(data) { $('#books_by_author').html(data) } );
</script>
{% endblock script_content %}