From 964c56079ca3531929cb64a853439e9232e6bf7f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 10 Feb 2020 22:32:03 -0800 Subject: [PATCH] Adds very simple author pages --- fedireads/openlibrary.py | 5 +++-- fedireads/templates/author.html | 15 +++++++++++++++ fedireads/templates/snippets/book.html | 2 +- fedireads/urls.py | 1 + fedireads/views.py | 16 ++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 fedireads/templates/author.html diff --git a/fedireads/openlibrary.py b/fedireads/openlibrary.py index 49b1e3bc..5a7fd799 100644 --- a/fedireads/openlibrary.py +++ b/fedireads/openlibrary.py @@ -19,11 +19,12 @@ def book_search(query): for doc in data['docs'][:5]: key = doc['key'] key = key.split('/')[-1] + author = doc.get('author_name') or ['Unknown'] results.append({ 'title': doc.get('title'), 'olkey': key, 'year': doc.get('first_publish_year'), - 'author': doc.get('author_name')[0], + 'author': author[0], }) return results @@ -68,7 +69,7 @@ def get_or_create_book(olkey, user=None, update=False): author_id = author_id.split('/')[-1] 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) return book diff --git a/fedireads/templates/author.html b/fedireads/templates/author.html new file mode 100644 index 00000000..7c54361e --- /dev/null +++ b/fedireads/templates/author.html @@ -0,0 +1,15 @@ +{% extends 'layout.html' %} +{% load fr_display %} +{% block content %} +
+
+

{{ author.data.name }}

+ {% for book in books %} +
+ {% include 'snippets/book.html' with book=book size=large description=True %} +
+ {% endfor %} +
+
+{% endblock %} + diff --git a/fedireads/templates/snippets/book.html b/fedireads/templates/snippets/book.html index 25259b6a..eec9b357 100644 --- a/fedireads/templates/snippets/book.html +++ b/fedireads/templates/snippets/book.html @@ -3,7 +3,7 @@

{{ book.data.title }}

-

by {{ book.authors.first.data.name }}

+

by {{ book.authors.first.data.name }}

{% if rating %} {{ rating | stars }} {{ rating }} diff --git a/fedireads/urls.py b/fedireads/urls.py index 390f6e63..7105089b 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -29,6 +29,7 @@ urlpatterns = [ re_path(r'^user/(?P[\w@\.]+)/?$', views.user_profile), re_path(r'^user/(?P\w+)/edit/?$', views.user_profile_edit), re_path(r'^book/(?P\w+)/?$', views.book_page), + re_path(r'^author/(?P\w+)/?$', views.author_page), # internal action endpoints re_path(r'^review/?$', views.review), diff --git a/fedireads/views.py b/fedireads/views.py index 11c2a95d..87c0b3b6 100644 --- a/fedireads/views.py +++ b/fedireads/views.py @@ -190,6 +190,22 @@ def book_page(request, book_identifier): 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 def shelve(request, shelf_id, book_id, reshelve=True): ''' put a book on a user's shelf '''