From ae2453d0735666633ffa4e1801813d1f6f63a16d Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Tue, 13 Jun 2023 18:53:03 +1000 Subject: [PATCH] fixed BUGS 28/29 -> can now have empty year_published, fixed up limits to 1850-2100 now too and better error messages. Also auto-change when choose wishlist --- BUGs | 2 -- main.py | 56 +++++++++++++++++++++++++++++++++++++-------- templates/book.html | 1 + 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/BUGs b/BUGs index 3aa4b88..b97d740 100644 --- a/BUGs +++ b/BUGs @@ -2,8 +2,6 @@ #### BUGS (next-30) BUG-27: created a new book, with a series, series was not added -BUG-28: choosing on wish list, does not alter the condition/covertype, etc. (anymore?) -BUG-29: saving a book on wish list needs a year_published (shouldn't) ### DB/back-end diff --git a/main.py b/main.py index 5ca1dd1..df3a774 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ from flask_sqlalchemy import SQLAlchemy from sqlalchemy.exc import SQLAlchemyError from flask_marshmallow import Marshmallow from flask_bootstrap import Bootstrap -from wtforms import SubmitField, StringField, HiddenField, SelectField, IntegerField, TextAreaField, validators +from wtforms import SubmitField, StringField, HiddenField, SelectField, IntegerField, TextAreaField, validators, ValidationError from flask_wtf import FlaskForm #from flask_compress import Compress from status import st, Status @@ -65,6 +65,12 @@ from loan import Loan, LoanForm, LoanSchema from series import Series, SeriesForm, SeriesSchema, ListOfSeriesWithMissingBooks, CalcAvgRating from user import BDBUser + +# hacky constants (FIX THIS) +ON_WISHLIST=2 +COVERTYPE_NOT_APPLICABLE=4 +CONDITION_NOT_APPLICABLE=4 + ####################################### CLASSES / DB model ####################################### class QuickParentBook: parent=[] @@ -223,7 +229,7 @@ class BookForm(FlaskForm): owned = SelectField( 'owned' ) covertype = SelectField( 'covertype' ) condition = SelectField( 'condition' ) - year_published = IntegerField('Year Published:', validators=[validators.DataRequired(), validators.NumberRange(min=1900, max=2100)] ) + year_published = IntegerField('Year Published:', validators=[validators.Optional(), validators.NumberRange(min=1850, max=2100)] ) rating = SelectField( 'rating' ) notes = TextAreaField('Notes:') blurb = TextAreaField('Blurb:') @@ -232,6 +238,25 @@ class BookForm(FlaskForm): add_sub = SubmitField('Add Sub-Book' ) rem_sub = SubmitField('Remove Sub-Book from Parent' ) + def validate(self): + print( f"type: self.errors={type(self.errors)}" ) + # if on wish list, just accept year_published + if int(self.owned.data) == ON_WISHLIST: + return True + else: + # we own/sold this, so covertype and condition cant be N/A + if int(self.covertype.data) == COVERTYPE_NOT_APPLICABLE: + return False + if int(self.condition.data) == CONDITION_NOT_APPLICABLE: + return False + + # otherwise lets check that there is data and its in the right range + if self.year_published.data and self.year_published.data>1850 and int(self.year_published.raw_data[0]) < 2100: + return True + else: + return False + + ################################# helper functions ################################### def GetBookIdFromSeriesByBookNum( series_id, book_num ): tmp_book = Book_Series_Link.query.filter(Book_Series_Link.series_id==series_id,Book_Series_Link.book_num==book_num).all() @@ -547,7 +572,11 @@ def new_book(): form.process() return render_template("book.html", page_title='Create new (sub) Book', b=bb, books=None, book_form=form, author_list=author_list, genre_list=genre_list, alert="", message="", poss_series_list=ListOfSeriesWithMissingBooks() ) elif form.validate_on_submit() and len(book_genres): - book = Book( title=request.form['title'], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors ) + if request.form['year_published'].isnumeric(): + book = Book( title=request.form['title'], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors ) + else: + book = Book( title=request.form['title'], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors ) + db.session.add(book) db.session.commit() # this is a sub-book we have added @@ -574,13 +603,17 @@ def new_book(): return redirect( '/book/{}'.format(book.id) ) else: alert="danger" - message="Failed to create Book" + message="Failed to create Book." for field in form.errors: message = "{}
{}={}".format( message, field, form.errors[field] ) if len(book_genres) == 0: message = "{}
genre=book has to have a genre selected".format( message ) print( "ERROR: Failed to create book: {}".format(message) ) - book = Book( title=request.form["title"], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors ) + if request.form['year_published'].isnumeric(): + book = Book( title=request.form["title"], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], year_published=request.form['year_published'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors ) + else: + book = Book( title=request.form["title"], owned=request.form['owned'], covertype=request.form['covertype'], condition=request.form['condition'], publisher=request.form['publisher'], rating=request.form['rating'], notes=request.form['notes'], blurb=request.form['blurb'], genre=book_genres, author=book_authors ) + if 'parent_id' in request.form: bb=QuickParentBook() bb.parent=[] @@ -643,7 +676,8 @@ def book(id): book.covertype = request.form['covertype'] book.condition = request.form['condition'] book.publisher = request.form['publisher'] - book.year_published = request.form['year_published'] + if request.form['year_published'].isnumeric(): + book.year_published = request.form['year_published'] book.rating = request.form['rating'] book.notes = request.form['notes'] book.blurb = request.form['blurb'] @@ -715,9 +749,13 @@ def book(id): st.SetMessage( "Successfully Updated Book (id={})".format(id) ) else: st.SetAlert("danger") - message="Failed to update Book (id={})".format(id) - for field in book_form.errors: - message = "{}
{}={}".format( message, field, book_form.errors[field] ) + message=f"Failed to update Book (id={id}). " + if not book_form.year_published.data: + message += f" year_published cannot be empty"; + if int(book_form.condition.data) == CONDITION_NOT_APPLICABLE: + message += f" condition cannot be N/A"; + if int(book_form.covertype.data) == COVERTYPE_NOT_APPLICABLE: + message += f" covertype cannot be N/A"; book = Book.query.get(id) st.SetMessage(message) else: diff --git a/templates/book.html b/templates/book.html index ce1f6d1..685b144 100644 --- a/templates/book.html +++ b/templates/book.html @@ -436,6 +436,7 @@ function AddAuthorToBook(num) { function() { $('#author-{{cnt.idx}}').prop( 'style', '' ) } ) {% set cnt.idx = cnt.idx+1 %} {% endfor %} + $('#owned').click( function() { if( $("#owned option:selected").text() == 'On Wish List' ) { $('#covertype').val( $('#covertype option:last').val() ); $('#condition').val( $('#condition option:last').val() ); $('#year_published').val(''); $('#rating').val( $('#rating option:last').val() ) } } ) } ) {% endblock script_content %}