select suggested books for statuses

This commit is contained in:
Mouse Reeve 2020-09-30 15:00:46 -07:00
parent f196787c16
commit 9994bdc81e
3 changed files with 34 additions and 19 deletions

View file

@ -3,17 +3,18 @@
{% block content %}
<div class="columns">
<div class="column">
<div class="column is-one-third">
<h2 class="title is-4">Suggested books</h2>
<div class="tabs is-small is-toggle">
<ul>
{% for book in user_books|slice:3 %}
{% for book in suggested_books %}
<li class="{% if forloop.first %}is-active{% endif %}" data-id="tab-book-{{ book.id }}">
<label for="book-{{ book.id }}" onclick="tabChange(event)"><a>{% include 'snippets/book_cover.html' with book=book size="medium" %}</a></label>
</li>
{% endfor %}
</ul>
</div>
{% for book in user_books|slice:5 %}
{% for book in suggested_books %}
<div>
<input class="toggle-control" type="radio" name="recent-books" id="book-{{ book.id }}" {% if forloop.first %}checked{% endif %}>
<div class="toggle-content hidden">

View file

@ -143,18 +143,6 @@ def shelve_button_text(context, book):
return 'Want to read'
@register.simple_tag(takes_context=True)
def current_shelf(context, book):
''' check what shelf a user has a book on, if any '''
try:
shelf = models.ShelfBook.objects.get(
shelf__user=context['user'],
book=book
)
except models.ShelfBook.DoesNotExist:
return None
return shelf.name
@register.simple_tag(takes_context=False)
def latest_read_through(book, user):
''' the most recent read activity '''

View file

@ -2,7 +2,7 @@
import re
from django.contrib.auth.decorators import login_required
from django.db.models import Avg, Q
from django.db.models import Avg, Count, Q
from django.http import HttpResponseBadRequest, HttpResponseNotFound,\
JsonResponse
from django.core.exceptions import PermissionDenied
@ -56,8 +56,34 @@ def home_tab(request, tab):
except ValueError:
page = 1
# allows us to check if a user has shelved a book
user_books = models.Edition.objects.filter(shelves__user=request.user).all()
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'
)[: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')
]
suggested_books = []
for queryset in querysets:
length = count - len(suggested_books)
suggested_books += list(queryset[:length])
if len(suggested_books) >= count:
break
activities = get_activity_feed(request.user, tab)
@ -68,7 +94,7 @@ def home_tab(request, tab):
prev_page = '/?page=%d#feed' % (page - 1)
data = {
'user': request.user,
'user_books': user_books,
'suggested_books': suggested_books,
'activities': activities,
'review_form': forms.ReviewForm(),
'quotation_form': forms.QuotationForm(),