From ce34f682546a70a268d997542081a403a4c04abc Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Mon, 30 Nov 2020 21:59:50 +1100 Subject: [PATCH] added html for subbooks, made up/down buttons that work too. Also put back reference from subbook to parent book, via ORM data - need to investigate more, but it all works --- README | 6 ++--- main.py | 54 +++++++++++++++++++++++++++++++++------------ templates/book.html | 45 ++++++++++++++++++++++++++++--------- 3 files changed, 78 insertions(+), 27 deletions(-) diff --git a/README b/README index df2e98a..2946902 100644 --- a/README +++ b/README @@ -25,9 +25,9 @@ python3 main.py ### TODO: -- ORM: - - saving a book with the ORM is probably the last oddity... probably time to try it (in a sense, the book edit page is the only time we change things, maybe we mess with that book-query as its too slow, and for book edit, if we don't allow editing items like author/publisher, then may only need to set the objects we want to change... I'm sort of curious can you save a book table based on id, without saging its author table, seems that should be okay, but how do I save both, just create 2 objects and do it, but what if the ORM has auto-connected bunches of stuff, how do I update the connected stuff.... MAYBE, I need to get say loan or something first, then try all this? (make a pop-up for stuff like loans, but what about series or sub-book editing????) - - have no html for sub_book + - ORM: okay, could have child and parent references in book, would that remove need for hand-sql of subs? + * see code for subbooks_for_book ('parent')... + * if so, book.html has books (from schema.dump(book), could just be book with parent/child?) - need to save a book - need to create all classes (green + button) - need to delete all classes (and watch for referential integrity) diff --git a/main.py b/main.py index 10963c5..09b57c5 100644 --- a/main.py +++ b/main.py @@ -92,6 +92,8 @@ class Book(db.Model): parent_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" ) child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id" ) + parent = db.relationship('Book', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" ) + def IsParent(self): if len(self.child_ref): return True @@ -139,7 +141,7 @@ class Book(db.Model): return def __repr__(self): - return "".format(self.id, self.author, self.title, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher ) + return "".format(self.id, self.author, self.title, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher, self.parent ) class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema): class Meta: model = Book_Sub_Book_Link @@ -283,26 +285,50 @@ def books_for_series(id): series = Series.query.get(id) return render_template("books_for_series.html", books=books, series=series) -@app.route("/book/", methods=["GET"]) -def book(id): - book = Book.query.get(id) - book_s = book_schema.dump(book) +@app.route("/subbooks_for_book/", methods=["GET", "POST"]) +def subbooks_for_book(id): + print( "called subbooks_for_book: {}".format(id) ) + if request.method == 'POST': + if 'move_button' in request.form: + # split form's pressed move_button to yield: dir ("up" or "down") and bid1 = book_id of subbook + dir, bid1 = request.form['move_button'].split('-') + bsbl1 = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==id, Book_Sub_Book_Link.sub_book_id==bid1 ).all() + if dir == "up": + swap_with_num = bsbl1[0].sub_book_num-1 + else: + swap_with_num = bsbl1[0].sub_book_num+1 + bid2=GetBookIdFromBookSubBookLinkByIdAndSubBookNum( id, swap_with_num ) + bsbl2 = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==id, Book_Sub_Book_Link.sub_book_id==bid2 ).all() + + # swap the 2 books (by switching sub_book_nums) + tmp=bsbl1[0].sub_book_num + bsbl1[0].sub_book_num = bsbl2[0].sub_book_num + bsbl2[0].sub_book_num = tmp + db.session.commit() + #################################### # force sub books for jinja2 to be able to use (ORM is not giving all the details #################################### - subs = db.engine.execute ( "select bsb.book_id, bsb.sub_book_id, bsb.sub_book_num, book.title, book.rating, book.year_published, book.notes, bal.author_id as author_id, author.surname||', '||author.firstnames as author from book_sub_book_link bsb, book, book_author_link bal, author where bsb.book_id = {} and book.id = bsb.sub_book_id and book.id = bal.book_id and bal.author_id = author.id".format( id ) ) + subs = db.engine.execute ( "select bsb.book_id, bsb.sub_book_id, bsb.sub_book_num, book.title, \ + r.name as rating, book.year_published, book.notes, \ + bal.author_id as author_id, author.surname||', '||author.firstnames as author \ + from book_sub_book_link bsb, book, book_author_link bal, author, rating r\ + where bsb.book_id = {} and book.id = bsb.sub_book_id and book.id = bal.book_id and \ + bal.author_id = author.id and r.id = book.rating \ + order by bsb.sub_book_num".format( id ) ) sub_book=[] for row in subs: - # get genres for sub book and add by hand first - tmp_g = [] - genres = db.engine.execute ( "select genre.id, genre.name from genre, book_genre_link bgl where genre.id = bgl.genre_id and bgl.book_id = {}".format( row.sub_book_id ) ) - for genre in genres: - tmp_g.append( { 'id': genre.id, 'name': genre.name } ) + sub_book.append( { 'book_id': row.book_id, 'sub_book_id': row.sub_book_id, \ + 'sub_book_num': row.sub_book_num, 'title' : row.title, 'rating': row.rating,\ + 'year_published' : row.year_published, 'notes' : row.notes, 'author_id' : row.author_id, 'author' : row.author } ) - sub_book.append( { 'sub_book_id': row.sub_book_id, 'sub_book_num': row.sub_book_num, 'title' : row.title, 'rating': row.rating, 'year_published' : row.year_published, 'notes' : row.notes, 'author_id' : row.author_id, 'author' : row.author, 'genres' : tmp_g } ) + return render_template("subbooks_for_book.html", sub_books=sub_book, s2=subs ) - book_s['sub_book'] = sub_book +@app.route("/book/", methods=["GET"]) +def book(id): + book = Book.query.get(id) + book_s = book_schema.dump(book) book_form=BookForm(request.form) # set defaults for drop-down's based on this book @@ -314,7 +340,7 @@ def book(id): author_list = GetAuthors() genre_list = GetGenres() publisher_list = GetPublishers() - return render_template("book.html", books=book_s, subs=sub_book, book_form=book_form, author_list=author_list, publisher_list=publisher_list, genre_list=genre_list ) + return render_template("book.html", b=book, books=book_s, book_form=book_form, author_list=author_list, publisher_list=publisher_list, genre_list=genre_list ) @app.route("/", methods=["GET"]) def main_page(): diff --git a/templates/book.html b/templates/book.html index f9fc424..06b9b56 100644 --- a/templates/book.html +++ b/templates/book.html @@ -1,7 +1,6 @@ {% extends "base.html" %} {% block main_content %} -

View/Edit Book

{% set keys = [ 'title', 'author', 'publisher', 'genre', 'owned', 'covertype', 'condition', 'year_published', 'rating', 'notes', 'blurb' ] %}
@@ -80,7 +79,11 @@
{% else %}
- {{book_form[key](class="form-control", value=books[key], rows="5" )}} + {% set rows=4 %} + {% if key == "notes" %} + {% set rows=2 %} + {% endif %} + {{book_form[key](class="form-control", value=books[key], rows=rows )}}
{% endif %}
@@ -91,19 +94,32 @@ {% for s in books.series %} {% endfor %}
- Book {{ SeriesBookNum( s.id, books.id ) }} of {{s.num_books}} in {{s.title}} + {% if SeriesBookNum( s.id, books.id ) %} + + {% else %} + + {% endif %}
+ {% endif %} + {% if books.child_ref|length %} +
+ +
+
+
+ {% endif %} + {% if books.parent_ref|length %} +
+ + +
+ {% endif %} - {% endif %} - {% if books.sub_book|length %} -

sub_book is defined: {{books.sub_book}}

- {% endif %} - {% if books.subs|length %} -

subs is defined {{ books.subs }}

- {% endif %} {% if books.loan|length %}
@@ -122,3 +138,12 @@
{% endblock main_content %} +{% if books.child_ref|length %} + {% block script_content %} + + {% endblock script_content %} +{% endif %}