Use recent reviews instead of most reviewed books

This commit is contained in:
Mouse Reeve 2021-01-03 14:28:28 -08:00
parent 8e923d6333
commit 0247f89c0b
3 changed files with 27 additions and 12 deletions

View file

@ -1,8 +1,9 @@
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
{% if book %}
<div class="columns"> <div class="columns">
<div class="column is-narrow"> <div class="column is-narrow">
{% include 'snippets/book_cover.html' with book=book size="large" %} {% include 'snippets/book_cover.html' with book=book size="large" %}
{% include 'snippets/stars.html' with rating=book.review__rating__avg %} {% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %}
</div> </div>
<div class="column"> <div class="column">
<h3 class="title is-5"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3> <h3 class="title is-5"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3>
@ -12,3 +13,4 @@
<blockquote class="content">{{ book|book_description|to_markdown|safe|truncatewords_html:50 }}</blockquote> <blockquote class="content">{{ book|book_description|to_markdown|safe|truncatewords_html:50 }}</blockquote>
</div> </div>
</div> </div>
{% endif %}

View file

@ -1,8 +1,11 @@
{% load bookwyrm_tags %}
{% if book %}
{% include 'snippets/book_cover.html' with book=book %} {% include 'snippets/book_cover.html' with book=book %}
{% include 'snippets/stars.html' with rating=book.review__rating__avg %} {% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %}
<h3 class="title is-6"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3> <h3 class="title is-6"><a href="/book/{{ book.id }}">{{ book.title }}</a></h3>
{% if book.authors %} {% if book.authors %}
<p class="subtitle is-6">by {% include 'snippets/authors.html' with book=book %}</p> <p class="subtitle is-6">by {% include 'snippets/authors.html' with book=book %}</p>
{% endif %} {% endif %}
{% endif %}

View file

@ -4,7 +4,7 @@ import re
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.postgres.search import TrigramSimilarity from django.contrib.postgres.search import TrigramSimilarity
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import Avg, Count, Q from django.db.models import Avg, F, Q, Max
from django.db.models.functions import Greatest from django.db.models.functions import Greatest
from django.http import HttpResponseNotFound, JsonResponse from django.http import HttpResponseNotFound, JsonResponse
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
@ -64,11 +64,12 @@ def not_found_page(request, _):
request, 'notfound.html', {'title': 'Not found'}, status=404) request, 'notfound.html', {'title': 'Not found'}, status=404)
@login_required
@require_GET @require_GET
def home(request): def home(request):
''' this is the same as the feed on the home tab ''' ''' this is the same as the feed on the home tab '''
if request.user.is_authenticated:
return home_tab(request, 'home') return home_tab(request, 'home')
return discover_page(request)
@login_required @login_required
@ -134,19 +135,28 @@ def get_suggested_books(user, max_books=5):
@require_GET @require_GET
def discover_page(request): def discover_page(request):
''' tiled book activity page ''' ''' tiled book activity page '''
books = models.Edition.objects.exclude( books = models.Edition.objects.filter(
review__published_date__isnull=False,
review__user__local=True
).exclude(
cover__exact='' cover__exact=''
).annotate( ).annotate(
Count('review') Max('review__published_date')
).annotate( ).order_by('-review__published_date__max')[:6]
Avg('review__rating')
).order_by('-review__count')
ratings = {}
for book in books:
reviews = models.Review.objects.filter(
book__in=book.parent_work.editions.all()
)
reviews = get_activity_feed(
request.user, 'federated', model=reviews)
ratings[book.id] = reviews.aggregate(Avg('rating'))['rating__avg']
data = { data = {
'title': 'Discover', 'title': 'Discover',
'login_form': forms.LoginForm(),
'register_form': forms.RegisterForm(), 'register_form': forms.RegisterForm(),
'books': books 'books': list(set(books)),
'ratings': ratings
} }
return TemplateResponse(request, 'discover.html', data) return TemplateResponse(request, 'discover.html', data)