From 800dcc0c25de0b5518692ed524dc3da684ddbccb Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sat, 26 Dec 2020 21:43:42 +1100 Subject: [PATCH] first-pass of deleting a book, it works, it alerts if you try to delete a parent book, nothing else really tested though --- main.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index bdb1390..fbbda5e 100644 --- a/main.py +++ b/main.py @@ -375,7 +375,7 @@ def new_book(): 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(): + 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 ) db.session.add(book) db.session.commit() @@ -396,6 +396,8 @@ def new_book(): message="Failed to create Book" for field in form.errors: message = "{}
{}={}".format( message, field, form.errors[field] ) + if len(book_genres) == 0: + message = "{}
genre=book has to have a genre selected".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 ) 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/", methods=["GET", "POST"]) def book(id): - alert="success" - message="" book_form = BookForm(request.form) if request.method == 'POST': if 'delete' in request.form: book = Book.query.get(id) - alert="danger" - message="Sorry, Deleting unsupported at present" + if len(book.child_ref) > 0: + 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(): book = Book.query.get(id) book.title = request.form['title'] @@ -451,13 +458,14 @@ def book(id): ## TODO: # what about series?, subbooks?, loan?, etc. db.session.commit() - message="Successfully Updated Book (id={})".format(id) + st.SetMesage( "Successfully Updated Book (id={})".format(id) ) else: - alert="danger" + st.SetAlert("danger") message="Failed to update Book (id={})".format(id) for field in book_form.errors: message = "{}
{}={}".format( message, field, book_form.errors[field] ) book = Book.query.get(id) + st.SetMessage(message) else: book = Book.query.get(id) book_form=BookForm(obj=book) @@ -466,7 +474,7 @@ def book(id): genre_list = GetGenres() 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 ): st="select count(id) as count from book where "