diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 9b89b4e7..0cbde8ef 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -23,9 +23,8 @@ def get_annual_summary_year(): 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) + if LAST_DAY > 0 and date(today.year, 1, 1) <= today <= date( + today.year, 1, LAST_DAY ): return today.year - 1 @@ -60,14 +59,8 @@ class AnnualSummary(View): ) user = request.user - read_shelf = get_object_or_404(user.shelf_set, identifier="read") - read_book_ids_in_year = ( - models.ShelfBook.objects.filter(shelf=read_shelf) - .filter(user=user) - .filter(shelved_date__year=year) - .order_by("shelved_date", "created_date", "updated_date") - .values_list("book", flat=True) - ) + + read_book_ids_in_year = get_read_book_ids_in_year(user, year) if len(read_book_ids_in_year) == 0: data = { @@ -78,18 +71,11 @@ class AnnualSummary(View): } 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)] - ) - read_books_in_year = models.Edition.objects.filter( - id__in=read_book_ids_in_year - ).order_by(read_shelf_order) + read_books_in_year = get_books_from_shelfbooks(read_book_ids_in_year) # pages stats queries 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() # books with no pages no_page_list = len(read_books_in_year.filter(pages__exact=None)) @@ -101,7 +87,6 @@ class AnnualSummary(View): .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)] ratings_stats = ratings.aggregate(Avg("rating")) data = { @@ -110,14 +95,39 @@ class AnnualSummary(View): "books": read_books_in_year, "pages_total": page_stats["pages__sum"], "pages_average": round(page_stats["pages__avg"]), - "book_pages_lowest": book_pages_lowest, - "book_pages_highest": book_pages_highest, + "book_pages_lowest": book_list_by_pages.first(), + "book_pages_highest": book_list_by_pages.last(), "no_page_number": no_page_list, "ratings_total": len(ratings), "rating_average": round(ratings_stats["rating__avg"], 2), "book_rating_highest": ratings.order_by("-rating").first(), - "best_ratings_books_ids": best_ratings_books_ids, + "best_ratings_books_ids": [ + review.book.id for review in ratings.filter(rating=5) + ], "paginated_years": paginated_years, } return TemplateResponse(request, "annual_summary/layout.html", data) + + +def get_read_book_ids_in_year(user, year): + """return an ordered QuerySet of the read book ids""" + + read_shelf = get_object_or_404(user.shelf_set, identifier="read") + read_book_ids_in_year = ( + models.ShelfBook.objects.filter(shelf=read_shelf) + .filter(user=user) + .filter(shelved_date__year=year) + .order_by("shelved_date", "created_date", "updated_date") + .values_list("book", flat=True) + ) + return read_book_ids_in_year + + +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