Fixed BUG-02 (recalc rating on book change), also added route to reset all ratings in Admin menu

This commit is contained in:
2021-01-10 11:10:49 +11:00
parent 35d92e0ab6
commit 6f5b65788d
4 changed files with 32 additions and 5 deletions

2
BUGs
View File

@@ -3,6 +3,8 @@
### DB/back-end ### DB/back-end
BUG-2: series does not deal with calcd_rating... 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) - (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/<id> with a series, then re-calc (with Calc func) for just this series
### UI not updating after DB change: ### 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) 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)

13
main.py
View File

@@ -38,7 +38,7 @@ from covertype import Covertype, CovertypeForm, CovertypeSchema, GetCovertypeByI
from owned import Owned, OwnedForm, OwnedSchema, GetOwnedById from owned import Owned, OwnedForm, OwnedSchema, GetOwnedById
from rating import Rating, RatingForm, RatingSchema, GetRatingById from rating import Rating, RatingForm, RatingSchema, GetRatingById
from loan import Loan, LoanForm, LoanSchema from loan import Loan, LoanForm, LoanSchema
from series import Series, SeriesForm, SeriesSchema, ListOfSeriesWithMissingBooks from series import Series, SeriesForm, SeriesSchema, ListOfSeriesWithMissingBooks, CalcAvgRating
####################################### CLASSES / DB model ####################################### ####################################### CLASSES / DB model #######################################
class QuickParentBook: class QuickParentBook:
@@ -515,6 +515,7 @@ def book(id):
return redirect( '/book/{}'.format(pid) ) return redirect( '/book/{}'.format(pid) )
else: else:
return redirect( '/' ) return redirect( '/' )
# save/update of book
elif book_form.validate(): elif book_form.validate():
book = Book.query.get(id) book = Book.query.get(id)
book.title = request.form['title'] book.title = request.form['title']
@@ -580,8 +581,14 @@ def book(id):
else: 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)]) 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 ) 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() db.session.commit()
st.SetMessage( "Successfully Updated Book (id={})".format(id) ) st.SetMessage( "Successfully Updated Book (id={})".format(id) )
else: else:

View File

@@ -42,8 +42,8 @@ class SeriesForm(FlaskForm):
def CalcAvgRating(sid): 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)) 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: for row in res:
print( row ) rating = row.rating
return row.rating return rating
def ListOfSeriesWithMissingBooks(): 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") 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) form = SeriesForm(request.values, obj=series)
return render_template("series.html", form=form, page_title=page_title, message=st.GetMessage(), alert=st.GetAlert() ) 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 # Gets the Series matching id from DB, helper func in jinja2 code to show books
################################################################################ ################################################################################

View File

@@ -93,6 +93,7 @@
<a class="dropdown-item" href="{{url_for('ratings')}}">Show Ratings</a> <a class="dropdown-item" href="{{url_for('ratings')}}">Show Ratings</a>
<a class="dropdown-item" href="{{url_for('new_loan')}}">Create new Loan</a> <a class="dropdown-item" href="{{url_for('new_loan')}}">Create new Loan</a>
<a class="dropdown-item" href="{{url_for('loans')}}">Show Loans</a> <a class="dropdown-item" href="{{url_for('loans')}}">Show Loans</a>
<a class="dropdown-item" href="{{url_for('reset_all_series_ratings')}}">Recalculate all series ratings</a>
</div class="dropdow-menu"> </div class="dropdow-menu">
</div class="nav-item dropdown"> </div class="nav-item dropdown">
</div clas="navbar-nav"> </div clas="navbar-nav">