made RemoveDuplicateAuthorInForm() func to remove duplicate code

This commit is contained in:
2023-07-08 14:18:22 +10:00
parent 59df1accbf
commit 89966e0570
2 changed files with 27 additions and 30 deletions

6
BUGs
View File

@@ -1,11 +1,7 @@
#### BUGS (next-46)
BUG-41: failed to add a book with 2 x same series - implement same fix as for duplicate authors (search for processed_bal) - should
actually turn processed_bal into a function, as its used twice, and maybe able to make it 4 times for series as well if think hard enough.
e.g.
bals, message = RemoveDuplicatesFromForm( what )
# what == "author" or "series" and then hardcode 'author-' or 'book-bsl-'
ALSO stop being inconsistent its bal or bals?
actually turn processed_bal into a function, as its used twice, then do similar for series
BUG-42: bug-7 removed as bug-41 covers the more useful version of this AND what we also need (this bug) is not allowing adding a
sub_book_num for a series that has already been taken -- not sure the DB enforces either

51
main.py
View File

@@ -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=[]