moviewyrm/bookwyrm/views/annual_summary.py

239 lines
7 KiB
Python
Raw Normal View History

2021-12-21 11:12:32 +00:00
"""end-of-year read books stats"""
2021-12-21 10:43:09 +00:00
from datetime import date
from uuid import uuid4
2021-12-21 10:43:09 +00:00
from django.contrib.auth.decorators import login_required
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-27 22:49:48 +00:00
from django.shortcuts import redirect
2021-12-20 22:37:45 +00:00
from django.template.response import TemplateResponse
from django.views import View
from django.views.decorators.http import require_POST
2021-12-20 22:37:45 +00:00
from bookwyrm import models
from .helpers import get_user_from_username
2021-12-20 22:37:45 +00:00
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
2021-12-21 12:17:18 +00:00
# pylint: disable= no-self-use
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, username, year):
2021-12-20 22:37:45 +00:00
"""get response"""
user = get_user_from_username(request.user, username)
2021-12-21 10:43:09 +00:00
year_key = None
if user.summary_keys and year in user.summary_keys:
year_key = user.summary_keys[year]
2021-12-21 10:52:33 +00:00
2021-12-22 16:09:12 +00:00
privacy_verification(request, user, year, year_key)
2021-12-21 11:22:40 +00:00
paginated_years = (
int(year) - 1 if is_year_available(user, int(year) - 1) else None,
int(year) + 1 if is_year_available(user, int(year) + 1) else None,
)
2021-12-21 13:44:21 +00:00
# get data
read_book_ids_in_year = (
user.readthrough_set.filter(
finish_date__year__gte=year,
finish_date__year__lt=int(year) + 1,
)
2021-12-28 09:49:09 +00:00
.order_by("finish_date")
.values_list("book__id", flat=True)
)
2021-12-21 10:52:33 +00:00
if len(read_book_ids_in_year) == 0:
data = {
"summary_user": user,
2021-12-21 10:52:33 +00:00
"year": year,
2021-12-22 14:12:56 +00:00
"year_key": year_key,
2021-12-21 10:52:33 +00:00
"book_total": 0,
"books": [],
"paginated_years": paginated_years,
}
return TemplateResponse(request, "annual_summary/layout.html", data)
2021-12-21 11:22:40 +00:00
read_books_in_year = get_books_from_shelfbooks(read_book_ids_in_year)
2021-12-20 22:37:45 +00:00
2021-12-21 11:12:32 +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")
2021-12-20 22:37:45 +00:00
2021-12-21 11:12:32 +00:00
# books with no pages
2021-12-20 22:37:45 +00:00
no_page_list = len(read_books_in_year.filter(pages__exact=None))
2021-12-21 11:12:32 +00:00
# rating stats queries
2021-12-20 22:37:45 +00:00
ratings = (
models.Review.objects.filter(user=user)
.exclude(deleted=True)
.exclude(rating=None)
.filter(book_id__in=read_book_ids_in_year)
)
2021-12-21 09:21:30 +00:00
ratings_stats = ratings.aggregate(Avg("rating"))
2021-12-20 22:37:45 +00:00
data = {
"summary_user": user,
2021-12-20 22:37:45 +00:00
"year": year,
2021-12-22 14:12:56 +00:00
"year_key": year_key,
2021-12-20 22:37:45 +00:00
"books_total": len(read_books_in_year),
"books": read_books_in_year,
"pages_total": page_stats["pages__sum"] or 0,
2021-12-21 13:44:21 +00:00
"pages_average": round(
page_stats["pages__avg"] if page_stats["pages__avg"] else 0
),
2021-12-21 11:22:40 +00:00
"book_pages_lowest": book_list_by_pages.first(),
"book_pages_highest": book_list_by_pages.last(),
2021-12-20 22:37:45 +00:00
"no_page_number": no_page_list,
"ratings_total": len(ratings),
2021-12-22 10:18:26 +00:00
"rating_average": round(
ratings_stats["rating__avg"] if ratings_stats["rating__avg"] else 0, 2
),
2021-12-20 22:37:45 +00:00
"book_rating_highest": ratings.order_by("-rating").first(),
2021-12-21 11:22:40 +00:00
"best_ratings_books_ids": [
review.book.id for review in ratings.filter(rating=5)
],
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)
2021-12-21 11:22:40 +00:00
@login_required
def personal_annual_summary(request, year):
"""redirect simple URL to URL with username"""
return redirect("annual-summary", request.user.localname, year)
2021-12-22 14:12:56 +00:00
@login_required
@require_POST
def summary_add_key(request):
"""add summary key"""
year = request.POST["year"]
user = request.user
new_key = uuid4().hex
if not user.summary_keys:
user.summary_keys = {
year: new_key,
}
else:
user.summary_keys[year] = new_key
user.save()
response = redirect("annual-summary", user.localname, year)
response["Location"] += f"?key={str(new_key)}"
return response
@login_required
@require_POST
def summary_revoke_key(request):
"""revoke summary key"""
year = request.POST["year"]
user = request.user
if user.summary_keys and year in user.summary_keys:
user.summary_keys.pop(year)
user.save()
return redirect("annual-summary", user.localname, year)
def get_annual_summary_year():
"""return the latest available annual summary year or None"""
today = date.today()
if date(today.year, 12, FIRST_DAY) <= today <= date(today.year, 12, 31):
return today.year
if LAST_DAY > 0 and date(today.year, 1, 1) <= today <= date(
today.year, 1, LAST_DAY
):
return today.year - 1
return None
2021-12-22 16:09:12 +00:00
def privacy_verification(request, user, year, year_key):
2021-12-22 16:14:21 +00:00
"""raises a 404 error if the user should not access the page"""
2021-12-22 16:09:12 +00:00
if user != request.user:
request_key = None
if "key" in request.GET:
request_key = request.GET["key"]
if not request_key or request_key != year_key:
raise Http404(f"The summary for {year} is unavailable")
if not is_year_available(user, year):
raise Http404(f"The summary for {year} is unavailable")
def is_year_available(user, year):
"""return boolean"""
2021-12-22 17:15:05 +00:00
earliest_year = int(get_earliest_year(user, year))
today = date.today()
year = int(year)
2021-12-22 16:09:12 +00:00
if earliest_year <= year < today.year:
return True
if year == today.year and today >= date(today.year, 12, FIRST_DAY):
return True
return False
2021-12-22 17:15:05 +00:00
def get_earliest_year(user, year):
"""return the earliest finish_date or shelved_date year for user books in read shelf"""
read_shelfbooks = models.ShelfBook.objects.filter(user__id=user.id).filter(
shelf__identifier__exact="read"
)
read_shelfbooks_list = list(read_shelfbooks.values("book", "shelved_date"))
book_dates = []
for book in read_shelfbooks_list:
earliest_finished = (
models.ReadThrough.objects.filter(user__id=user.id)
.filter(book_id=book["book"])
.exclude(finish_date__exact=None)
.order_by("finish_date")
.values("finish_date")
.first()
)
if earliest_finished:
book_dates.append(
min(earliest_finished["finish_date"], book["shelved_date"])
)
else:
book_dates.append(book["shelved_date"])
2021-12-22 17:15:05 +00:00
if book_dates:
return min(book_dates).year
return year
2021-12-21 11:22:40 +00:00
def get_books_from_shelfbooks(books_ids):
"""return an ordered QuerySet of books from a list"""
ordered = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(books_ids)])
books = models.Edition.objects.filter(id__in=books_ids).order_by(ordered)
return books