From 368d2cd71614cc471432b5bfeae13a2de06d34b6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 May 2021 17:33:10 -0700 Subject: [PATCH 1/8] 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 7d821c5ba..d9f3eba99 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 2d46181ef..d0229b689 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 a03d6b4cc..9a85dbb16 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"]) From 3d3ab6433e9fca5edd0ddc019b87af1076e1de16 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 May 2021 17:58:08 -0700 Subject: [PATCH 2/8] Improves query efficiency for shelve buttons About 50 fewer queries (yikes) --- bookwyrm/templates/get_started/book_preview.html | 2 +- bookwyrm/templates/layout.html | 3 +++ bookwyrm/templates/snippets/shelf_selector.html | 2 +- bookwyrm/templates/snippets/shelve_button/shelve_button.html | 4 ++-- .../snippets/shelve_button/shelve_button_dropdown.html | 2 +- bookwyrm/templatetags/bookwyrm_tags.py | 4 +--- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/bookwyrm/templates/get_started/book_preview.html b/bookwyrm/templates/get_started/book_preview.html index 578fef708..d8941ad50 100644 --- a/bookwyrm/templates/get_started/book_preview.html +++ b/bookwyrm/templates/get_started/book_preview.html @@ -5,7 +5,7 @@
diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index bad1cb6e5..d5a2087e5 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -193,8 +193,11 @@
+ {# almost every view needs to know the user shelves #} + {% with request.user.shelf_set.all as user_shelves %} {% block content %} {% endblock %} + {% endwith %}
diff --git a/bookwyrm/templates/snippets/shelf_selector.html b/bookwyrm/templates/snippets/shelf_selector.html index 0036a4e90..ca5a39f6e 100644 --- a/bookwyrm/templates/snippets/shelf_selector.html +++ b/bookwyrm/templates/snippets/shelf_selector.html @@ -6,7 +6,7 @@ {% endblock %} {% block dropdown-list %} -{% for shelf in request.user.shelf_set.all %} +{% for shelf in user_shelves %}
{% else %}
- {% include 'snippets/shelve_button/shelve_button_options.html' with class="shelf-option is-small" shelves=request.user.shelf_set.all active_shelf=active_shelf button_uuid=uuid %} + {% include 'snippets/shelve_button/shelve_button_options.html' with class="shelf-option is-small" shelves=user_shelves active_shelf=active_shelf button_uuid=uuid %}
{% include 'snippets/shelve_button/shelve_button_dropdown.html' with class="is-small" button_uuid=uuid%} {% endif %} @@ -25,7 +25,7 @@ {% include 'snippets/shelve_button/finish_reading_modal.html' with book=active_shelf.book controls_text="finish-reading" controls_uid=uuid readthrough=readthrough %} -{% include 'snippets/shelve_button/progress_update_modal.html' with book=shelf_book.book controls_text="progress-update" controls_uid=uuid readthrough=readthrough %} +{% include 'snippets/shelve_button/progress_update_modal.html' with book=active_shelf_book.book controls_text="progress-update" controls_uid=uuid readthrough=readthrough %} {% endwith %} {% endif %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown.html index 4439bfc28..353a37a1c 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown.html @@ -7,5 +7,5 @@ {% endblock %} {% block dropdown-list %} -{% include 'snippets/shelve_button/shelve_button_options.html' with active_shelf=active_shelf shelves=request.user.shelf_set.all dropdown=True class="shelf-option is-fullwidth is-small is-radiusless is-white" %} +{% include 'snippets/shelve_button/shelve_button_options.html' with active_shelf=active_shelf shelves=user_shelves dropdown=True class="shelf-option is-fullwidth is-small is-radiusless is-white" %} {% endblock %} diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 70228799f..072ff6634 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -3,8 +3,6 @@ from django import template from django.db.models import Avg from bookwyrm import models, views -from bookwyrm.views.status import to_markdown -from bookwyrm.templatetags.utilities import get_user_identifier register = template.Library() @@ -73,7 +71,7 @@ def active_shelf(context, book): """check what shelf a user has a book on, if any""" shelf = models.ShelfBook.objects.filter( shelf__user=context["request"].user, book__in=book.parent_work.editions.all() - ).first() + ).select_related("book", "shelf").first() return shelf if shelf else {"book": book} From e5ff4ac36d314b0f9047ea7cdbc790383bb197ff Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 May 2021 18:02:14 -0700 Subject: [PATCH 3/8] Python formatting --- bookwyrm/templatetags/bookwyrm_tags.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 072ff6634..3f33643bf 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -69,9 +69,14 @@ def related_status(notification): @register.simple_tag(takes_context=True) def active_shelf(context, book): """check what shelf a user has a book on, if any""" - shelf = models.ShelfBook.objects.filter( - shelf__user=context["request"].user, book__in=book.parent_work.editions.all() - ).select_related("book", "shelf").first() + shelf = ( + models.ShelfBook.objects.filter( + shelf__user=context["request"].user, + book__in=book.parent_work.editions.all(), + ) + .select_related("book", "shelf") + .first() + ) return shelf if shelf else {"book": book} From 7a4c4248df14e703a21477492b9612ca1a84d892 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 May 2021 18:34:34 -0700 Subject: [PATCH 4/8] Improves status template performance Reduces queries about about 65 --- bookwyrm/activitystreams.py | 2 ++ bookwyrm/templates/layout.html | 6 +++-- bookwyrm/templates/snippets/boost_button.html | 24 ++++++++++++++++--- bookwyrm/templates/snippets/fav_button.html | 6 +++-- .../templates/snippets/status/layout.html | 2 +- .../snippets/status/status_options.html | 4 ++-- bookwyrm/templatetags/interaction.py | 8 ++----- bookwyrm/templatetags/status_display.py | 7 +++++- 8 files changed, 42 insertions(+), 17 deletions(-) diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index 86321cd83..ff3c55fbd 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -55,6 +55,8 @@ class ActivityStream(RedisStore): return ( models.Status.objects.select_subclasses() .filter(id__in=statuses) + .select_related("user", "reply_parent") + .prefetch_related("mention_books", "mention_users") .order_by("-published_date") ) diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index d5a2087e5..1158d82db 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -134,12 +134,14 @@ {% trans "Notifications" %} + {% with request.user.unread_notification_count as notification_count %} - {{ request.user.unread_notification_count }} + {{ notification_count }} + {% endwith %} {% else %} diff --git a/bookwyrm/templates/snippets/boost_button.html b/bookwyrm/templates/snippets/boost_button.html index 5b84ea44e..0f9274068 100644 --- a/bookwyrm/templates/snippets/boost_button.html +++ b/bookwyrm/templates/snippets/boost_button.html @@ -3,14 +3,31 @@ {% load i18n %} {% with status.id|uuid as uuid %} - +{% with request.user|boosted:status as boosted %} + {% csrf_token %} - -
+ {% csrf_token %}
{% endwith %} +{% endwith %} diff --git a/bookwyrm/templates/snippets/fav_button.html b/bookwyrm/templates/snippets/fav_button.html index 40fa2d25f..6c4cfe9aa 100644 --- a/bookwyrm/templates/snippets/fav_button.html +++ b/bookwyrm/templates/snippets/fav_button.html @@ -3,7 +3,8 @@ {% load i18n %} {% with status.id|uuid as uuid %} -
+{% with request.user|liked:status as liked %} + {% csrf_token %}
-
+ {% csrf_token %}
{% endwith %} +{% endwith %} diff --git a/bookwyrm/templates/snippets/status/layout.html b/bookwyrm/templates/snippets/status/layout.html index 6fd7ebde6..b1d361fe0 100644 --- a/bookwyrm/templates/snippets/status/layout.html +++ b/bookwyrm/templates/snippets/status/layout.html @@ -15,7 +15,7 @@