mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-25 09:30:33 +00:00
Use cache
This commit is contained in:
parent
bb4b724b1a
commit
26928252cd
5 changed files with 68 additions and 50 deletions
|
@ -2,12 +2,17 @@
|
||||||
{% load humanize %}
|
{% load humanize %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% load utilities %}
|
{% load utilities %}
|
||||||
|
{% load bookwyrm_tags %}
|
||||||
|
{% load cache %}
|
||||||
|
|
||||||
{% block title %}
|
{% block title %}
|
||||||
{% trans "About" %}
|
{% trans "About" %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block about_content %}
|
{% block about_content %}
|
||||||
|
{# seven day cache #}
|
||||||
|
{% cache 604800 about_page %}
|
||||||
|
{% get_book_superlatives as superlatives %}
|
||||||
<section class="content pb-4">
|
<section class="content pb-4">
|
||||||
<h2>
|
<h2>
|
||||||
{% blocktrans with site_name=site.name %}Welcome to {{ site_name }}!{% endblocktrans %}
|
{% blocktrans with site_name=site.name %}Welcome to {{ site_name }}!{% endblocktrans %}
|
||||||
|
@ -22,7 +27,7 @@
|
||||||
|
|
||||||
<div class="columns">
|
<div class="columns">
|
||||||
{% if top_rated %}
|
{% if top_rated %}
|
||||||
{% with book=top_rated.default_edition rating=top_rated.rating %}
|
{% with book=superlatives.top_rated.default_edition rating=top_rated.rating %}
|
||||||
<div class="column is-one-third is-flex">
|
<div class="column is-one-third is-flex">
|
||||||
<div class="media notification">
|
<div class="media notification">
|
||||||
<div class="media-left">
|
<div class="media-left">
|
||||||
|
@ -41,7 +46,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if wanted %}
|
{% if wanted %}
|
||||||
{% with book=wanted.default_edition %}
|
{% with book=superlatives.wanted.default_edition %}
|
||||||
<div class="column is-one-third is-flex">
|
<div class="column is-one-third is-flex">
|
||||||
<div class="media notification">
|
<div class="media notification">
|
||||||
<div class="media-left">
|
<div class="media-left">
|
||||||
|
@ -60,7 +65,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if controversial %}
|
{% if controversial %}
|
||||||
{% with book=controversial.default_edition %}
|
{% with book=superlatives.controversial.default_edition %}
|
||||||
<div class="column is-one-third is-flex">
|
<div class="column is-one-third is-flex">
|
||||||
<div class="media notification">
|
<div class="media notification">
|
||||||
<div class="media-left">
|
<div class="media-left">
|
||||||
|
@ -131,4 +136,6 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
{% endcache %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% load static %}
|
{% load static %}
|
||||||
{% load humanize %}
|
{% load humanize %}
|
||||||
|
{% load cache %}
|
||||||
|
|
||||||
{% with status_type=status.status_type %}
|
{% with status_type=status.status_type %}
|
||||||
<div
|
<div
|
||||||
|
@ -15,6 +16,7 @@
|
||||||
|
|
||||||
<div class="columns is-gapless">
|
<div class="columns is-gapless">
|
||||||
{% if not hide_book %}
|
{% if not hide_book %}
|
||||||
|
{% cache 259200 content_status_book status.id %}
|
||||||
{% with book=status.book|default:status.mention_books.first %}
|
{% with book=status.book|default:status.mention_books.first %}
|
||||||
{% if book %}
|
{% if book %}
|
||||||
<div class="column is-cover">
|
<div class="column is-cover">
|
||||||
|
@ -34,6 +36,7 @@
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
{% endcache %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<article class="column ml-3-tablet my-3-mobile">
|
<article class="column ml-3-tablet my-3-mobile">
|
||||||
|
|
|
@ -3,8 +3,10 @@
|
||||||
{% load bookwyrm_tags %}
|
{% load bookwyrm_tags %}
|
||||||
{% load markdown %}
|
{% load markdown %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
{% load cache %}
|
||||||
|
|
||||||
{% if not hide_book %}
|
{% if not hide_book %}
|
||||||
|
{% cache 259200 generated_status_book status.id %}
|
||||||
{% with book=status.book|default:status.mention_books.first %}
|
{% with book=status.book|default:status.mention_books.first %}
|
||||||
<div class="columns is-mobile is-gapless">
|
<div class="columns is-mobile is-gapless">
|
||||||
<a class="column is-cover is-narrow" href="{{ book.local_path }}">
|
<a class="column is-cover is-narrow" href="{{ book.local_path }}">
|
||||||
|
@ -24,6 +26,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
{% endcache %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endspaceless %}
|
{% endspaceless %}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
""" template filters """
|
""" template filters """
|
||||||
from django import template
|
from django import template
|
||||||
from django.db.models import Avg
|
from django.db.models import Avg, StdDev, Count, F, Q
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from bookwyrm.views.feed import get_suggested_books
|
from bookwyrm.views.feed import get_suggested_books
|
||||||
|
@ -68,6 +68,57 @@ def load_subclass(status):
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag(takes_context=False)
|
||||||
|
def get_book_superlatives():
|
||||||
|
"""get book stats for the about page"""
|
||||||
|
total_ratings = models.Review.objects.filter(local=True, deleted=False).count()
|
||||||
|
data = {}
|
||||||
|
data["top_rated"] = (
|
||||||
|
models.Work.objects.annotate(
|
||||||
|
rating=Avg(
|
||||||
|
"editions__review__rating",
|
||||||
|
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
||||||
|
),
|
||||||
|
rating_count=Count(
|
||||||
|
"editions__review",
|
||||||
|
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.annotate(weighted=F("rating") * F("rating_count") / total_ratings)
|
||||||
|
.filter(rating__gt=4, weighted__gt=0)
|
||||||
|
.order_by("-weighted")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
data["controversial"] = (
|
||||||
|
models.Work.objects.annotate(
|
||||||
|
deviation=StdDev(
|
||||||
|
"editions__review__rating",
|
||||||
|
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
||||||
|
),
|
||||||
|
rating_count=Count(
|
||||||
|
"editions__review",
|
||||||
|
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.annotate(weighted=F("deviation") * F("rating_count") / total_ratings)
|
||||||
|
.filter(weighted__gt=0)
|
||||||
|
.order_by("-weighted")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
data["wanted"] = (
|
||||||
|
models.Work.objects.annotate(
|
||||||
|
shelf_count=Count(
|
||||||
|
"editions__shelves", filter=Q(editions__shelves__identifier="to-read")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.order_by("-shelf_count")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag(takes_context=False)
|
@register.simple_tag(takes_context=False)
|
||||||
def related_status(notification):
|
def related_status(notification):
|
||||||
"""for notifications"""
|
"""for notifications"""
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
""" non-interactive pages """
|
""" non-interactive pages """
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
from django.db.models import Avg, StdDev, Count, F, Q
|
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.views.decorators.http import require_GET
|
from django.views.decorators.http import require_GET
|
||||||
|
@ -24,51 +23,6 @@ def about(request):
|
||||||
"version": settings.VERSION,
|
"version": settings.VERSION,
|
||||||
}
|
}
|
||||||
|
|
||||||
total_ratings = models.Review.objects.filter(local=True, deleted=False).count()
|
|
||||||
data["top_rated"] = (
|
|
||||||
models.Work.objects.annotate(
|
|
||||||
rating=Avg(
|
|
||||||
"editions__review__rating",
|
|
||||||
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
|
||||||
),
|
|
||||||
rating_count=Count(
|
|
||||||
"editions__review",
|
|
||||||
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.annotate(weighted=F("rating") * F("rating_count") / total_ratings)
|
|
||||||
.filter(rating__gt=4, weighted__gt=0)
|
|
||||||
.order_by("-weighted")
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
|
|
||||||
data["controversial"] = (
|
|
||||||
models.Work.objects.annotate(
|
|
||||||
deviation=StdDev(
|
|
||||||
"editions__review__rating",
|
|
||||||
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
|
||||||
),
|
|
||||||
rating_count=Count(
|
|
||||||
"editions__review",
|
|
||||||
filter=Q(editions__review__local=True, editions__review__deleted=False),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.annotate(weighted=F("deviation") * F("rating_count") / total_ratings)
|
|
||||||
.filter(weighted__gt=0)
|
|
||||||
.order_by("-weighted")
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
|
|
||||||
data["wanted"] = (
|
|
||||||
models.Work.objects.annotate(
|
|
||||||
shelf_count=Count(
|
|
||||||
"editions__shelves", filter=Q(editions__shelves__identifier="to-read")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.order_by("-shelf_count")
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
|
|
||||||
return TemplateResponse(request, "about/about.html", data)
|
return TemplateResponse(request, "about/about.html", data)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue