From 3c52426a1ca0067e3bfbf61c4f8c5a6290b6e369 Mon Sep 17 00:00:00 2001 From: 0x29a Date: Sun, 29 Jan 2023 16:16:31 +0100 Subject: [PATCH 1/5] Optimize `get_suggested_books` --- bookwyrm/views/feed.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index 6d7aeb239..17218b93e 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -237,16 +237,24 @@ def feed_page_data(user): def get_suggested_books(user, max_books=5): """helper to get a user's recent books""" book_count = 0 - preset_shelves = [("reading", max_books), ("read", 2), ("to-read", max_books)] + preset_shelves = {"reading": max_books, "read": 2, "to-read": max_books} suggested_books = [] - for (preset, shelf_max) in preset_shelves: + + user_shelves = { + shelf.identifier: shelf + for shelf in user.shelf_set.filter( + identifier__in=preset_shelves.keys() + ).exclude(books__isnull=True) + } + + for preset, shelf_max in preset_shelves.items(): limit = ( shelf_max if shelf_max < (max_books - book_count) else max_books - book_count ) - shelf = user.shelf_set.get(identifier=preset) - if not shelf.books.exists(): + shelf = user_shelves.get(preset, None) + if not shelf: continue shelf_preview = { From 6ea251379075260d731e80de0e8876cc8831c660 Mon Sep 17 00:00:00 2001 From: 0x29a Date: Sun, 29 Jan 2023 16:25:29 +0100 Subject: [PATCH 2/5] Remove duplicated notifications request from layout.html --- bookwyrm/templates/layout.html | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index e838ead12..a8d06ee8d 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -25,6 +25,7 @@ {% block body %} From 6e1418c5d4d2f6c2b8f47a9fc2f36ebb489d3248 Mon Sep 17 00:00:00 2001 From: 0x29a Date: Sun, 29 Jan 2023 16:30:09 +0100 Subject: [PATCH 3/5] Remove duplicated mentions request from layout.html --- bookwyrm/templates/layout.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index a8d06ee8d..1227e39c4 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -25,7 +25,7 @@ {% block body %}