rewrote to remove parent_ref and just use parent from ORM, added POST for book/id route, so we can now error on delete and save title only - baby steps -- with alert/message feedback, and also removed debugs

This commit is contained in:
2020-12-01 20:28:22 +11:00
parent 136be26ad0
commit 7e67f5bb6f

35
main.py
View File

@@ -89,10 +89,11 @@ class Book(db.Model):
created = db.Column(db.Date) created = db.Column(db.Date)
modified = db.Column(db.Date) modified = db.Column(db.Date)
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" ) # take actual parent book as there is no real associated sub_book_num data and can just use it
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" )
# but use child_ref as sub_book_num is per book, and I can't connect an empty array of sub_book_nums to a child book array in "child"
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" ) 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): def IsParent(self):
if len(self.child_ref): if len(self.child_ref):
@@ -101,7 +102,7 @@ class Book(db.Model):
return False return False
def IsChild(self): def IsChild(self):
if len(self.parent_ref): if len(self.parent):
return True return True
else: else:
return False return False
@@ -127,7 +128,6 @@ class Book(db.Model):
return 1 return 1
def MoveBookInSeries( self, series_id, amt ): def MoveBookInSeries( self, series_id, amt ):
print( "Moving {} by {}".format( self.title, amt ) )
if self.IsParent(): if self.IsParent():
tmp_book=book_schema.dump(self) tmp_book=book_schema.dump(self)
for book in tmp_book['child_ref']: for book in tmp_book['child_ref']:
@@ -158,7 +158,6 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
genre = ma.Nested(GenreSchema, many=True) genre = ma.Nested(GenreSchema, many=True)
loan = ma.Nested(LoanSchema, many=True) loan = ma.Nested(LoanSchema, many=True)
series = ma.Nested(SeriesSchema, many=True) series = ma.Nested(SeriesSchema, many=True)
parent_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True) child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
class Meta: model = Book class Meta: model = Book
@@ -175,6 +174,8 @@ class BookForm(FlaskForm):
rating = SelectField( 'rating', choices=[(c.id, c.name) for c in Rating.query.order_by('id')] ) rating = SelectField( 'rating', choices=[(c.id, c.name) for c in Rating.query.order_by('id')] )
notes = TextAreaField('Notes:') notes = TextAreaField('Notes:')
blurb = TextAreaField('Blurb:') blurb = TextAreaField('Blurb:')
submit = SubmitField('Save' )
delete = SubmitField('Delete' )
# allow jinja2 to call this python function # allow jinja2 to call this python function
@@ -208,8 +209,9 @@ def GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book_id, sub_book_num ):
def books(): def books():
books = Book.query.all() books = Book.query.all()
# ignore ORM, its too slow. Just select sub_book data and hand add it to # ignore ORM, the sub_book_num comes from the link, but does not have any attached data, so can't tell which book it connects to.
# the books object, and use it in jinja2 to indent/order the books/sub books # Just select sub_book data and hand add it to the books object, and use it in jinja2 to indent/order the books/sub books
# This data is used to sort/indent subbooks
subs = db.engine.execute ( "select * from book_sub_book_link" ) subs = db.engine.execute ( "select * from book_sub_book_link" )
for row in subs: for row in subs:
index = next((i for i, item in enumerate(books) if item.id == row.sub_book_id), -1) index = next((i for i, item in enumerate(books) if item.id == row.sub_book_id), -1)
@@ -262,7 +264,7 @@ def books_for_series(id):
# if book2 is a sub book, then we need its parent as book2 instead, as we have to move the whole book as one # if book2 is a sub book, then we need its parent as book2 instead, as we have to move the whole book as one
if book2.IsChild(): if book2.IsChild():
book2 = Book.query.get( book2.parent_ref[0].book_id ) book2 = Book.query.get( book2.parent[0].id )
# By here: book1 is parent or normal book we are swapping with book2 which is parent or normal book # By here: book1 is parent or normal book we are swapping with book2 which is parent or normal book
b1_numsb = book1.NumSubBooks() b1_numsb = book1.NumSubBooks()
@@ -287,7 +289,6 @@ def books_for_series(id):
@app.route("/subbooks_for_book/<id>", methods=["GET", "POST"]) @app.route("/subbooks_for_book/<id>", methods=["GET", "POST"])
def subbooks_for_book(id): def subbooks_for_book(id):
print( "called subbooks_for_book: {}".format(id) )
if request.method == 'POST': if request.method == 'POST':
if 'move_button' in request.form: if 'move_button' in request.form:
# split form's pressed move_button to yield: dir ("up" or "down") and bid1 = book_id of subbook # split form's pressed move_button to yield: dir ("up" or "down") and bid1 = book_id of subbook
@@ -325,9 +326,21 @@ def subbooks_for_book(id):
return render_template("subbooks_for_book.html", sub_books=sub_book, s2=subs ) return render_template("subbooks_for_book.html", sub_books=sub_book, s2=subs )
@app.route("/book/<id>", methods=["GET"]) @app.route("/book/<id>", methods=["GET", "POST"])
def book(id): def book(id):
alert="success"
message=""
book_form = BookForm(request.form)
book = Book.query.get(id) book = Book.query.get(id)
if request.method == 'POST' and book_form.validate():
if 'delete' in request.form:
alert="danger"
message="Sorry, Deleting unsupported at present"
else:
book.title = request.form['title']
db.session.commit()
message="Successfully Updated Book (id={})".format(id)
book_s = book_schema.dump(book) book_s = book_schema.dump(book)
book_form=BookForm(request.form) book_form=BookForm(request.form)
@@ -340,7 +353,7 @@ def book(id):
author_list = GetAuthors() author_list = GetAuthors()
genre_list = GetGenres() genre_list = GetGenres()
publisher_list = GetPublishers() publisher_list = GetPublishers()
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 ) 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, alert=alert, message=message )
@app.route("/", methods=["GET"]) @app.route("/", methods=["GET"])
def main_page(): def main_page():