From 368d2cd71614cc471432b5bfeae13a2de06d34b6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 May 2021 17:33:10 -0700 Subject: [PATCH] Reduces feed page queries for goals 9 fewer queries --- bookwyrm/models/user.py | 13 ++++++------- bookwyrm/templates/snippets/goal_progress.html | 15 +++++++++------ bookwyrm/views/feed.py | 9 +++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 7d821c5b..d9f3eba9 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -381,17 +381,16 @@ class AnnualGoal(BookWyrmModel): return {r.book.id: r.rating for r in reviews} @property - def progress_percent(self): - """how close to your goal, in percent form""" - return int(float(self.book_count / self.goal) * 100) - - @property - def book_count(self): + def progress(self): """how many books you've read this year""" - return self.user.readthrough_set.filter( + count = self.user.readthrough_set.filter( finish_date__year__gte=self.year, finish_date__year__lt=self.year + 1, ).count() + return { + "count": count, + "percent": int(float(count / self.goal) * 100), + } @app.task diff --git a/bookwyrm/templates/snippets/goal_progress.html b/bookwyrm/templates/snippets/goal_progress.html index 2d46181e..d0229b68 100644 --- a/bookwyrm/templates/snippets/goal_progress.html +++ b/bookwyrm/templates/snippets/goal_progress.html @@ -1,16 +1,19 @@ {% load i18n %} {% load humanize %} + +{% with goal.progress as progress %}

- {% if goal.progress_percent >= 100 %} + {% if progress.percent >= 100 %} {% trans "Success!" %} - {% elif goal.progress_percent %} - {% blocktrans with percent=goal.progress_percent %}{{ percent }}% complete!{% endblocktrans %} + {% elif progress.percent %} + {% blocktrans with percent=progress.percent %}{{ percent }}% complete!{% endblocktrans %} {% endif %} {% if goal.user == request.user %} - {% blocktrans with read_count=goal.book_count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}You've read {{ read_count }} of {{ goal_count}} books.{% endblocktrans %} + {% blocktrans with read_count=progress.count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}You've read {{ read_count }} of {{ goal_count}} books.{% endblocktrans %} {% else %} - {% blocktrans with username=goal.user.display_name read_count=goal.book_count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}{{ username }} has read {{ read_count }} of {{ goal_count}} books.{% endblocktrans %} + {% blocktrans with username=goal.user.display_name read_count=progress.count|intcomma goal_count=goal.goal|intcomma path=goal.local_path %}{{ username }} has read {{ read_count }} of {{ goal_count}} books.{% endblocktrans %} {% endif %}

- + +{% endwith %} diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index a03d6b4c..9a85dbb1 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -162,14 +162,15 @@ def get_suggested_books(user, max_books=5): else max_books - book_count ) shelf = user.shelf_set.get(identifier=preset) - - shelf_books = shelf.shelfbook_set.order_by("-updated_date")[:limit] - if not shelf_books: + if not shelf.books.exists(): continue + shelf_preview = { "name": shelf.name, "identifier": shelf.identifier, - "books": [s.book for s in shelf_books], + "books": shelf.books.order_by("shelfbook").prefetch_related("authors")[ + :limit + ], } suggested_books.append(shelf_preview) book_count += len(shelf_preview["books"])