first-pass of deleting a book, it works, it alerts if you try to delete a parent book, nothing else really tested though

This commit is contained in:
2020-12-26 21:43:42 +11:00
parent 61629760cd
commit 800dcc0c25

24
main.py
View File

@@ -375,7 +375,7 @@ def new_book():
form.blurb.default = book.blurb form.blurb.default = book.blurb
form.process() 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() ) 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(): elif form.validate_on_submit() and len(book_genres):
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 ) 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.add(book)
db.session.commit() db.session.commit()
@@ -396,6 +396,8 @@ def new_book():
message="Failed to create Book" message="Failed to create Book"
for field in form.errors: for field in form.errors:
message = "{}<br>{}={}".format( message, field, form.errors[field] ) message = "{}<br>{}={}".format( message, field, form.errors[field] )
if len(book_genres) == 0:
message = "{}<br>genre=book has to have a genre selected".format( message )
print( "ERROR: Failed to create book: {}".format(message) ) 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 ) 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() ) 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() )
@@ -405,14 +407,19 @@ def new_book():
@app.route("/book/<id>", methods=["GET", "POST"]) @app.route("/book/<id>", methods=["GET", "POST"])
def book(id): def book(id):
alert="success"
message=""
book_form = BookForm(request.form) book_form = BookForm(request.form)
if request.method == 'POST': if request.method == 'POST':
if 'delete' in request.form: if 'delete' in request.form:
book = Book.query.get(id) book = Book.query.get(id)
alert="danger" if len(book.child_ref) > 0:
message="Sorry, Deleting unsupported at present" st.SetAlert( "danger" )
st.SetMessage( "This is a parent book, cannot delete it without deleting sub books first" )
return redirect( '/book/{}'.format(book.id) )
else:
db.session.delete(book)
db.session.commit()
st.SetAlert("warning")
st.SetMessage("WARNING: Deleting being tested at present")
elif book_form.validate(): elif book_form.validate():
book = Book.query.get(id) book = Book.query.get(id)
book.title = request.form['title'] book.title = request.form['title']
@@ -451,13 +458,14 @@ def book(id):
## TODO: ## TODO:
# what about series?, subbooks?, loan?, etc. # what about series?, subbooks?, loan?, etc.
db.session.commit() db.session.commit()
message="Successfully Updated Book (id={})".format(id) st.SetMesage( "Successfully Updated Book (id={})".format(id) )
else: else:
alert="danger" st.SetAlert("danger")
message="Failed to update Book (id={})".format(id) message="Failed to update Book (id={})".format(id)
for field in book_form.errors: for field in book_form.errors:
message = "{}<br>{}={}".format( message, field, book_form.errors[field] ) message = "{}<br>{}={}".format( message, field, book_form.errors[field] )
book = Book.query.get(id) book = Book.query.get(id)
st.SetMessage(message)
else: else:
book = Book.query.get(id) book = Book.query.get(id)
book_form=BookForm(obj=book) book_form=BookForm(obj=book)
@@ -466,7 +474,7 @@ def book(id):
genre_list = GetGenres() genre_list = GetGenres()
book_s = book_schema.dump(book) book_s = book_schema.dump(book)
return render_template("book.html", b=book, books=book_s, book_form=book_form, author_list=author_list, genre_list=genre_list, alert=alert, message=message, poss_series_list=ListOfSeriesWithMissingBooks() ) return render_template("book.html", b=book, books=book_s, book_form=book_form, author_list=author_list, genre_list=genre_list, alert=st.GetAlert(), message=st.GetMessage(), poss_series_list=ListOfSeriesWithMissingBooks() )
def GetCount( what, where ): def GetCount( what, where ):
st="select count(id) as count from book where " st="select count(id) as count from book where "