adding a sub-book now works

This commit is contained in:
2020-12-26 19:11:43 +11:00
parent 6a72c6d9dd
commit 61629760cd
3 changed files with 64 additions and 47 deletions

2
README
View File

@@ -31,8 +31,6 @@ python3 main.py
### TODO:
- when remove a Parent book from a series, what do we do?
(remove all sub books from series too?)
- need to add sub_book to parent book
** okay, tried to overload /book/<id> .. too hard, need to overload /book if anything... so add_sub button needs to call different URL, and then maybe this would work? ( if parent, then show extra bit AND, if parent, add the sub_book_link when adding new book???
- need to delete 1 sub_book from book
- need to add books to loan (on loan page, and via a book search?)
- need to delete book from loan

92
main.py
View File

@@ -338,16 +338,13 @@ def subbooks_for_book(id):
return render_template("subbooks_for_book.html", sub_books=sub_book, s2=subs )
class QuickParentBook:
parent=[]
def __repr__(self):
return "<parent: {}, publisher: {}, owned: {}, covertype: {}, condition: {}, blurb: {}>".format(self.parent, self.publisher, self.owned, self.covertype, self.condition, self.blurb )
###
### Need a route to /book then incorporate the below:
###
# elif 'add_sub' in request.form:
# alert="danger"
# message="Sorry, Adding a sub-book unsupported at present"
# book = Book.query.get(id)
# parent=request.form['add_sub_parent_id']
# parent_book = Book.query.get(parent)
################################################################################
# /book -> GET/POST -> creates a new book and when created, takes you back to
# /books (or /book/<parent_id> -- if you added a sub-book of parent_id
@@ -357,36 +354,53 @@ def new_book():
form = BookForm(request.form)
author_list = GetAuthors()
genre_list = GetGenres()
if 'title' not in request.form:
return render_template("book.html", page_title='Create new Book', b=None, books=None, book_form=form, author_list=author_list, genre_list=genre_list, alert="", message="", poss_series_list=ListOfSeriesWithMissingBooks() )
elif form.validate():
book_authors=[]
for el in request.form:
if 'author-' in el:
book_authors.append( Author.query.get( request.form[el] ) )
book_genres = []
for genre in genre_list:
if "genre-{}".format(genre.id) in request.form:
book_genres.append( genre )
book = Book( title=request.form["title"], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors )
db.session.add(book)
db.session.commit()
st.SetMessage( "Created new Book ({})".format(book.title) )
cnt=1
for field in request.form:
if 'bsl-book_id-' in field and field != 'bsl-book_id-NUM':
cnt=int(re.findall( '\d+', field )[0])
sql="insert into book_series_link (book_id, series_id, book_num) values ( {}, {}, {} )".format( book.id, request.form['bsl-series_id-{}'.format(cnt)], request.form['bsl-book_num-{}'.format(cnt)])
db.engine.execute( sql )
cnt=cnt+1
return redirect( '/book/{}'.format(book.id) )
book_authors=[]
for el in request.form:
if 'author-' in el:
book_authors.append( Author.query.get( request.form[el] ) )
book_genres = []
for genre in genre_list:
if "genre-{}".format(genre.id) in request.form:
book_genres.append( genre )
if request.method == 'POST':
if 'add_sub' in request.form:
parent=request.form['add_sub_parent_id']
book = Book.query.get(parent)
bb=QuickParentBook()
bb.parent.append( { 'id': parent, 'title': book.title } )
form.publisher.default = book.publisher
form.owned.default = book.owned
form.condition.default = book.condition
form.covertype.default = book.covertype
form.blurb.default = book.blurb
form.process()
return render_template("book.html", page_title='Create new (sub) Book', b=bb, books=None, book_form=form, author_list=author_list, genre_list=genre_list, alert="", message="", poss_series_list=ListOfSeriesWithMissingBooks() )
elif form.validate_on_submit():
book = Book( title=request.form['title'], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors )
db.session.add(book)
db.session.commit()
if 'parent_id' in request.form:
db.engine.execute( "insert into book_sub_book_link ( book_id, sub_book_id, sub_book_num ) values ( {}, {}, (select COALESCE(MAX(sub_book_num),0)+1 from book_sub_book_link where book_id = {}) )".format( request.form['parent_id'], book.id, request.form['parent_id'] ) )
db.session.commit()
st.SetMessage( "Created new Book ({})".format(book.title) )
cnt=1
for field in request.form:
if 'bsl-book_id-' in field and field != 'bsl-book_id-NUM':
cnt=int(re.findall( '\d+', field )[0])
sql="insert into book_series_link (book_id, series_id, book_num) values ( {}, {}, {} )".format( book.id, request.form['bsl-series_id-{}'.format(cnt)], request.form['bsl-book_num-{}'.format(cnt)])
db.engine.execute( sql )
cnt=cnt+1
return redirect( '/book/{}'.format(book.id) )
else:
alert="danger"
message="Failed to create Book"
for field in form.errors:
message = "{}<br>{}={}".format( message, field, form.errors[field] )
print( "ERROR: Failed to create book: {}".format(message) )
book = Book( title=request.form["title"], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors )
return render_template("book.html", page_title='Create new Book', b=None, books=book, book_form=form, author_list=author_list, genre_list=genre_list, alert=alert, message=message, poss_series_list=ListOfSeriesWithMissingBooks() )
else:
alert="danger"
message="Failed to create Book"
for field in form.errors:
message = "{}<br>{}={}".format( message, field, form.errors[field] )
print( "Failed to create book: {}".format(message) )
return render_template("book.html", page_title='Create new Book', b=None, books=None, book_form=form, author_list=author_list, genre_list=genre_list, alert=alert, message=message, poss_series_list=ListOfSeriesWithMissingBooks() )
return render_template("book.html", page_title='Create new Book', b=None, books=None, book_form=form, author_list=author_list, genre_list=genre_list, alert="success", message="", poss_series_list=ListOfSeriesWithMissingBooks() )
@app.route("/book/<id>", methods=["GET", "POST"])
@@ -423,7 +437,7 @@ def book(id):
if 'author-' in el:
book.author.append( Author.query.get( request.form[el] ) )
print( "bsl={}".format( book.bsl ))
print( "DEBUG: bsl={}".format( book.bsl ))
# delete all bsls
db.engine.execute("delete from book_series_link where book_id = {}".format( book.id ) )
cnt=1

View File

@@ -142,6 +142,7 @@ function AddAuthorToBook(num) {
<i><a href="/book/{{b.parent[0].id}}">{{b.parent[0].title}}</a></i>
</button>
</div>
<input type="hidden" name="parent_id" value="{{b.parent[0].id}}">
</div>
{% endif %}
{% for key in keys %}
@@ -208,9 +209,11 @@ function AddAuthorToBook(num) {
{% if b.parent|length and
(key == 'publisher' or key == 'owned' or key == 'covertype'
or key == 'condition' or key == 'blurb' ) %}
{{book_form[key](class="form-control", value=books[key], rows=rows, disabled="disabled" )}}
{{book_form[key](class="form-control", rows=rows, disabled="disabled" )}}
{# disabled fields do not come through in form post, so add hidden to make it work #}
<input type="hidden" name={{key}} value={{book_form[key].data}}>
{% else %}
{{book_form[key](class="form-control", value=books[key], rows=rows )}}
{{book_form[key](class="form-control", rows=rows )}}
{% endif %}
</div class="col">
{% endif %}
@@ -290,14 +293,16 @@ function AddAuthorToBook(num) {
<div class="form-row col mx-0">
{{ book_form.delete( class="btn btn-outline-danger offset-lg-2 col-lg-2" )}}
{{ book_form.submit( class="btn btn-primary col-lg-2" )}}
{% if b.parent|length == 0 %}
<input type="hidden" name="add_sub_parent_id" value="{{books.id}}">
{{ book_form.add_sub( class="btn btn-outline-success offset-lg-4 col-lg-2" )}}
{% endif %}
</div class="form-row">
</div class="form-row">
{{ hiddens.txt|safe }}
</form>
{% if b.parent|length == 0 %}
<form role="form" class="form col-lg-10" action="{{url_for('new_book')}}" method="POST">
<input type="hidden" name="add_sub_parent_id" value="{{books.id}}">
{{ book_form.add_sub( class="btn btn-outline-success offset-lg-2 col-lg-2" )}}
</form>
{% endif %}
{% if books.loan|length %}
<div class="col">
<div class="card border-primary">