made RemoveDuplicateAuthorInForm() func to remove duplicate code
This commit is contained in:
51
main.py
51
main.py
@@ -557,6 +557,29 @@ def remove_sub_book():
|
||||
st.SetMessage( e.orig )
|
||||
return redirect( '/book/{}'.format(request.form['rem_sub_sub_book_id']) )
|
||||
|
||||
################################################################################
|
||||
# Go through request.form, find author's and remove any that are duplicates as
|
||||
# that is not allowed / DB integrity issue. This just sets a message that
|
||||
# will allow the book to be create/saved still, but the duplicate author will
|
||||
# be removed
|
||||
################################################################################
|
||||
def RemoveDuplicateAuthorInForm( request ):
|
||||
processed=[]
|
||||
ret=[]
|
||||
cnt=1
|
||||
message=""
|
||||
|
||||
for el in request.form:
|
||||
if 'author-' in el:
|
||||
if not request.form[el] in processed:
|
||||
ret.append( Book_Author_Link( author_id=request.form[el], author_num=cnt ) )
|
||||
processed.append( request.form[el] )
|
||||
cnt+=1
|
||||
else:
|
||||
message="Removed duplicate Author!!!"
|
||||
return ret, message
|
||||
|
||||
|
||||
################################################################################
|
||||
# /book -> GET/POST -> creates a new book and when created, takes you back to
|
||||
# /books (or /book/<parent_id> -- if you added a sub-book of parent_id
|
||||
@@ -576,20 +599,10 @@ def new_book():
|
||||
book_genres = []
|
||||
|
||||
bals=[]
|
||||
auth_cnt=1
|
||||
processed_bal=[]
|
||||
message=""
|
||||
|
||||
if request.method == 'POST':
|
||||
# handle author info for new book
|
||||
for el in request.form:
|
||||
if 'author-' in el:
|
||||
if not request.form[el] in processed_bal:
|
||||
bals.append( Book_Author_Link( author_id=request.form[el], author_num=auth_cnt ) )
|
||||
processed_bal.append( request.form[el] )
|
||||
auth_cnt+=1
|
||||
else:
|
||||
message="Removed duplicate author!!!"
|
||||
bals, message = RemoveDuplicateAuthorInForm( request )
|
||||
|
||||
# handle genre info for new book
|
||||
for genre in genre_list:
|
||||
@@ -666,7 +679,7 @@ def new_book():
|
||||
if len(book_genres) == 0:
|
||||
message = f"{message}<br>book has to have a genre selected"
|
||||
if int(request.form['owned']) != int(ON_WISHLIST) and not request.form['year_published'].isnumeric():
|
||||
message = f"{message}<br>book is not on wish list {request.form['owned']}, so needs a year_published between 1850 & 2100"
|
||||
message = f"{message}<br>book is not on wish list, so needs a year_published between 1850 & 2100"
|
||||
|
||||
print( "ERROR: Failed to create book: {}".format(message) )
|
||||
if request.form['year_published'].isnumeric():
|
||||
@@ -734,8 +747,6 @@ def book(id):
|
||||
book_form.rating.choices=[(c.id, c.name) for c in Rating.query.order_by('id')]
|
||||
page_title='Edit Book'
|
||||
CheckSeriesChange=None
|
||||
processed_bal=[]
|
||||
alert="success"
|
||||
message=""
|
||||
|
||||
if request.method == 'POST':
|
||||
@@ -768,17 +779,7 @@ def book(id):
|
||||
book.genre.append( genre )
|
||||
# set book author (empty list) - cant use ORM, not sure why
|
||||
ClearAuthorsForBook( book.id )
|
||||
# then use form data -> author-0, author-1, ... author-n) & append them back to now empty list
|
||||
cnt=1
|
||||
for el in request.form:
|
||||
if 'author-' in el:
|
||||
if not request.form[el] in processed_bal:
|
||||
book.bals.append( Book_Author_Link( author_id=request.form[el], book_id=id, author_num=cnt ) )
|
||||
processed_bal.append( request.form[el] )
|
||||
cnt += 1
|
||||
else:
|
||||
alert="warning"
|
||||
message="removed duplicate author!!!"
|
||||
book.bals, message = RemoveDuplicateAuthorInForm( request )
|
||||
|
||||
# go through form, if we have removed a series, then copy data out of form to be passed into html for a pop-up
|
||||
removing_series=[]
|
||||
|
||||
Reference in New Issue
Block a user