Adds very simple author pages

This commit is contained in:
Mouse Reeve 2020-02-10 22:32:03 -08:00
parent 0f1240ca1f
commit 964c56079c
5 changed files with 36 additions and 3 deletions

View file

@ -19,11 +19,12 @@ def book_search(query):
for doc in data['docs'][:5]: for doc in data['docs'][:5]:
key = doc['key'] key = doc['key']
key = key.split('/')[-1] key = key.split('/')[-1]
author = doc.get('author_name') or ['Unknown']
results.append({ results.append({
'title': doc.get('title'), 'title': doc.get('title'),
'olkey': key, 'olkey': key,
'year': doc.get('first_publish_year'), 'year': doc.get('first_publish_year'),
'author': doc.get('author_name')[0], 'author': author[0],
}) })
return results return results
@ -68,7 +69,7 @@ def get_or_create_book(olkey, user=None, update=False):
author_id = author_id.split('/')[-1] author_id = author_id.split('/')[-1]
book.authors.add(get_or_create_author(author_id)) book.authors.add(get_or_create_author(author_id))
if data['covers'] and len(data['covers']): if data.get('covers') and len(data['covers']):
book.cover.save(*get_cover(data['covers'][0]), save=True) book.cover.save(*get_cover(data['covers'][0]), save=True)
return book return book

View file

@ -0,0 +1,15 @@
{% extends 'layout.html' %}
{% load fr_display %}
{% block content %}
<div id="content">
<div>
<h2>{{ author.data.name }}</h2>
{% for book in books %}
<div class="book-preview">
{% include 'snippets/book.html' with book=book size=large description=True %}
</div>
{% endfor %}
</div>
</div>
{% endblock %}

View file

@ -3,7 +3,7 @@
<p class="title"> <p class="title">
<a href="/book/{{ book.openlibrary_key }}">{{ book.data.title }}</a> <a href="/book/{{ book.openlibrary_key }}">{{ book.data.title }}</a>
</p> </p>
<p>by <a href="/author/{{ book.author.id }}" class="author">{{ book.authors.first.data.name }}</a></p> <p>by <a href="/author/{{ book.authors.first.openlibrary_key }}" class="author">{{ book.authors.first.data.name }}</a></p>
{% if rating %} {% if rating %}
{{ rating | stars }} {{ rating }} {{ rating | stars }} {{ rating }}

View file

@ -29,6 +29,7 @@ urlpatterns = [
re_path(r'^user/(?P<username>[\w@\.]+)/?$', views.user_profile), re_path(r'^user/(?P<username>[\w@\.]+)/?$', views.user_profile),
re_path(r'^user/(?P<username>\w+)/edit/?$', views.user_profile_edit), re_path(r'^user/(?P<username>\w+)/edit/?$', views.user_profile_edit),
re_path(r'^book/(?P<book_identifier>\w+)/?$', views.book_page), re_path(r'^book/(?P<book_identifier>\w+)/?$', views.book_page),
re_path(r'^author/(?P<author_identifier>\w+)/?$', views.author_page),
# internal action endpoints # internal action endpoints
re_path(r'^review/?$', views.review), re_path(r'^review/?$', views.review),

View file

@ -190,6 +190,22 @@ def book_page(request, book_identifier):
return TemplateResponse(request, 'book.html', data) return TemplateResponse(request, 'book.html', data)
@login_required
def author_page(request, author_identifier):
''' landing page for an author '''
try:
author = models.Author.objects.get(openlibrary_key=author_identifier)
except ValueError:
return HttpResponseNotFound()
books = models.Book.objects.filter(authors=author)
data = {
'author': author,
'books': books,
}
return TemplateResponse(request, 'author.html', data)
@login_required @login_required
def shelve(request, shelf_id, book_id, reshelve=True): def shelve(request, shelf_id, book_id, reshelve=True):
''' put a book on a user's shelf ''' ''' put a book on a user's shelf '''