adding a sub-book now works
This commit is contained in:
92
main.py
92
main.py
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user