moviewyrm/bookwyrm/views/annual_summary.py

120 lines
3.9 KiB
Python
Raw Normal View History

2021-12-21 10:43:09 +00:00
from datetime import date
2021-12-21 09:21:30 +00:00
from django.db.models import Case, When, Avg, Sum
2021-12-21 10:43:09 +00:00
from django.http import Http404
2021-12-20 22:37:45 +00:00
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
from django.views import View
from bookwyrm import models
2021-12-21 10:43:09 +00:00
# December day of first availability
FIRST_DAY = 15
# January day of last availability, 0 for no availability in Jan.
LAST_DAY = 15
def get_annual_summary_year():
"""return the latest available annual summary year or None"""
today = date.today()
if today >= date(today.year, 12, FIRST_DAY) and today <= date(today.year, 12, 31):
return today.year
if LAST_DAY > 0 and today >= date(today.year, 1, 1) and today <= date(today.year, 1, LAST_DAY):
return today.year - 1
return None
2021-12-21 10:43:09 +00:00
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
2021-12-20 22:37:45 +00:00
class AnnualSummary(View):
2021-12-21 10:43:09 +00:00
"""display a summary of the year for the current user"""
2021-12-20 22:37:45 +00:00
def get(self, request, year):
"""get response"""
2021-12-21 10:43:09 +00:00
if not is_year_available(year):
raise Http404(f"The summary for {year} is unavailable")
2021-12-21 10:52:33 +00:00
paginated_years = (
int(year) - 1,
int(year) + 1 if is_year_available(int(year) + 1) else None
)
2021-12-20 22:37:45 +00:00
user = request.user
read_shelf = get_object_or_404(user.shelf_set, identifier="read")
2021-12-21 09:21:30 +00:00
read_book_ids_in_year = (
2021-12-20 22:37:45 +00:00
models.ShelfBook.objects.filter(shelf=read_shelf)
.filter(user=user)
.filter(shelved_date__year=year)
.order_by("shelved_date", "created_date", "updated_date")
2021-12-21 09:21:30 +00:00
.values_list("book", flat=True)
2021-12-20 22:37:45 +00:00
)
2021-12-21 10:52:33 +00:00
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)
2021-12-21 09:21:30 +00:00
read_shelf_order = Case(
2021-12-20 22:37:45 +00:00
*[When(pk=pk, then=pos) for pos, pk in enumerate(read_book_ids_in_year)]
)
read_books_in_year = models.Edition.objects.filter(
id__in=read_book_ids_in_year
2021-12-21 09:21:30 +00:00
).order_by(read_shelf_order)
2021-12-20 22:37:45 +00:00
"""pages stats queries"""
2021-12-21 09:21:30 +00:00
page_stats = read_books_in_year.aggregate(Sum("pages"), Avg("pages"))
book_list_by_pages = read_books_in_year.filter(pages__gte=0).order_by("pages")
book_pages_lowest = book_list_by_pages.first()
book_pages_highest = book_list_by_pages.last()
2021-12-20 22:37:45 +00:00
"""books with no pages"""
no_page_list = len(read_books_in_year.filter(pages__exact=None))
"""rating stats queries"""
ratings = (
models.Review.objects.filter(user=user)
.exclude(deleted=True)
.exclude(rating=None)
.filter(book_id__in=read_book_ids_in_year)
)
best_ratings_books_ids = [review.book.id for review in ratings.filter(rating=5)]
2021-12-21 09:21:30 +00:00
ratings_stats = ratings.aggregate(Avg("rating"))
2021-12-20 22:37:45 +00:00
data = {
"year": year,
"books_total": len(read_books_in_year),
"books": read_books_in_year,
2021-12-21 09:21:30 +00:00
"pages_total": page_stats["pages__sum"],
"pages_average": page_stats["pages__avg"],
2021-12-20 22:37:45 +00:00
"book_pages_lowest": book_pages_lowest,
"book_pages_highest": book_pages_highest,
"no_page_number": no_page_list,
"ratings_total": len(ratings),
2021-12-21 09:21:30 +00:00
"rating_average": ratings_stats["rating__avg"],
2021-12-20 22:37:45 +00:00
"book_rating_highest": ratings.order_by("-rating").first(),
"best_ratings_books_ids": best_ratings_books_ids,
2021-12-21 10:43:09 +00:00
"paginated_years": paginated_years,
2021-12-20 22:37:45 +00:00
}
return TemplateResponse(request, "annual_summary/layout.html", data)