fixed BUG-48 - sub book add/rem -- it was because QuickParentBook used an array and the code had been half converted to just use an object. Fixed to all be an object/dict now and its working again
This commit is contained in:
48
main.py
48
main.py
@@ -1,6 +1,6 @@
|
||||
from flask import Flask, render_template, request, redirect, jsonify, url_for
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from sqlalchemy import desc
|
||||
from sqlalchemy import desc, text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from flask_marshmallow import Marshmallow
|
||||
from flask_bootstrap import Bootstrap
|
||||
@@ -77,7 +77,15 @@ CONDITION_NOT_APPLICABLE=4
|
||||
|
||||
####################################### CLASSES / DB model #######################################
|
||||
class QuickParentBook:
|
||||
parent=[]
|
||||
|
||||
def __init__(self):
|
||||
self.parent={}
|
||||
self.publisher=-1
|
||||
self.owned=-1
|
||||
self.covertype=-1
|
||||
self.condition=-1
|
||||
self.blurb=''
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return "<parent: {}, publisher: {}, owned: {}, covertype: {}, condition: {}, blurb: {}>".format(self.parent, self.publisher, self.owned, self.covertype, self.condition, self.blurb )
|
||||
@@ -292,8 +300,7 @@ def GetBookIdFromBookSubBookLinkByIdAndSubBookNum( book_id, sub_book_num ):
|
||||
# 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
|
||||
def AddSubs(books):
|
||||
with db.engine.connect() as conn:
|
||||
subs = conn.exec_driver_sql( "select * from book_sub_book_link" )
|
||||
subs=db.session.execute( text( "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)
|
||||
if index == -1:
|
||||
@@ -304,8 +311,7 @@ def AddSubs(books):
|
||||
# HACK: Couldn't work out ORM to excluded sub_book self-ref, so using basic python
|
||||
# loop to remove sub_books from list
|
||||
def RemSubs(books):
|
||||
with db.engine.connect() as conn:
|
||||
subs = conn.exec_driver_sql( "select * from book_sub_book_link" )
|
||||
subs=db.session.execute( text( "select * from book_sub_book_link" ) )
|
||||
for row in subs:
|
||||
for i, item in enumerate(books):
|
||||
if item.id == row.sub_book_id:
|
||||
@@ -638,12 +644,18 @@ def new_book():
|
||||
if "genre-{}".format(genre.id) in request.form:
|
||||
book_genres.append( genre )
|
||||
|
||||
print( request.form )
|
||||
|
||||
# handle creating a new sub-book of an existing book (add_sub_parent_id) - html / with form data for the new book...
|
||||
if 'add_sub' in request.form:
|
||||
print( "here" )
|
||||
print( request.form['add_sub_parent_id'] )
|
||||
book=Book.query.get(request.form['add_sub_parent_id'])
|
||||
print( book )
|
||||
bb=QuickParentBook()
|
||||
bb.parent=[]
|
||||
bb.parent.append( { 'id': book.id, 'title': book.title } )
|
||||
print( bb )
|
||||
|
||||
bb.parent={ 'id': book.id, 'title': book.title }
|
||||
form.publisher.default = book.publisher
|
||||
form.owned.default = book.owned
|
||||
form.condition.default = book.condition
|
||||
@@ -668,9 +680,10 @@ def new_book():
|
||||
if 'parent_id' in request.form:
|
||||
parent=Book.query.get(request.form['parent_id'])
|
||||
max_bsbl = Book_Sub_Book_Link.query.filter(Book_Sub_Book_Link.book_id==parent.id).order_by(desc(Book_Sub_Book_Link.sub_book_num)).first()
|
||||
max_sbn=max_bsbl.sub_book_num
|
||||
if max_sbn == None:
|
||||
if max_bsbl == None:
|
||||
max_sbn=0
|
||||
else:
|
||||
max_sbn=max_bsbl.sub_book_num
|
||||
new_bsbl = Book_Sub_Book_Link( book_id=parent.id, sub_book_id=book.id, sub_book_num=max_sbn+1 )
|
||||
db.session.add(new_bsbl)
|
||||
if len(parent.series) > 0:
|
||||
@@ -710,8 +723,7 @@ def new_book():
|
||||
|
||||
if 'parent_id' in request.form:
|
||||
bb=QuickParentBook()
|
||||
bb.parent=[]
|
||||
bb.parent.append( { 'id': request.form['parent_id'], 'title': request.form['parent_title'] } )
|
||||
bb.parent= { 'id': request.form['parent_id'], 'title': request.form['parent_title'] }
|
||||
else:
|
||||
bb=None
|
||||
return render_template("book.html", page_title=page_title, b=bb, books=book, book_form=form, author_list=author_list, genre_list=genre_list, alert=alert, message=message, poss_series_list=ListOfSeriesWithMissingBooks() )
|
||||
@@ -777,7 +789,7 @@ def book(id):
|
||||
# happens in error conditions only
|
||||
return redirect( '/' )
|
||||
else:
|
||||
# could return to parent book, or current book depending on what was delted
|
||||
# could return to parent book, or current book depending on what was deleted
|
||||
return redirect( f"/book/{redirect_to}" )
|
||||
# save/update of book
|
||||
elif book_form.validate():
|
||||
@@ -864,7 +876,9 @@ def book(id):
|
||||
book = Book.query.get(id)
|
||||
st.SetMessage(message)
|
||||
else:
|
||||
print( f"getting book id: {id}" )
|
||||
book = Book.query.get(id)
|
||||
print( book )
|
||||
if book == None:
|
||||
st.SetAlert("danger")
|
||||
st.SetMessage("Cannot find Book (id={})".format(id))
|
||||
@@ -885,8 +899,9 @@ def book(id):
|
||||
|
||||
def GetCount( what, where ):
|
||||
st="select count(id) as count from book where "
|
||||
with db.engine.connect() as conn:
|
||||
res = conn.exec_driver_sql( st+where )
|
||||
# with db.engine.connect() as conn:
|
||||
# res = conn.exec_driver_sql( st+where )
|
||||
res = db.session.execute( text( st+where ) )
|
||||
rtn={}
|
||||
for row in res:
|
||||
rtn['stat']=what
|
||||
@@ -1013,8 +1028,7 @@ def unrated_books():
|
||||
return render_template("books.html", books=books, page_title="Books with no rating", show_cols='Rating', hide_cols='' )
|
||||
|
||||
def FindMissingBooks():
|
||||
with db.engine.connect() as conn:
|
||||
tmp=conn.exec_driver_sql( "select s.*, count(bsl.book_num) from book_series_link bsl, series s where bsl.book_num is not null and s.id = bsl.series_id group by s.id order by s.title")
|
||||
tmp=db.session.execute(text( "select s.*, count(bsl.book_num) from book_series_link bsl, series s where bsl.book_num is not null and s.id = bsl.series_id group by s.id order by s.title") )
|
||||
books=[]
|
||||
sold=Owned.query.filter(Owned.name=='Sold').all()
|
||||
for t in tmp:
|
||||
|
||||
Reference in New Issue
Block a user