From 89966e05707a4c3021bd487dc3dff102d2a1ba12 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sat, 8 Jul 2023 14:18:22 +1000 Subject: [PATCH] made RemoveDuplicateAuthorInForm() func to remove duplicate code --- BUGs | 6 +----- main.py | 51 ++++++++++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/BUGs b/BUGs index 57dd29c..f096112 100644 --- a/BUGs +++ b/BUGs @@ -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 diff --git a/main.py b/main.py index 39d03c9..3a2894a 100644 --- a/main.py +++ b/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/ -- 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}
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}
book is not on wish list {request.form['owned']}, so needs a year_published between 1850 & 2100" + message = f"{message}
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=[]