mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-12 18:25:35 +00:00
Use weighted averages
This commit is contained in:
parent
840746754d
commit
d690224559
2 changed files with 15 additions and 8 deletions
|
@ -30,7 +30,7 @@
|
|||
</div>
|
||||
<div class="media-content">
|
||||
{% blocktrans trimmed with title=top_rated|book_title site_name=site.name rating=top_rated.rating|floatformat:1 %}
|
||||
<em>{{ title }}</em> is {{ site_name }}'s most beloved book, with a {{ rating }} rating out of 5
|
||||
<em>{{ title }}</em> is {{ site_name }}'s most beloved book, with an average rating of {{ rating }} out of 5.
|
||||
{% endblocktrans %}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -44,7 +44,7 @@
|
|||
</div>
|
||||
<div class="media-content">
|
||||
{% blocktrans trimmed with title=wanted|book_title site_name=site.name %}
|
||||
More {{ site_name }} users want to read <em>{{ title }}</em>
|
||||
More {{ site_name }} users want to read <em>{{ title }}</em>.
|
||||
{% endblocktrans %}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -58,7 +58,7 @@
|
|||
</div>
|
||||
<div class="media-content">
|
||||
{% blocktrans trimmed with title=controversial|book_title site_name=site.name %}
|
||||
<em>{{ title }}</em> has the most divisive ratings of any book on {{ site_name }}
|
||||
<em>{{ title }}</em> has the most divisive ratings of any book on {{ site_name }}.
|
||||
{% endblocktrans %}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -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"))
|
||||
|
|
Loading…
Reference in a new issue