[books] Update widget to show all scrobbles WITH complete status
All checks were successful
build & deploy / test (push) Successful in 1m53s
build & deploy / deploy (push) Successful in 33s

This commit is contained in:
2026-03-31 13:41:45 -04:00
parent 2ca3dd1ed9
commit 6b278ad99f
2 changed files with 30 additions and 3 deletions

View File

@ -1144,9 +1144,32 @@ class EmbeddableTopBooksWidget(BaseEmbeddableWidget):
media_type = "Books"
count_label = "reads"
no_data_message = "No books read"
scrobble_filter = {"scrobble__long_play_complete": True}
def get_items(self, user, start_date):
from books.models import Book
from django.db.models import Count, Exists, OuterRef
return super().get_items(user, start_date, Book)
completed_subquery = Book.objects.filter(
scrobble__user=user,
scrobble__timestamp__gte=start_date,
scrobble__long_play_complete=True,
pk=OuterRef("pk"),
)
queryset = (
Book.objects.filter(
scrobble__user=user,
scrobble__timestamp__gte=start_date,
)
.annotate(
count=Count("scrobble", distinct=True),
is_completed=Exists(completed_subquery),
)
.order_by("-count")[:10]
)
items = list(queryset)
for item in items:
if not hasattr(item, "name") and hasattr(item, "title"):
item.name = item.title
return items