Re-add tags to book page

This commit is contained in:
Mouse Reeve 2020-04-04 13:12:15 -07:00
parent f35cae49b3
commit 630c94a34a
6 changed files with 41 additions and 30 deletions

View file

@ -49,9 +49,9 @@ class Connector(AbstractConnector):
annotated with work data. ''' annotated with work data. '''
try: try:
book = models.Book.objects.select_subclasses().get( book = models.Book.objects.select_subclasses().filter(
openlibrary_key=olkey openlibrary_key=olkey
) ).first()
return book return book
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
@ -119,7 +119,7 @@ class Connector(AbstractConnector):
author_id = author_blob['key'] author_id = author_blob['key']
author_id = author_id.split('/')[-1] author_id = author_id.split('/')[-1]
book.authors.add(self.get_or_create_author(author_id)) book.authors.add(self.get_or_create_author(author_id))
if not data.get('authors'): if not data.get('authors') and book.parent_work.authors.count():
book.authors.set(book.parent_work.authors.all()) book.authors.set(book.parent_work.authors.all())
@ -166,7 +166,7 @@ class Connector(AbstractConnector):
try: try:
models.Edition.objects.get(openlibrary_key=olkey) models.Edition.objects.get(openlibrary_key=olkey)
except ObjectDoesNotExist: except models.Edition.DoesNotExist:
book = models.Edition.objects.create(openlibrary_key=olkey) book = models.Edition.objects.create(openlibrary_key=olkey)
self.update_from_data(book, data) self.update_from_data(book, data)
@ -177,7 +177,7 @@ class Connector(AbstractConnector):
raise ValueError('Invalid OpenLibrary author ID') raise ValueError('Invalid OpenLibrary author ID')
try: try:
return models.Author.objects.get(openlibrary_key=olkey) return models.Author.objects.get(openlibrary_key=olkey)
except ObjectDoesNotExist: except models.Author.DoesNotExist:
pass pass
response = requests.get('%s/authors/%s.json' % (self.url, olkey)) response = requests.get('%s/authors/%s.json' % (self.url, olkey))

View file

@ -546,15 +546,17 @@ dd {
} }
.tag { .tag {
border: 1px solid black;
display: inline-block; display: inline-block;
padding: 0.2em; padding: 0.2em;
border-radius: 0.2em; border-radius: 0.2em;
background-color: #F3FFBD; background-color: #EEE;
} }
.tag form { .tag form {
display: inline; display: inline;
} }
.tag a {
text-decoration: none;
}
blockquote { blockquote {
white-space: pre-line; white-space: pre-line;

View file

@ -48,14 +48,8 @@
<p><a href="/editions/{{ book.parent_work.id }}">{{ book.parent_work.edition_set.count }} editions</a></p> <p><a href="/editions/{{ book.parent_work.id }}">{{ book.parent_work.edition_set.count }} editions</a></p>
{% endif %} {% endif %}
<div id="tag-cloud"> {% if request.user.is_authenticated %}
{% for tag in tags %}
{% include 'snippets/tag.html' with tag=tag user=request.user user_tags=user_tag_names %}
{% endfor %}
</div>
<div> <div>
{% if request.user.is_authenticated %}
<h3>Leave a review</h3> <h3>Leave a review</h3>
<form class="review-form" name="review" action="/review/" method="post"> <form class="review-form" name="review" action="/review/" method="post">
{% csrf_token %} {% csrf_token %}
@ -64,8 +58,24 @@
{{ review_form.as_p }} {{ review_form.as_p }}
<button type="submit">Post review</button> <button type="submit">Post review</button>
</form> </form>
{% endif %}
</div> </div>
<div>
<h3>Tags</h3>
<form name="tag" action="/tag/" method="post">
{% csrf_token %}
<input type="hidden" name="book" value="{{ book.fedireads_key }}"></input>
<input type="text" name="name"></input>
<button type="submit">Add tag</button>
</form>
</div>
<div class="tag-cloud">
{% for tag in tags %}
{% include 'snippets/tag.html' with book=book tag=tag user_tags=user_tags %}
{% endfor %}
</div>
{% endif %}
</div> </div>
</div> </div>

View file

@ -3,9 +3,9 @@
{% block content %} {% block content %}
<div class="content-container"> <div class="content-container">
<h2>Recently Added Books</h2> <h2>Recently Added Books</h2>
<div class="book-grid row wrap"> <div class="book-grid row wrap shrink">
{% for book in books %} {% for book in books %}
<div class="book-preview"> <div class="cover-container">
<a href="{{ book.absolute_id }}"> <a href="{{ book.absolute_id }}">
{% include 'snippets/book_cover.html' with book=book %} {% include 'snippets/book_cover.html' with book=book %}
</a> </a>

View file

@ -1,12 +1,16 @@
{% extends 'layout.html' %} {% extends 'layout.html' %}
{% load fr_display %} {% load fr_display %}
{% block content %} {% block content %}
<div id="content"> <div class="content-container">
<div> <h2>Books tagged "{{ tag.name }}"</h2>
<h2>Books tagged "{{ tag.name }}"</h2> <div class="book-grid row wrap shrink">
{% for book in books.all %} {% for book in books.all %}
<div class="book-preview grid"> <div class="cover-container">
{% include 'snippets/book.html' with book=book size=large %} <a href="{{ book.absolute_id }}">
{% include 'snippets/book_cover.html' with book=book %}
</a>
{% include 'snippets/rate_action.html' with user=request.user book=book %}
{% include 'snippets/shelve_button.html' with book=book %}
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

View file

@ -354,6 +354,8 @@ def book_page(request, book_identifier, tab='friends'):
book = book.default_edition book = book.default_edition
work = book.parent_work work = book.parent_work
if not work:
return HttpResponseNotFound()
book_reviews = models.Review.objects.filter(book__in=work.edition_set.all()) book_reviews = models.Review.objects.filter(book__in=work.edition_set.all())
@ -375,18 +377,13 @@ def book_page(request, book_identifier, tab='friends'):
user_tags = models.Tag.objects.filter( user_tags = models.Tag.objects.filter(
book=book, user=request.user book=book, user=request.user
).all() ).values_list('identifier', flat=True)
user_tag_names = user_tags.values_list('identifier', flat=True)
user_rating = user_reviews.aggregate(Avg('rating')),
else: else:
tab = 'public' tab = 'public'
reviews = book_reviews.filter(privacy='public') reviews = book_reviews.filter(privacy='public')
shelf = None shelf = None
user_reviews = [] user_reviews = []
user_rating = None
user_tags = [] 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(
@ -399,12 +396,10 @@ 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_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_tag_names,
'review_form': forms.ReviewForm(), 'review_form': forms.ReviewForm(),
'tag_form': forms.TagForm(), 'tag_form': forms.TagForm(),
'feed_tabs': [ 'feed_tabs': [