From d69022455983a5b3f8575bc226e5a9c3cc0f3c73 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 6 Jan 2022 09:39:51 -0800 Subject: [PATCH] Use weighted averages --- bookwyrm/templates/about/about.html | 6 +++--- bookwyrm/views/landing/landing.py | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/bookwyrm/templates/about/about.html b/bookwyrm/templates/about/about.html index 3dd53d19b..5aa68d1e0 100644 --- a/bookwyrm/templates/about/about.html +++ b/bookwyrm/templates/about/about.html @@ -30,7 +30,7 @@
{% blocktrans trimmed with title=top_rated|book_title site_name=site.name rating=top_rated.rating|floatformat:1 %} - {{ title }} is {{ site_name }}'s most beloved book, with a {{ rating }} rating out of 5 + {{ title }} is {{ site_name }}'s most beloved book, with an average rating of {{ rating }} out of 5. {% endblocktrans %}
@@ -44,7 +44,7 @@
{% blocktrans trimmed with title=wanted|book_title site_name=site.name %} - More {{ site_name }} users want to read {{ title }} + More {{ site_name }} users want to read {{ title }}. {% endblocktrans %}
@@ -58,7 +58,7 @@
{% blocktrans trimmed with title=controversial|book_title site_name=site.name %} - {{ title }} has the most divisive ratings of any book on {{ site_name }} + {{ title }} has the most divisive ratings of any book on {{ site_name }}. {% endblocktrans %}
diff --git a/bookwyrm/views/landing/landing.py b/bookwyrm/views/landing/landing.py index 782cae24c..995d923ec 100644 --- a/bookwyrm/views/landing/landing.py +++ b/bookwyrm/views/landing/landing.py @@ -1,6 +1,6 @@ """ non-interactive pages """ from dateutil.relativedelta import relativedelta -from django.db.models import Avg, StdDev, Count, Q +from django.db.models import Avg, StdDev, Count, F, Q from django.template.response import TemplateResponse from django.utils import timezone from django.views import View @@ -29,13 +29,20 @@ def about(request): books = models.Edition.objects.exclude(cover__exact="") + total_ratings = models.Review.objects.filter(user__local=True).count() data["top_rated"] = books.annotate( - rating=Avg("review__rating", filter=Q(review__user__local=True)) - ).filter(rating__gt=0).order_by("-rating").first() + rating=Avg("review__rating", filter=Q(review__user__local=True)), + rating_count=Count("review__rating", filter=Q(review__user__local=True)), + ).annotate( + weighted=F("rating") * F("rating_count") / total_ratings + ).filter(weighted__gt=0).order_by("-weighted").first() data["controversial"] = books.annotate( - deviation=StdDev("review__rating") - ).filter(deviation__gt=0).order_by("-deviation").first() + deviation=StdDev("review__rating", filter=Q(review__user__local=True)), + rating_count=Count("review__rating", filter=Q(review__user__local=True)), + ).annotate( + weighted=F("deviation") * F("rating_count") / total_ratings + ).filter(weighted__gt=0).order_by("-weighted").first() data["wanted"] = books.annotate( shelf_count=Count("shelves", filter=Q(shelves__identifier="to-read"))