From 61629760cdbf22109394a57b218e635236e0ecc6 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sat, 26 Dec 2020 19:11:43 +1100 Subject: [PATCH] adding a sub-book now works --- README | 2 - main.py | 92 ++++++++++++++++++++++++++------------------- templates/book.html | 17 ++++++--- 3 files changed, 64 insertions(+), 47 deletions(-) diff --git a/README b/README index 5cb9764..630513a 100644 --- a/README +++ b/README @@ -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/ .. 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 diff --git a/main.py b/main.py index 113b547..bdb1390 100644 --- a/main.py +++ b/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 "".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/ -- 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 = "{}
{}={}".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 = "{}
{}={}".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/", 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 diff --git a/templates/book.html b/templates/book.html index 9cdfc7d..036875d 100644 --- a/templates/book.html +++ b/templates/book.html @@ -142,6 +142,7 @@ function AddAuthorToBook(num) { {{b.parent[0].title}} + {% 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 #} + {% else %} - {{book_form[key](class="form-control", value=books[key], rows=rows )}} + {{book_form[key](class="form-control", rows=rows )}} {% endif %} {% endif %} @@ -290,14 +293,16 @@ function AddAuthorToBook(num) {
{{ 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 %} - - {{ book_form.add_sub( class="btn btn-outline-success offset-lg-4 col-lg-2" )}} - {% endif %}
{{ hiddens.txt|safe }} + {% if b.parent|length == 0 %} +
+ + {{ book_form.add_sub( class="btn btn-outline-success offset-lg-2 col-lg-2" )}} +
+ {% endif %} {% if books.loan|length %}