Updates queries

This commit is contained in:
Mouse Reeve 2022-01-08 11:18:53 -08:00
parent 392dbfce01
commit bb4b724b1a
2 changed files with 31 additions and 27 deletions

View file

@ -22,54 +22,60 @@
<div class="columns">
{% if top_rated %}
{% with book=top_rated.default_edition rating=top_rated.rating %}
<div class="column is-one-third is-flex">
<div class="media notification">
<div class="media-left">
<a href="{{ top_rated.local_path }}">
{% include 'snippets/book_cover.html' with book=top_rated cover_class='is-h-m' size='medium' aria='show' %}
<a href="{{ book.local_path }}">
{% include 'snippets/book_cover.html' with book=book cover_class='is-h-m' size='medium' aria='show' %}
</a>
</div>
<div class="media-content">
{% blocktrans trimmed with title=top_rated|book_title book_path=top_rated.local_path site_name=site.name rating=top_rated.rating|floatformat:1 %}
{% blocktrans trimmed with title=book|book_title book_path=book.local_path site_name=site.name rating=rating|floatformat:1 %}
<a href="{{ book_path }}"><em>{{ title }}</em></a> is {{ site_name }}'s most beloved book, with an average rating of {{ rating }} out of 5.
{% endblocktrans %}
</div>
</div>
</div>
{% endwith %}
{% endif %}
{% if wanted %}
{% with book=wanted.default_edition %}
<div class="column is-one-third is-flex">
<div class="media notification">
<div class="media-left">
<a href="{{ wanted.local_path }}">
{% include 'snippets/book_cover.html' with book=wanted cover_class='is-h-m' size='medium' aria='show' %}
<a href="{{ book.local_path }}">
{% include 'snippets/book_cover.html' with book=book cover_class='is-h-m' size='medium' aria='show' %}
</a>
</div>
<div class="media-content">
{% blocktrans trimmed with title=wanted|book_title book_path=wanted.local_path site_name=site.name %}
{% blocktrans trimmed with title=book|book_title book_path=book.local_path site_name=site.name %}
More {{ site_name }} users want to read <a href="{{ book_path }}"><em>{{ title }}</em></a> than any other book.
{% endblocktrans %}
</div>
</div>
</div>
{% endwith %}
{% endif %}
{% if controversial %}
{% with book=controversial.default_edition %}
<div class="column is-one-third is-flex">
<div class="media notification">
<div class="media-left">
<a href="{{ controversial.local_path }}">
{% include 'snippets/book_cover.html' with book=controversial cover_class='is-h-m' size='medium' aria='show' %}
<a href="{{ book.local_path }}">
{% include 'snippets/book_cover.html' with book=book cover_class='is-h-m' size='medium' aria='show' %}
</a>
</div>
<div class="media-content">
{% blocktrans trimmed with title=controversial|book_title book_path=controversial.local_path site_name=site.name %}
{% blocktrans trimmed with title=book|book_title book_path=book.local_path site_name=site.name %}
<a href="{{ book_path }}"><em>{{ title }}</em></a> has the most divisive ratings of any book on {{ site_name }}.
{% endblocktrans %}
</div>
</div>
</div>
{% endwith %}
{% endif %}
</div>

View file

@ -24,37 +24,33 @@ def about(request):
"version": settings.VERSION,
}
books = models.Edition.objects.exclude(cover__exact="")
total_ratings = models.Review.objects.filter(
user__local=True, deleted=False
).count()
total_ratings = models.Review.objects.filter(local=True, deleted=False).count()
data["top_rated"] = (
books.annotate(
models.Work.objects.annotate(
rating=Avg(
"review__rating",
filter=Q(review__user__local=True, review__deleted=False),
"editions__review__rating",
filter=Q(editions__review__local=True, editions__review__deleted=False),
),
rating_count=Count(
"review__rating",
filter=Q(review__user__local=True, review__deleted=False),
"editions__review",
filter=Q(editions__review__local=True, editions__review__deleted=False),
),
)
.annotate(weighted=F("rating") * F("rating_count") / total_ratings)
.filter(weighted__gt=0)
.filter(rating__gt=4, weighted__gt=0)
.order_by("-weighted")
.first()
)
data["controversial"] = (
books.annotate(
models.Work.objects.annotate(
deviation=StdDev(
"review__rating",
filter=Q(review__user__local=True, review__deleted=False),
"editions__review__rating",
filter=Q(editions__review__local=True, editions__review__deleted=False),
),
rating_count=Count(
"review__rating",
filter=Q(review__user__local=True, review__deleted=False),
"editions__review",
filter=Q(editions__review__local=True, editions__review__deleted=False),
),
)
.annotate(weighted=F("deviation") * F("rating_count") / total_ratings)
@ -64,8 +60,10 @@ def about(request):
)
data["wanted"] = (
books.annotate(
shelf_count=Count("shelves", filter=Q(shelves__identifier="to-read"))
models.Work.objects.annotate(
shelf_count=Count(
"editions__shelves", filter=Q(editions__shelves__identifier="to-read")
)
)
.order_by("-shelf_count")
.first()