Adds shelf page

Works on #25
This commit is contained in:
Mouse Reeve 2020-02-21 16:03:05 -08:00
parent 3eb91980e0
commit 6c629be667
8 changed files with 105 additions and 65 deletions

View file

@ -14,7 +14,7 @@
</div>
{% endfor %}
{% if shelf.size > shelf.books.count %}
<a href="/shelf/{{ shelf.identifier }}">See all {{ shelf.size }}</a>
<a href="/shelf/{{ user | username }}/{{ shelf.identifier }}">See all {{ shelf.size }}</a>
{% endif %}
{% endfor %}

View file

@ -0,0 +1,10 @@
{% extends 'layout.html' %}
{% block content %}
<div id="content">
<div>
<h2>{% include 'snippets/username.html' %} > {{ shelf.name }}</h2>
{% include 'snippets/shelf.html' with shelf=shelf ratings=ratings %}
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,61 @@
{% load humanize %}
{% load fr_display %}
{% if shelf.books %}
<table>
<tr class="book-preview">
<th>
Cover
</th>
<th>
Title
</th>
<th>
Author
</th>
<th>
Published
</th>
<th>
Shelved
</th>
<th>
External links
</th>
{% if ratings %}
<th>
Rating
</th>
{% endif %}
</tr>
{% for book in shelf.books.all %}
<tr class="book-preview">
<td>
{% include 'snippets/book_cover.html' with book=book %}
</td>
<td>
<a href="/book/{{ book.openlibrary_key }}">{{ book.data.title }}</a>
</td>
<td>
{{ book.authors.first.data.name }}
</td>
<td>
{{ book.data.first_publish_date }}
</td>
<td>
{{ book.added_date | naturalday }}
</td>
<td>
<a href="https://openlibrary.org{{ book.data.key }}" target="_blank">OpenLibrary</a>
</td>
{% if ratings %}
<td>
{{ ratings | dict_key:book.id | stars}}
</td>
{% endif %}
</tr>
{% endfor %}
</table>
{% else %}
<p>This shelf is empty.</p>
{% endif %}

View file

@ -1,2 +1,2 @@
<a href="/user/{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}" class="user">{% if user.name %}{{ user.name }}{% elif user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</a>
{% load fr_display %}
<a href="/user/{{ user | username }}" class="user">{% if user.name %}{{ user.name }}{% else %}{{ user | username }}{% endif %}</a>

View file

@ -1,6 +1,4 @@
{% extends 'layout.html' %}
{% load humanize %}
{% load fr_display %}
{% block content %}
<div id="sidebar">
<div class="user-profile">
@ -59,60 +57,7 @@
{% for shelf in shelves %}
<div>
<h2>{{ shelf.name }}</h2>
{% if shelf.books %}
<table>
<tr class="book-preview">
<th>
Cover
</th>
<th>
Title
</th>
<th>
Author
</th>
<th>
Published
</th>
<th>
Shelved
</th>
<th>
External links
</th>
<th>
Rating
</th>
</tr>
{% for book in shelf.books.all %}
<tr class="book-preview">
<td>
{% include 'snippets/book_cover.html' with book=book %}
</td>
<td>
<a href="/book/{{ book.openlibrary_key }}">{{ book.data.title }}</a>
</td>
<td>
{{ book.authors.first.data.name }}
</td>
<td>
{{ book.data.first_publish_date }}
</td>
<td>
{{ book.added_date | naturalday }}
</td>
<td>
<a href="https://openlibrary.org{{ book.data.key }}" target="_blank">OpenLibrary</a>
</td>
<td>
{{ ratings | dict_key:book.id | stars}}
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>This shelf is empty.</p>
{% endif %}
{% include 'snippets/shelf.html' with shelf=shelf ratings=ratings %}
</div>
{% endfor %}
</div>

View file

@ -39,6 +39,12 @@ def bio_format(bio):
return bio[0].strip()
@register.filter(name='username')
def get_user_identifier(user):
''' use localname for local users, username for remote '''
return user.localname if user.localname else user.username
@register.simple_tag(takes_context=True)
def shelve_button_identifier(context, book):
''' check what shelf a user has a book on, if any '''
@ -73,3 +79,4 @@ def shelve_button_text(context, book):
elif identifier == 'reading':
return 'I\'m done!'
return 'Want to read'

View file

@ -28,7 +28,6 @@ urlpatterns = [
r'^user/(?P<username>\w+)/(status|review)/(?P<status_id>\d+)/replies/?$',
incoming.get_replies
),
# TODO: shelves need pages in the UI and for their activitypub Collection
# .well-known endpoints
re_path(r'^.well-known/webfinger/?$', wellknown.webfinger),
@ -49,6 +48,7 @@ urlpatterns = [
re_path(r'^book/(?P<book_identifier>\w+)/?$', views.book_page),
re_path(r'^author/(?P<author_identifier>\w+)/?$', views.author_page),
re_path(r'^tag/(?P<tag_id>[\w-]+)/?$', views.tag_page),
re_path(r'^shelf/(?P<username>[\w@\.-]+)/(?P<shelf_identifier>[\w-]+)/?$', views.shelf_page),
# internal action endpoints
re_path(r'^review/?$', views.review),

View file

@ -143,10 +143,7 @@ def user_profile(request, username):
# otherwise we're at a UI view
try:
user = models.User.objects.get(localname=username)
except models.User.DoesNotExist:
try:
user = models.User.objects.get(username=username)
user = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
@ -163,6 +160,14 @@ def user_profile(request, username):
}
return TemplateResponse(request, 'user.html', data)
def get_user_from_username(username):
''' resolve a localname or a username to a user '''
try:
user = models.User.objects.get(localname=username)
except models.User.DoesNotExist:
user = models.User.objects.get(username=username)
return user
@login_required
def user_profile_edit(request, username):
@ -256,6 +261,18 @@ def tag_page(request, tag_id):
return TemplateResponse(request, 'tag.html', data)
def shelf_page(request, username, shelf_identifier):
''' display a shelf '''
# TODO: json view
try:
user = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
return TemplateResponse(request, 'shelf.html', {'shelf': shelf})
@login_required
def shelve(request, username, shelf_id, book_id, reshelve=True):
''' put a book on a user's shelf '''