Add previous/next year links

This commit is contained in:
Joachim 2021-12-21 11:43:09 +01:00
parent a24afdb6bf
commit a8e8785106
3 changed files with 53 additions and 2 deletions

View file

@ -10,6 +10,28 @@
{% endblock %}
{% block content %}
<div class="columns">
{% with year=paginated_years|first %}
<div class="column">
<a href="{% url 'annual-summary' year %}">
<span class="icon icon-arrow-left" aria-hidden="true"></span>
{% blocktrans %}{{ year }} in the books{% endblocktrans %}
</a>
</div>
{% endwith %}
{% with year=paginated_year|last %}
{% if year %}
<div class="column has-text-right">
<a href="{% url 'annual-summary' year %}">
{% blocktrans %}{{ year }} in the books{% endblocktrans %}
<span class="icon icon-arrow-right" aria-hidden="true"></span>
</a>
</div>
{% endif %}
{% endwith %}
</div>
<h1 class="title is-1 is-serif has-text-centered mb-5">{% blocktrans %}{{ year }} <em>in the books</em>{% endblocktrans %}</h1>
{% if not books %}

View file

@ -478,5 +478,5 @@ urlpatterns = [
r"^ostatus_success/?$", views.ostatus_follow_success, name="ostatus-success"
),
# annual summary
re_path(r"^my-year-in-the-books/(?P<year>\d{4})/?$", views.AnnualSummary.as_view()),
re_path(r"^my-year-in-the-books/(?P<year>\d{4})/?$", views.AnnualSummary.as_view(), name="annual-summary"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -1,4 +1,7 @@
from datetime import date
from django.db.models import Case, When, Avg, Sum
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
from django.views import View
@ -6,12 +9,32 @@ from django.views import View
from bookwyrm import models
# December day of first availability
FIRST_DAY = 15
def is_year_available(year):
"""return boolean"""
today = date.today()
year = int(year)
if year < today.year:
return True
if year == today.year and today >= date(today.year, 12, FIRST_DAY):
return True
return False
class AnnualSummary(View):
"""display a summary of the year"""
"""display a summary of the year for the current user"""
def get(self, request, year):
"""get response"""
if not is_year_available(year):
raise Http404(f"The summary for {year} is unavailable")
user = request.user
read_shelf = get_object_or_404(user.shelf_set, identifier="read")
read_book_ids_in_year = (
@ -47,6 +70,11 @@ class AnnualSummary(View):
best_ratings_books_ids = [review.book.id for review in ratings.filter(rating=5)]
ratings_stats = ratings.aggregate(Avg("rating"))
paginated_years = (
int(year) - 1,
int(year) + 1 if is_year_available(int(year) + 1) else None
)
data = {
"year": year,
"books_total": len(read_books_in_year),
@ -60,6 +88,7 @@ class AnnualSummary(View):
"rating_average": ratings_stats["rating__avg"],
"book_rating_highest": ratings.order_by("-rating").first(),
"best_ratings_books_ids": best_ratings_books_ids,
"paginated_years": paginated_years,
}
return TemplateResponse(request, "annual_summary/layout.html", data)