diff --git a/BUGs b/BUGs index 4a651a1..a317411 100644 --- a/BUGs +++ b/BUGs @@ -3,6 +3,8 @@ ### DB/back-end BUG-2: series does not deal with calcd_rating... - (on edit could, recalc as a catch-all, and obviously if we change a single book's rating, we should re-calc) + OK: have a once-off reset calc values (have an admin route for it, why not - leverage the Calc func in series.py) + THEN: any POST to /book/ with a series, then re-calc (with Calc func) for just this series ### UI not updating after DB change: BUG-3: alter a condition, covertype, etc. and its not in drop-down list (even though db look via index is correct, e.g books.html shows updated covertype) diff --git a/main.py b/main.py index 76b563a..eeb00a1 100644 --- a/main.py +++ b/main.py @@ -38,7 +38,7 @@ from covertype import Covertype, CovertypeForm, CovertypeSchema, GetCovertypeByI from owned import Owned, OwnedForm, OwnedSchema, GetOwnedById from rating import Rating, RatingForm, RatingSchema, GetRatingById from loan import Loan, LoanForm, LoanSchema -from series import Series, SeriesForm, SeriesSchema, ListOfSeriesWithMissingBooks +from series import Series, SeriesForm, SeriesSchema, ListOfSeriesWithMissingBooks, CalcAvgRating ####################################### CLASSES / DB model ####################################### class QuickParentBook: @@ -515,6 +515,7 @@ def book(id): return redirect( '/book/{}'.format(pid) ) else: return redirect( '/' ) + # save/update of book elif book_form.validate(): book = Book.query.get(id) book.title = request.form['title'] @@ -580,8 +581,14 @@ def book(id): else: sql="insert into book_series_link (book_id, series_id, book_num) values ( {}, {}, {} )".format( request.form['bsl-book_id-{}'.format(cnt)], request.form['bsl-series_id-{}'.format(cnt)], request.form['bsl-book_num-{}'.format(cnt)]) db.engine.execute( sql ) - cnt=cnt+1 - + db.session.commit() + # reset rating on this series as the book has changed (and maybe the rating has changed) + for field in request.form: + if 'bsl-book_id-' in field and field != 'bsl-book_id-NUM': + cnt=int(re.findall( '\d+', field )[0]) + s=Series.query.get(request.form['bsl-series_id-{}'.format(cnt)]) + s.calcd_rating = CalcAvgRating(s.id) + cnt=cnt+1 db.session.commit() st.SetMessage( "Successfully Updated Book (id={})".format(id) ) else: diff --git a/series.py b/series.py index 71cf7cb..e043c5d 100644 --- a/series.py +++ b/series.py @@ -42,8 +42,8 @@ class SeriesForm(FlaskForm): def CalcAvgRating(sid): res=db.engine.execute("select round(avg(to_number(r.name, '99')),1) as rating from book b, rating r, series s, book_series_link bsl where s.id={} and s.id = bsl.series_id and bsl.book_id = b.id and b.rating = r.id and r.name ~ E'^\\\\d+$'".format(sid)) for row in res: - print( row ) - return row.rating + rating = row.rating + return rating def ListOfSeriesWithMissingBooks(): res=db.engine.execute("select id, title, num_books, count from ( select s.id, s.title, s.num_books, count(bsl.book_id) from series s, book_series_link bsl where s.id = bsl.series_id group by s.id ) as foo where num_books > count") @@ -121,6 +121,23 @@ def series(id): form = SeriesForm(request.values, obj=series) return render_template("series.html", form=form, page_title=page_title, message=st.GetMessage(), alert=st.GetAlert() ) +################################################################################ +# /series/rating_reset -> forces a reset of calculated ratings of all series +################################################################################ +@app.route("/seriess/rating_reset", methods=["GET"]) +def reset_all_series_ratings(): + s_list = Series.query.all() + try: + for s in s_list: + s.calcd_rating = CalcAvgRating(s.id) + db.session.commit() + st.SetMessage("Successfully reset calculated average ratings for all series"); + return render_template("base.html", message=st.GetMessage(), alert=st.GetAlert() ) + except: + st.SetAlert("danger") + st.SetMessage("Failed to reset calculated average rating for all series"); + return render_template("base.html", message=st.GetMessage(), alert=st.GetAlert() ) + ################################################################################ # Gets the Series matching id from DB, helper func in jinja2 code to show books ################################################################################ diff --git a/templates/base.html b/templates/base.html index 260289b..ea6ec04 100644 --- a/templates/base.html +++ b/templates/base.html @@ -93,6 +93,7 @@ Show Ratings Create new Loan Show Loans + Recalculate all series ratings