From b763b7469bbb4c95e2a0efb859a02c8429c38fe7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 6 Nov 2020 20:36:52 -0800 Subject: [PATCH] Show shelf names with suggested books --- bookwyrm/static/js/shared.js | 10 +++-- bookwyrm/templates/feed.html | 33 +++++++++++---- .../templates/snippets/shelve_button.html | 6 +-- bookwyrm/views.py | 41 ++++++------------- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/bookwyrm/static/js/shared.js b/bookwyrm/static/js/shared.js index 9b16174e7..eb4ab677b 100644 --- a/bookwyrm/static/js/shared.js +++ b/bookwyrm/static/js/shared.js @@ -34,13 +34,15 @@ function rate_stars(e) { function tabChange(e) { var target = e.target.closest('li') var identifier = target.getAttribute('data-id'); + var parent_element = target.parentElement.closest('li').parentElement; - var tabs = target.parentElement.children; + var tabs = parent_element.getElementsByTagName('label'); for (i = 0; i < tabs.length; i++) { - if (tabs[i].getAttribute('data-id') == identifier) { - tabs[i].className += ' is-active'; + var tab = tabs[i].parentElement; + if (tab.getAttribute('data-id') == identifier) { + tab.className += ' is-active'; } else { - tabs[i].className = tabs[i].className.replace('is-active', ''); + tab.className = tab.className.replace('is-active', ''); } } diff --git a/bookwyrm/templates/feed.html b/bookwyrm/templates/feed.html index 3003f5187..ea62a6772 100644 --- a/bookwyrm/templates/feed.html +++ b/bookwyrm/templates/feed.html @@ -4,22 +4,37 @@
-

Suggested books

+

Your books

{% if not suggested_books %}

There are no books here right now! Try searching for a book to get started

{% else %} -
+
- {% for book in suggested_books %} + {% for shelf in suggested_books %} + {% with shelf_counter=forloop.counter %} + {% for book in shelf.books %}
- + {% endfor %} + {% endwith %} + {% endfor %} {% endif %}
diff --git a/bookwyrm/templates/snippets/shelve_button.html b/bookwyrm/templates/snippets/shelve_button.html index b09ba2974..d295bb795 100644 --- a/bookwyrm/templates/snippets/shelve_button.html +++ b/bookwyrm/templates/snippets/shelve_button.html @@ -68,7 +68,7 @@
@@ -111,13 +111,13 @@
diff --git a/bookwyrm/views.py b/bookwyrm/views.py index 9794d5857..84581bf1a 100644 --- a/bookwyrm/views.py +++ b/bookwyrm/views.py @@ -60,34 +60,19 @@ def home_tab(request, tab): except ValueError: page = 1 - count = 5 - querysets = [ - # recemt currently reading - models.Edition.objects.filter( - shelves__user=request.user, - shelves__identifier='reading' - ), - # read - models.Edition.objects.filter( - shelves__user=request.user, - shelves__identifier='read' - ).order_by('-updated_date')[:2], - # to-read - models.Edition.objects.filter( - shelves__user=request.user, - shelves__identifier='to-read' - ), - # popular books - models.Edition.objects.annotate( - shelf_count=Count('shelves') - ).order_by('-shelf_count') - ] + max_books = 5 + book_count = 0 + preset_shelves = ['reading', 'read', 'to-read'] suggested_books = [] - for queryset in querysets: - length = count - len(suggested_books) - suggested_books += list(queryset[:length]) - if len(suggested_books) >= count: - break + for preset in preset_shelves: + limit = max_books - book_count + shelf = request.user.shelf_set.get(identifier=preset) + shelf_preview = { + 'name': shelf.name, + 'books': shelf.books.all()[:limit] + } + suggested_books.append(shelf_preview) + book_count += len(shelf_preview['books']) activities = get_activity_feed(request.user, tab) @@ -99,7 +84,7 @@ def home_tab(request, tab): data = { 'title': 'Updates Feed', 'user': request.user, - 'suggested_books': set(suggested_books), + 'suggested_books': suggested_books, 'activities': activities, 'review_form': forms.ReviewForm(), 'quotation_form': forms.QuotationForm(),