Fix for empty years

This commit is contained in:
Joachim 2021-12-21 11:52:33 +01:00
parent 1fd44aeb5c
commit c2c57db8e6
2 changed files with 17 additions and 7 deletions

View file

@ -20,7 +20,7 @@
</div>
{% endwith %}
{% with year=paginated_year|last %}
{% with year=paginated_years|last %}
{% if year %}
<div class="column has-text-right">
<a href="{% url 'annual-summary' year %}">
@ -35,7 +35,7 @@
<h1 class="title is-1 is-serif has-text-centered mb-5">{% blocktrans %}{{ year }} <em>in the books</em>{% endblocktrans %}</h1>
{% if not books %}
{% blocktrans %}Sadly you didn't finish any book in {{ year }}{% endblocktrans %}
<p class="has-text-centered is-size-5">{% blocktrans %}Sadly you didn't finish any book in {{ year }}{% endblocktrans %}</p>
{% else %}
<div class="columns is-mobile">

View file

@ -50,6 +50,11 @@ class AnnualSummary(View):
if not is_year_available(year):
raise Http404(f"The summary for {year} is unavailable")
paginated_years = (
int(year) - 1,
int(year) + 1 if is_year_available(int(year) + 1) else None
)
user = request.user
read_shelf = get_object_or_404(user.shelf_set, identifier="read")
read_book_ids_in_year = (
@ -59,6 +64,16 @@ class AnnualSummary(View):
.order_by("shelved_date", "created_date", "updated_date")
.values_list("book", flat=True)
)
if len(read_book_ids_in_year) == 0:
data = {
"year": year,
"book_total": 0,
"books": [],
"paginated_years": paginated_years,
}
return TemplateResponse(request, "annual_summary/layout.html", data)
read_shelf_order = Case(
*[When(pk=pk, then=pos) for pos, pk in enumerate(read_book_ids_in_year)]
)
@ -85,11 +100,6 @@ 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),