about to really rewrite the swapping sub books, so committing what we have that partially works

This commit is contained in:
2020-11-29 13:47:07 +11:00
parent 0f1935466b
commit 5b3ee20fc2

50
main.py
View File

@@ -92,6 +92,26 @@ 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" )
def IsParent(self):
if len(self.child_ref):
return True
else:
return False
def IsChild(self):
if len(self.parent_ref):
return True
else:
return False
def FirstSubBookId(self):
# need to work out the first sub book and return an id?
return False
def LastSubBookId(self):
# need to work out the last sub book and return an id?
return False
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 )
@@ -114,9 +134,6 @@ class BookSchema(ma.SQLAlchemyAutoSchema):
child_ref = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
class Meta: model = Book
#
# To be completed
#
class BookForm(FlaskForm):
id = HiddenField()
title = StringField('Title:', [validators.DataRequired()])
@@ -175,8 +192,12 @@ def books_for_series(id):
book1_s = book_schema.dump( book1 )
# only have to check if we are a parent, as if we were a child, we
# do not have a button to even press to cause this condition
if len(book1_s['child_ref']):
print( "we want to swap a parent book" )
if book1.IsParent():
print( book1_s )
bsl_of_first_sub_of_b1=Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==bid,Book_Sub_Book_Link.sub_book_num==book1_s['child_ref'][0]['sub_book_num'] ).all()
print( bsl_of_first_sub_of_b1 )
bid=bsl_of_first_sub_of_b1[0].sub_book_id
print( "we want to swap a parent book - we have {} subbooks, and subbook in series is: {}".format( len(book1_s['child_ref']), bid ))
bsl1=Book_Series_Link.query.filter(Book_Series_Link.series_id==id, Book_Series_Link.book_id==bid).all()
print( bsl1[0].book_num )
if dir == "up":
@@ -190,13 +211,11 @@ def books_for_series(id):
book2 = Book.query.get(bsl2[0].book_id)
book2_s = book_schema.dump( book2 )
print( book2_s )
if len(book2_s['child_ref']):
print( "swapping with a parent book!" )
if len(book2_s['parent_ref']):
print( "swapping with a parent book! (and the 'next' book is a child book)" )
bsl2[0].book_num=bsl1[0].book_num
bsl1[0].book_num=other_bn
db.session.commit()
print( "swapping with a parent book! (as the next book in the series is a child book)" )
# bsl2[0].book_num=bsl1[0].book_num
# bsl1[0].book_num=other_bn
# db.session.commit()
books = Book.query.join(Book_Series_Link).filter(Book_Series_Link.series_id==id).all()
series = Series.query.get(id)
return render_template("books_for_series.html", books=books, series=series)
@@ -206,12 +225,9 @@ def book(id):
book = Book.query.get(id)
book_s = book_schema.dump(book)
######
###
### okay, this manual hacking of sub_book is currently going to be needed, because in the jinja2 I want to list the book, and more than just the id number of the sub_book, I want the details... (sub_book_schema needs a book relationship BUT, dependencies mean I can't define a book schema inside sub_book schema, and I am definitely not sure how to join it anyway... for another time.
###
#####
# force sub books for jinja2 to be able to use
####################################
# 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 ) )
sub_book=[]
for row in subs: