From acd922970a3e4109dc9d20fa41493f8982ab7d66 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 31 Jan 2021 10:34:25 -0800 Subject: [PATCH] Add to and edit lists --- bookwyrm/templates/lists/form.html | 45 ++++++++++++++++++ bookwyrm/templates/lists/list.html | 50 ++++++++++++++++++-- bookwyrm/templates/lists/lists.html | 45 +----------------- bookwyrm/templates/snippets/rate_action.html | 2 +- bookwyrm/templates/snippets/stars.html | 7 ++- bookwyrm/templatetags/bookwyrm_tags.py | 14 +++++- bookwyrm/urls.py | 2 +- bookwyrm/views/list.py | 44 +++++++++++++++-- 8 files changed, 149 insertions(+), 60 deletions(-) create mode 100644 bookwyrm/templates/lists/form.html diff --git a/bookwyrm/templates/lists/form.html b/bookwyrm/templates/lists/form.html new file mode 100644 index 000000000..af0b8abf6 --- /dev/null +++ b/bookwyrm/templates/lists/form.html @@ -0,0 +1,45 @@ +{% csrf_token %} + + +
+
+
+ + {{ list_form.name }} +
+
+ + {{ list_form.description }} +
+
+
+
+ List curation: + + + + + + +
+
+
+
+
+ {% include 'snippets/privacy_select.html' with current=list.privacy %} +
+
+ + {% include 'snippets/toggle/close_button.html' with controls_text='create-list' text="Cancel" %} +
+
+ diff --git a/bookwyrm/templates/lists/list.html b/bookwyrm/templates/lists/list.html index 854ee4c56..2260acb5d 100644 --- a/bookwyrm/templates/lists/list.html +++ b/bookwyrm/templates/lists/list.html @@ -1,28 +1,66 @@ {% extends 'layout.html' %} +{% load bookwyrm_tags %} {% block content %}

{{ list.name }} {% include 'snippets/privacy-icons.html' with item=list %}

+

Created by {% include 'snippets/username.html' with user=list.user %}

{% include 'snippets/trimmed_text.html' with full=list.description %}
+ {% if request.user == list.user %}
- {% include 'snippets/toggle/open_button.html' with text="Edit list" icon="pencil" controls_text="edit-list" %} + {% include 'snippets/toggle/open_button.html' with text="Edit list" icon="pencil" controls_text="create-list" %}
+ {% endif %}
+ + +
-
-

Books

+
{% if not list.books.exists %}

This list is currently empty

{% else %} - {% for book in list.books.all %} - {{ book }} +
    + {% for item in list.listitem_set.all %} +
  1. +
    +
    + {% include 'snippets/book_cover.html' with book=item.book size="medium" %} +
    + {% include 'snippets/book_titleby.html' with book=item.book %} + {% include 'snippets/stars.html' with rating=item.book|rating:request.user %} + {% include 'snippets/shelve_button.html' with book=item.book %} +
    +
    + {% if item.note %} +
    + {% include 'snippets/trimmed_text.html' with full=item.note %} +
    + {% endif %} + +
    +
  2. {% endfor %} +
{% endif %}
+ {% if not list.curation == 'closed' or request.user == list.user %}

Add Books

{% for book in suggested_books %} @@ -34,11 +72,13 @@

{% include 'snippets/book_titleby.html' with book=book %}

{% csrf_token %} +
{% endfor %} + {% endif %} {% endblock %} diff --git a/bookwyrm/templates/lists/lists.html b/bookwyrm/templates/lists/lists.html index 8f8f7e199..1f230b783 100644 --- a/bookwyrm/templates/lists/lists.html +++ b/bookwyrm/templates/lists/lists.html @@ -18,50 +18,7 @@ {% if request.user.list_set.exists %} diff --git a/bookwyrm/templates/snippets/rate_action.html b/bookwyrm/templates/snippets/rate_action.html index 7a82501ab..833f2b88b 100644 --- a/bookwyrm/templates/snippets/rate_action.html +++ b/bookwyrm/templates/snippets/rate_action.html @@ -13,7 +13,7 @@ {% for i in '12345'|make_list %} - + diff --git a/bookwyrm/templates/snippets/stars.html b/bookwyrm/templates/snippets/stars.html index b73a9b2bc..7d5b63d73 100644 --- a/bookwyrm/templates/snippets/stars.html +++ b/bookwyrm/templates/snippets/stars.html @@ -1,8 +1,7 @@ -
+

{% if rating %}{{ rating|floatformat }} star{{ rating|floatformat | pluralize }}{% else %}No rating{% endif %} {% for i in '12345'|make_list %} - + {% endfor %} -

- +

diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 08bbcafed..6dea32e9d 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -4,9 +4,10 @@ from datetime import datetime from dateutil.relativedelta import relativedelta from django import template +from django.db.models import Avg from django.utils import timezone -from bookwyrm import models +from bookwyrm import models, views from bookwyrm.views.status import to_markdown @@ -20,6 +21,17 @@ def dict_key(d, k): @register.filter(name='rating') def get_rating(book, user): + ''' get the overall rating of a book ''' + queryset = views.helpers.get_activity_feed( + user, + ['public', 'followers', 'unlisted', 'direct'], + queryset=models.Review.objects.filter(book=book), + ) + return queryset.aggregate(Avg('rating'))['rating__avg'] + + +@register.filter(name='user_rating') +def get_user_rating(book, user): ''' get a user's rating of a book ''' rating = models.Review.objects.filter( user=user, diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 3d6a37da3..7b0975c0b 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -91,7 +91,7 @@ urlpatterns = [ re_path(r'^list/(?P\d+)(.json)?/?$', views.List.as_view(), name='list'), re_path(r'^list/(?P\d+)/add/?$', - views.List.as_view(), name='list-add-book'), + views.list.add_book, name='list-add-book'), # preferences re_path(r'^preferences/profile/?$', views.EditUser.as_view()), diff --git a/bookwyrm/views/list.py b/bookwyrm/views/list.py index b4da1a87e..63f99c4aa 100644 --- a/bookwyrm/views/list.py +++ b/bookwyrm/views/list.py @@ -1,11 +1,12 @@ ''' book list views''' from django.contrib.auth.decorators import login_required from django.db.models import Q -from django.http import HttpResponseNotFound +from django.http import HttpResponseNotFound, HttpResponseBadRequest from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View +from django.views.decorators.http import require_POST from bookwyrm import forms, models from bookwyrm.activitypub import ActivitypubResponse @@ -35,7 +36,6 @@ class Lists(View): ''' create a book_list ''' form = forms.ListForm(request.POST) if not form.is_valid(): - print(form.errors) return redirect('lists') book_list = form.save() return redirect(book_list.local_path) @@ -52,14 +52,15 @@ class List(View): if is_api_request(request): return ActivitypubResponse(book_list.to_activity()) - suggestions = request.user.shelfbook_set.all().filter( - ~Q(book__in=book_list.books) + suggestions = request.user.shelfbook_set.filter( + ~Q(book__in=book_list.books.all()) ) data = { 'title': '%s | Lists' % book_list.name, 'list': book_list, 'suggested_books': [s.book for s in suggestions[:5]], + 'list_form': forms.ListForm(instance=book_list), } return TemplateResponse(request, 'lists/list.html', data) @@ -69,4 +70,39 @@ class List(View): def post(self, request, list_id): ''' edit a book_list ''' book_list = get_object_or_404(models.List, id=list_id) + form = forms.ListForm(request.POST, instance=book_list) + if not form.is_valid(): + return redirect('list', book_list.id) + book_list = form.save() return redirect(book_list.local_path) + + +@require_POST +def add_book(request, list_id): + ''' put a book on a list ''' + book_list = get_object_or_404(models.List, id=list_id) + if not object_visible_to_user(request.user, book_list): + return HttpResponseNotFound() + + book = get_object_or_404(models.Edition, id=request.POST.get('book')) + # do you have permission to add to the list? + if request.user == book_list.user or book_list.curation == 'open': + # go ahead and add it + models.ListItem.objects.create( + book=book, + book_list=book_list, + added_by=request.user, + ) + elif book_list.curation == 'curated': + # make a pending entry + models.ListItem.objects.create( + approved=False, + book=book, + book_list=book_list, + added_by=request.user, + ) + else: + # you can't add to this list, what were you THINKING + return HttpResponseBadRequest() + + return redirect('list', list_id)