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

This commit is contained in:
2020-11-30 21:59:50 +11:00
parent 5cc03fe7e7
commit ce34f68254
3 changed files with 78 additions and 27 deletions

6
README
View File

@@ -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)

54
main.py
View File

@@ -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 "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".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 "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}, parent: {}>".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/<id>", methods=["GET"])
def book(id):
book = Book.query.get(id)
book_s = book_schema.dump(book)
@app.route("/subbooks_for_book/<id>", 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/<id>", 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():

View File

@@ -1,7 +1,6 @@
{% extends "base.html" %}
{% block main_content %}
<h3><center>View/Edit Book</center></h1>
{% set keys = [ 'title', 'author', 'publisher', 'genre', 'owned', 'covertype', 'condition', 'year_published', 'rating', 'notes', 'blurb' ] %}
<div class="container-fluid"><div class="row"><form class="form col-lg-10">
@@ -80,7 +79,11 @@
</div class="form-row">
{% else %}
<div class="col">
{{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 )}}
</div class="col">
{% endif %}
</div class="form-row">
@@ -91,19 +94,32 @@
<table>
{% for s in books.series %}
<tr><td>
Book {{ SeriesBookNum( s.id, books.id ) }} of {{s.num_books}} in <a href="/series/{{s.id}}">{{s.title}}</a>
{% if SeriesBookNum( s.id, books.id ) %}
<label class="form-control-plaintext">Book {{ SeriesBookNum( s.id, books.id ) }} of {{s.num_books}} in <a href="/series/{{s.id}}">{{s.title}}</a></label>
{% else %}
<label class="form-control-plaintext">Contains books in <a href='/series/{{s.id}}'>{{s.title}}</a></label>
{% endif %}
</td></tr>
{% endfor %}
</table>
</div>
{% endif %}
{% if books.child_ref|length %}
<div class="form-row">
<label class="col-lg-2 col-form-label">Sub Books:</label>
<div class="col" id="sub_book_content">
</div>
</div>
{% endif %}
{% if books.parent_ref|length %}
<div class="form-row">
<label class="col-lg-2 col-form-label">Parent Book:</label>
<div class="col">
<label class="form-control-plaintext"><a href="/book/{{b.parent[0].id}}">{{b.parent[0].title}}</a></label>
</div>
</div>
{% endif %}
</form>
{% endif %}
{% if books.sub_book|length %}
<p>sub_book is defined: {{books.sub_book}}</p>
{% endif %}
{% if books.subs|length %}
<p>subs is defined {{ books.subs }}</p>
{% endif %}
{% if books.loan|length %}
<div class="col">
<div class="card border-primary" style="max-width: 18rem;">
@@ -122,3 +138,12 @@
</div class="container">
{% endblock main_content %}
{% if books.child_ref|length %}
{% block script_content %}
<script>
$(document).ready( function() {
$("#sub_book_content").load("/subbooks_for_book/{{books.id}}")
} )
</script>
{% endblock script_content %}
{% endif %}