Get book page to work while not logged in.

This commit is contained in:
Adam Kelly 2020-03-27 15:05:27 +00:00
parent ea735fd570
commit b58a362df2
2 changed files with 43 additions and 28 deletions

View file

@ -25,6 +25,7 @@
</div> </div>
</div> </div>
{% if request.user.is_authenticated %}
<div class="content-container"> <div class="content-container">
<h2>Leave a review</h2> <h2>Leave a review</h2>
<form class="review-form" name="review" action="/review/" method="post"> <form class="review-form" name="review" action="/review/" method="post">
@ -38,6 +39,7 @@
<div class="content-container tabs"> <div class="content-container tabs">
{% include 'snippets/tabs.html' with tabs=feed_tabs active_tab=active_tab path=path %} {% include 'snippets/tabs.html' with tabs=feed_tabs active_tab=active_tab path=path %}
</div> </div>
{% endif %}
{% if not reviews %} {% if not reviews %}
<div class="content-container"> <div class="content-container">

View file

@ -306,32 +306,48 @@ def book_page(request, book_identifier, tab='friends'):
else: else:
book_reviews = models.Review.objects.filter(book=book) book_reviews = models.Review.objects.filter(book=book)
user_reviews = book_reviews.filter( if request.user.is_authenticated:
user=request.user, user_reviews = book_reviews.filter(
).all() user=request.user,
).all()
if tab == 'friends': if tab == 'friends':
reviews = book_reviews.filter( reviews = book_reviews.filter(
Q(user__followers=request.user, privacy='public') | \ Q(user__followers=request.user, privacy='public') | \
Q(user=request.user) | \ Q(user=request.user) | \
Q(mention_users=request.user), Q(mention_users=request.user),
) )
elif tab == 'local': elif tab == 'local':
reviews = book_reviews.filter( reviews = book_reviews.filter(
Q(privacy='public') | \ Q(privacy='public') | \
Q(mention_users=request.user), Q(mention_users=request.user),
user__local=True, user__local=True,
) )
else:
reviews = book_reviews.filter(
Q(privacy='public') | \
Q(mention_users=request.user),
)
try:
shelf = models.Shelf.objects.get(user=request.user, book=book)
except models.Shelf.DoesNotExist:
shelf = None
user_tags = models.Tag.objects.filter(
book=book, user=request.user
).all()
user_tag_names = user_tags.values_list('identifier', flat=True)
user_rating = user_reviews.aggregate(Avg('rating')),
else: else:
reviews = book_reviews.filter( tab = 'public'
Q(privacy='public') | \ reviews = book_reviews.filter(privacy='public')
Q(mention_users=request.user),
)
try:
shelf = models.Shelf.objects.get(user=request.user, book=book)
except models.Shelf.DoesNotExist:
shelf = None shelf = None
user_reviews = []
user_rating = None
user_tags = []
user_tag_names = []
rating = reviews.aggregate(Avg('rating')) rating = reviews.aggregate(Avg('rating'))
tags = models.Tag.objects.filter( tags = models.Tag.objects.filter(
@ -339,9 +355,6 @@ def book_page(request, book_identifier, tab='friends'):
).values( ).values(
'book', 'name', 'identifier' 'book', 'name', 'identifier'
).distinct().all() ).distinct().all()
user_tags = models.Tag.objects.filter(
book=book, user=request.user
).all()
review_form = forms.ReviewForm() review_form = forms.ReviewForm()
tag_form = forms.TagForm() tag_form = forms.TagForm()
@ -349,12 +362,12 @@ def book_page(request, book_identifier, tab='friends'):
'book': book, 'book': book,
'shelf': shelf, 'shelf': shelf,
'user_reviews': user_reviews, 'user_reviews': user_reviews,
'user_rating': user_reviews.aggregate(Avg('rating')), 'user_rating': user_rating,
'reviews': reviews.distinct(), 'reviews': reviews.distinct(),
'rating': rating['rating__avg'], 'rating': rating['rating__avg'],
'tags': tags, 'tags': tags,
'user_tags': user_tags, 'user_tags': user_tags,
'user_tag_names': user_tags.values_list('identifier', flat=True), 'user_tag_names': user_tag_names,
'review_form': review_form, 'review_form': review_form,
'tag_form': tag_form, 'tag_form': tag_form,
'feed_tabs': [ 'feed_tabs': [