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:
35
main.py
35
main.py
@@ -89,10 +89,11 @@ class Book(db.Model):
|
||||
created = 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" )
|
||||
|
||||
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):
|
||||
@@ -101,7 +102,7 @@ class Book(db.Model):
|
||||
return False
|
||||
|
||||
def IsChild(self):
|
||||
if len(self.parent_ref):
|
||||
if len(self.parent):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
@@ -127,7 +128,6 @@ class Book(db.Model):
|
||||
return 1
|
||||
|
||||
def MoveBookInSeries( self, series_id, amt ):
|
||||
print( "Moving {} by {}".format( self.title, amt ) )
|
||||
if self.IsParent():
|
||||
tmp_book=book_schema.dump(self)
|
||||
for book in tmp_book['child_ref']:
|
||||
@@ -158,7 +158,6 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
genre = ma.Nested(GenreSchema, many=True)
|
||||
loan = ma.Nested(LoanSchema, 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)
|
||||
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')] )
|
||||
notes = TextAreaField('Notes:')
|
||||
blurb = TextAreaField('Blurb:')
|
||||
submit = SubmitField('Save' )
|
||||
delete = SubmitField('Delete' )
|
||||
|
||||
|
||||
# allow jinja2 to call this python function
|
||||
@@ -208,8 +209,9 @@ def GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book_id, sub_book_num ):
|
||||
def books():
|
||||
books = Book.query.all()
|
||||
|
||||
# ignore ORM, its too slow. 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
|
||||
# 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.
|
||||
# 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" )
|
||||
for row in subs:
|
||||
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.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
|
||||
b1_numsb = book1.NumSubBooks()
|
||||
@@ -287,7 +289,6 @@ def books_for_series(id):
|
||||
|
||||
@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
|
||||
@@ -325,9 +326,21 @@ def subbooks_for_book(id):
|
||||
|
||||
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):
|
||||
alert="success"
|
||||
message=""
|
||||
book_form = BookForm(request.form)
|
||||
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_form=BookForm(request.form)
|
||||
@@ -340,7 +353,7 @@ def book(id):
|
||||
author_list = GetAuthors()
|
||||
genre_list = GetGenres()
|
||||
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"])
|
||||
def main_page():
|
||||
|
||||
Reference in New Issue
Block a user