moviewyrm/bookwyrm/views/list.py

510 lines
17 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" book list views"""
from typing import Optional
from urllib.parse import urlencode
2021-01-31 05:33:41 +00:00
from django.contrib.auth.decorators import login_required
2021-02-01 01:34:06 +00:00
from django.core.paginator import Paginator
2021-04-08 16:05:21 +00:00
from django.db import IntegrityError, transaction
2021-11-16 17:21:12 +00:00
from django.db.models import Avg, DecimalField, Q, Max
2021-04-08 16:05:21 +00:00
from django.db.models.functions import Coalesce
from django.http import HttpResponseBadRequest, HttpResponse, Http404
2021-01-31 05:33:41 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.urls import reverse
2021-01-31 05:33:41 +00:00
from django.utils.decorators import method_decorator
from django.views import View
2021-01-31 18:34:25 +00:00
from django.views.decorators.http import require_POST
from django.views.decorators.clickjacking import xframe_options_exempt
2021-01-31 05:33:41 +00:00
from bookwyrm import book_search, forms, models
2021-01-31 05:33:41 +00:00
from bookwyrm.activitypub import ActivitypubResponse
2021-11-16 17:21:12 +00:00
from bookwyrm.lists_stream import ListsStream
from bookwyrm.settings import PAGE_LENGTH
2021-10-06 17:37:09 +00:00
from .helpers import is_api_request
2021-02-01 19:34:08 +00:00
from .helpers import get_user_from_username
2021-01-31 05:33:41 +00:00
2021-04-08 16:05:21 +00:00
2021-01-31 05:33:41 +00:00
# pylint: disable=no-self-use
class Lists(View):
2021-04-26 16:15:42 +00:00
"""book list page"""
2021-03-08 16:49:10 +00:00
2021-01-31 05:33:41 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""display a book list"""
2021-12-30 18:40:26 +00:00
lists = ListsStream().get_list_stream(request.user)
2021-02-01 01:34:06 +00:00
paginated = Paginator(lists, 12)
2021-01-31 16:08:52 +00:00
data = {
2021-04-19 22:01:20 +00:00
"lists": paginated.get_page(request.GET.get("page")),
2021-03-08 16:49:10 +00:00
"list_form": forms.ListForm(),
"path": "/list",
2021-01-31 16:08:52 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "lists/lists.html", data)
2021-01-31 05:33:41 +00:00
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-31 05:33:41 +00:00
# pylint: disable=unused-argument
def post(self, request):
2021-04-26 16:15:42 +00:00
"""create a book_list"""
2021-01-31 16:41:11 +00:00
form = forms.ListForm(request.POST)
if not form.is_valid():
2021-03-08 16:49:10 +00:00
return redirect("lists")
2021-01-31 16:41:11 +00:00
book_list = form.save()
# list should not have a group if it is not group curated
if not book_list.curation == "group":
book_list.group = None
book_list.save(broadcast=False)
2021-02-02 17:37:46 +00:00
2021-01-31 05:33:41 +00:00
return redirect(book_list.local_path)
2021-03-08 16:49:10 +00:00
2021-08-23 20:40:07 +00:00
@method_decorator(login_required, name="dispatch")
2021-08-23 22:07:38 +00:00
class SavedLists(View):
"""saved book list page"""
2021-08-23 20:40:07 +00:00
def get(self, request):
"""display book lists"""
# hide lists with no approved books
lists = request.user.saved_lists.order_by("-updated_date")
paginated = Paginator(lists, 12)
data = {
"lists": paginated.get_page(request.GET.get("page")),
"list_form": forms.ListForm(),
"path": "/list",
}
return TemplateResponse(request, "lists/lists.html", data)
@method_decorator(login_required, name="dispatch")
2021-02-01 19:34:08 +00:00
class UserLists(View):
2021-04-26 16:15:42 +00:00
"""a user's book list page"""
2021-03-08 16:49:10 +00:00
2021-02-01 19:34:08 +00:00
def get(self, request, username):
2021-04-26 16:15:42 +00:00
"""display a book list"""
user = get_user_from_username(request.user, username)
2021-10-06 17:37:09 +00:00
lists = models.List.privacy_filter(request.user).filter(user=user)
2021-02-01 19:34:08 +00:00
paginated = Paginator(lists, 12)
data = {
2021-03-08 16:49:10 +00:00
"user": user,
"is_self": request.user.id == user.id,
2021-04-19 22:01:20 +00:00
"lists": paginated.get_page(request.GET.get("page")),
2021-03-08 16:49:10 +00:00
"list_form": forms.ListForm(),
"path": user.local_path + "/lists",
2021-02-01 19:34:08 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "user/lists.html", data)
2021-02-01 19:34:08 +00:00
2021-01-31 05:33:41 +00:00
class List(View):
2021-04-26 16:15:42 +00:00
"""book list page"""
2021-03-08 16:49:10 +00:00
2021-01-31 05:33:41 +00:00
def get(self, request, list_id):
2021-04-26 16:15:42 +00:00
"""display a book list"""
2021-01-31 05:33:41 +00:00
book_list = get_object_or_404(models.List, id=list_id)
book_list.raise_visible_to_user(request.user)
2021-01-31 05:33:41 +00:00
if is_api_request(request):
2021-02-01 20:03:11 +00:00
return ActivitypubResponse(book_list.to_activity(**request.GET))
2021-01-31 17:08:06 +00:00
2021-03-08 16:49:10 +00:00
query = request.GET.get("q")
2021-01-31 21:38:26 +00:00
suggestions = None
2021-04-08 16:05:21 +00:00
# sort_by shall be "order" unless a valid alternative is given
sort_by = request.GET.get("sort_by", "order")
if sort_by not in ("order", "title", "rating"):
sort_by = "order"
# direction shall be "ascending" unless a valid alternative is given
direction = request.GET.get("direction", "ascending")
if direction not in ("ascending", "descending"):
direction = "ascending"
2021-05-11 01:28:31 +00:00
directional_sort_by = {
2021-04-18 01:31:38 +00:00
"order": "order",
"title": "book__title",
"rating": "average_rating",
2021-05-11 01:28:31 +00:00
}[sort_by]
2021-04-18 01:31:38 +00:00
if direction == "descending":
directional_sort_by = "-" + directional_sort_by
items = book_list.listitem_set.prefetch_related("user", "book", "book__authors")
2021-05-11 01:28:31 +00:00
if sort_by == "rating":
items = items.annotate(
average_rating=Avg(
Coalesce("book__review__rating", 0.0),
output_field=DecimalField(),
2021-04-18 02:09:00 +00:00
)
2021-04-18 01:31:38 +00:00
)
2021-05-11 01:28:31 +00:00
items = items.filter(approved=True).order_by(directional_sort_by)
2021-04-18 01:31:38 +00:00
paginated = Paginator(items, PAGE_LENGTH)
2021-04-08 16:05:21 +00:00
2021-01-31 21:38:26 +00:00
if query and request.user.is_authenticated:
2021-01-31 19:11:26 +00:00
# search for books
suggestions = book_search.search(
query,
filters=[~Q(parent_work__editions__in=book_list.books.all())],
)
2021-01-31 21:38:26 +00:00
elif request.user.is_authenticated:
2021-01-31 19:11:26 +00:00
# just suggest whatever books are nearby
suggestions = request.user.shelfbook_set.filter(
~Q(book__in=book_list.books.all())
)
suggestions = [s.book for s in suggestions[:5]]
if len(suggestions) < 5:
2021-01-31 19:21:50 +00:00
suggestions += [
2021-03-08 16:49:10 +00:00
s.default_edition
for s in models.Work.objects.filter(
~Q(editions__in=book_list.books.all()),
).order_by("-updated_date")
][: 5 - len(suggestions)]
2021-01-31 17:08:06 +00:00
page = paginated.get_page(request.GET.get("page"))
2021-12-04 15:17:21 +00:00
embed_key = str(book_list.embed_key.hex)
embed_url = reverse("embed-list", args=[book_list.id, embed_key])
embed_url = request.build_absolute_uri(embed_url)
if request.GET:
2021-12-04 16:33:28 +00:00
embed_url = f"{embed_url}?{request.GET.urlencode()}"
2021-12-04 15:17:21 +00:00
2021-01-31 05:33:41 +00:00
data = {
2021-03-08 16:49:10 +00:00
"list": book_list,
"items": page,
"page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
),
2021-03-08 16:49:10 +00:00
"pending_count": book_list.listitem_set.filter(approved=False).count(),
"suggested_books": suggestions,
"list_form": forms.ListForm(instance=book_list),
"query": query or "",
2021-04-08 16:05:21 +00:00
"sort_form": forms.SortListForm(
{"direction": direction, "sort_by": sort_by}
2021-10-04 10:31:28 +00:00
),
2021-12-04 15:17:21 +00:00
"embed_url": embed_url,
2021-01-31 05:33:41 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "lists/list.html", data)
2021-01-31 05:33:41 +00:00
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-31 05:33:41 +00:00
def post(self, request, list_id):
2021-04-26 16:15:42 +00:00
"""edit a list"""
2021-01-31 05:33:41 +00:00
book_list = get_object_or_404(models.List, id=list_id)
book_list.raise_not_editable(request.user)
2021-01-31 18:34:25 +00:00
form = forms.ListForm(request.POST, instance=book_list)
if not form.is_valid():
2021-03-08 16:49:10 +00:00
return redirect("list", book_list.id)
2021-01-31 18:34:25 +00:00
book_list = form.save()
if not book_list.curation == "group":
book_list.group = None
book_list.save(broadcast=False)
2021-01-31 05:33:41 +00:00
return redirect(book_list.local_path)
2021-01-31 18:34:25 +00:00
class EmbedList(View):
"""embeded book list page"""
def get(self, request, list_id, list_key):
"""display a book list"""
book_list = get_object_or_404(models.List, id=list_id)
embed_key = str(book_list.embed_key.hex)
if list_key != embed_key:
raise Http404()
# sort_by shall be "order" unless a valid alternative is given
sort_by = request.GET.get("sort_by", "order")
if sort_by not in ("order", "title", "rating"):
sort_by = "order"
# direction shall be "ascending" unless a valid alternative is given
direction = request.GET.get("direction", "ascending")
if direction not in ("ascending", "descending"):
direction = "ascending"
directional_sort_by = {
"order": "order",
"title": "book__title",
"rating": "average_rating",
}[sort_by]
if direction == "descending":
directional_sort_by = "-" + directional_sort_by
items = book_list.listitem_set.prefetch_related("user", "book", "book__authors")
if sort_by == "rating":
items = items.annotate(
average_rating=Avg(
Coalesce("book__review__rating", 0.0),
output_field=DecimalField(),
)
)
items = items.filter(approved=True).order_by(directional_sort_by)
paginated = Paginator(items, PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page"))
data = {
"list": book_list,
"items": page,
"page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
),
}
return TemplateResponse(request, "lists/embed-list.html", data)
@method_decorator(login_required, name="dispatch")
2021-01-31 20:07:54 +00:00
class Curate(View):
2021-04-26 16:15:42 +00:00
"""approve or discard list suggestsions"""
2021-03-08 16:49:10 +00:00
2021-01-31 20:07:54 +00:00
def get(self, request, list_id):
2021-04-26 16:15:42 +00:00
"""display a pending list"""
2021-01-31 20:07:54 +00:00
book_list = get_object_or_404(models.List, id=list_id)
book_list.raise_not_editable(request.user)
2021-01-31 20:07:54 +00:00
data = {
2021-03-08 16:49:10 +00:00
"list": book_list,
"pending": book_list.listitem_set.filter(approved=False),
"list_form": forms.ListForm(instance=book_list),
2021-01-31 20:07:54 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "lists/curate.html", data)
2021-01-31 20:07:54 +00:00
def post(self, request, list_id):
2021-04-26 16:15:42 +00:00
"""edit a book_list"""
2021-01-31 20:07:54 +00:00
book_list = get_object_or_404(models.List, id=list_id)
book_list.raise_not_editable(request.user)
2021-03-08 16:49:10 +00:00
suggestion = get_object_or_404(models.ListItem, id=request.POST.get("item"))
approved = request.POST.get("approved") == "true"
2021-01-31 20:07:54 +00:00
if approved:
2021-04-19 22:01:20 +00:00
# update the book and set it to be the last in the order of approved books,
# before any pending books
2021-01-31 20:07:54 +00:00
suggestion.approved = True
order_max = (
book_list.listitem_set.filter(approved=True).aggregate(Max("order"))[
"order__max"
]
or 0
) + 1
suggestion.order = order_max
increment_order_in_reverse(book_list.id, order_max)
2021-01-31 20:07:54 +00:00
suggestion.save()
else:
2021-04-08 16:05:21 +00:00
deleted_order = suggestion.order
2021-04-11 01:15:13 +00:00
suggestion.delete(broadcast=False)
2021-04-08 16:05:21 +00:00
normalize_book_list_ordering(book_list.id, start=deleted_order)
2021-03-08 16:49:10 +00:00
return redirect("list-curate", book_list.id)
2021-01-31 20:07:54 +00:00
2021-01-31 18:34:25 +00:00
@require_POST
2021-08-23 20:15:35 +00:00
@login_required
2021-08-23 22:33:49 +00:00
def save_list(request, list_id):
2021-08-23 22:07:38 +00:00
"""save a list"""
book_list = get_object_or_404(models.List, id=list_id)
2021-08-23 20:15:35 +00:00
request.user.saved_lists.add(book_list)
return redirect("list", list_id)
@require_POST
@login_required
2021-08-23 22:33:49 +00:00
def unsave_list(request, list_id):
2021-08-23 20:15:35 +00:00
"""unsave a list"""
2021-08-23 22:07:38 +00:00
book_list = get_object_or_404(models.List, id=list_id)
2021-08-23 20:15:35 +00:00
request.user.saved_lists.remove(book_list)
return redirect("list", list_id)
2021-09-06 17:38:37 +00:00
@require_POST
@login_required
def delete_list(request, list_id):
"""delete a list"""
book_list = get_object_or_404(models.List, id=list_id)
# only the owner or a moderator can delete a list
book_list.raise_not_deletable(request.user)
2021-09-06 17:38:37 +00:00
book_list.delete()
return redirect("lists")
2021-08-23 20:15:35 +00:00
@require_POST
@login_required
2021-03-15 21:44:03 +00:00
def add_book(request):
2021-04-26 16:15:42 +00:00
"""put a book on a list"""
2021-03-15 22:33:05 +00:00
book_list = get_object_or_404(models.List, id=request.POST.get("list"))
is_group_member = False
if book_list.curation == "group":
2021-10-04 10:31:28 +00:00
is_group_member = models.GroupMember.objects.filter(
group=book_list.group, user=request.user
).exists()
book_list.raise_visible_to_user(request.user)
2021-01-31 18:34:25 +00:00
2021-03-08 16:49:10 +00:00
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
2021-01-31 18:34:25 +00:00
# do you have permission to add to the list?
try:
2021-10-04 10:31:28 +00:00
if (
request.user == book_list.user
or is_group_member
or book_list.curation == "open"
):
2021-04-19 22:01:20 +00:00
# add the book at the latest order of approved books, before pending books
order_max = (
book_list.listitem_set.filter(approved=True).aggregate(Max("order"))[
"order__max"
]
) or 0
increment_order_in_reverse(book_list.id, order_max + 1)
models.ListItem.objects.create(
book=book,
book_list=book_list,
user=request.user,
2021-04-08 16:05:21 +00:00
order=order_max + 1,
)
2021-03-08 16:49:10 +00:00
elif book_list.curation == "curated":
# make a pending entry at the end of the list
order_max = (
book_list.listitem_set.aggregate(Max("order"))["order__max"]
) or 0
models.ListItem.objects.create(
approved=False,
book=book,
book_list=book_list,
user=request.user,
2021-04-08 16:05:21 +00:00
order=order_max + 1,
)
else:
# you can't add to this list, what were you THINKING
return HttpResponseBadRequest()
except IntegrityError:
# if the book is already on the list, don't flip out
pass
2021-01-31 18:34:25 +00:00
path = reverse("list", args=[book_list.id])
params = request.GET.copy()
params["updated"] = True
2021-09-18 18:32:00 +00:00
return redirect(f"{path}?{urlencode(params)}")
2021-01-31 20:15:38 +00:00
@require_POST
2021-08-23 20:15:35 +00:00
@login_required
2021-01-31 20:15:38 +00:00
def remove_book(request, list_id):
2021-04-26 16:15:42 +00:00
"""remove a book from a list"""
2021-01-31 20:15:38 +00:00
2021-09-28 00:52:27 +00:00
book_list = get_object_or_404(models.List, id=list_id)
item = get_object_or_404(models.ListItem, id=request.POST.get("item"))
2021-09-28 00:52:27 +00:00
item.raise_not_deletable(request.user)
2021-01-31 20:15:38 +00:00
2021-09-28 00:52:27 +00:00
with transaction.atomic():
2021-04-08 16:05:21 +00:00
deleted_order = item.order
item.delete()
2021-09-27 21:03:06 +00:00
normalize_book_list_ordering(book_list.id, start=deleted_order)
2021-03-08 16:49:10 +00:00
return redirect("list", list_id)
2021-04-08 16:05:21 +00:00
@require_POST
2021-08-23 20:15:35 +00:00
@login_required
2021-04-08 16:05:21 +00:00
def set_book_position(request, list_item_id):
"""
Action for when the list user manually specifies a list position, takes
special care with the unique ordering per list.
2021-04-08 16:05:21 +00:00
"""
2021-09-27 21:03:06 +00:00
list_item = get_object_or_404(models.ListItem, id=list_item_id)
2021-09-28 00:52:27 +00:00
list_item.book_list.raise_not_editable(request.user)
2021-09-27 21:03:06 +00:00
try:
int_position = int(request.POST.get("position"))
except ValueError:
2021-09-27 23:08:52 +00:00
return HttpResponseBadRequest("bad value for position. should be an integer")
2021-04-08 16:05:21 +00:00
2021-09-27 21:03:06 +00:00
if int_position < 1:
return HttpResponseBadRequest("position cannot be less than 1")
2021-04-08 16:05:21 +00:00
2021-09-27 21:03:06 +00:00
book_list = list_item.book_list
2021-09-27 21:03:06 +00:00
# the max position to which a book may be set is the highest order for
# books which are approved
2021-09-27 23:08:52 +00:00
order_max = book_list.listitem_set.filter(approved=True).aggregate(Max("order"))[
"order__max"
]
2021-04-08 16:05:21 +00:00
2021-09-27 21:03:06 +00:00
int_position = min(int_position, order_max)
2021-04-08 16:05:21 +00:00
2021-09-27 21:03:06 +00:00
original_order = list_item.order
if original_order == int_position:
# no change
return HttpResponse(status=204)
2021-04-08 16:05:21 +00:00
2021-09-27 21:03:06 +00:00
with transaction.atomic():
2021-04-19 22:01:20 +00:00
if original_order > int_position:
2021-04-08 16:05:21 +00:00
list_item.order = -1
list_item.save()
increment_order_in_reverse(book_list.id, int_position, original_order)
else:
list_item.order = -1
list_item.save()
decrement_order(book_list.id, original_order, int_position)
list_item.order = int_position
list_item.save()
return redirect("list", book_list.id)
@transaction.atomic
def increment_order_in_reverse(
book_list_id: int, start: int, end: Optional[int] = None
):
2021-09-18 18:32:00 +00:00
"""increase the order number for every item in a list"""
2021-04-08 16:05:21 +00:00
try:
book_list = models.List.objects.get(id=book_list_id)
except models.List.DoesNotExist:
return
items = book_list.listitem_set.filter(order__gte=start)
if end is not None:
items = items.filter(order__lt=end)
items = items.order_by("-order")
2021-04-08 16:05:21 +00:00
for item in items:
item.order += 1
item.save()
@transaction.atomic
def decrement_order(book_list_id, start, end):
2021-04-26 16:15:42 +00:00
"""decrement the order value for every item in a list"""
2021-04-08 16:05:21 +00:00
try:
book_list = models.List.objects.get(id=book_list_id)
except models.List.DoesNotExist:
return
items = book_list.listitem_set.filter(order__gt=start, order__lte=end).order_by(
"order"
)
for item in items:
item.order -= 1
item.save()
@transaction.atomic
def normalize_book_list_ordering(book_list_id, start=0, add_offset=0):
2021-04-26 16:15:42 +00:00
"""gives each book in a list the proper sequential order number"""
2021-04-08 16:05:21 +00:00
try:
book_list = models.List.objects.get(id=book_list_id)
except models.List.DoesNotExist:
2021-04-19 22:01:20 +00:00
return
2021-04-08 16:05:21 +00:00
items = book_list.listitem_set.filter(order__gt=start).order_by("order")
for i, item in enumerate(items, start):
effective_order = i + add_offset
if item.order != effective_order:
item.order = effective_order
item.save()
@xframe_options_exempt
def unsafe_embed_list(request, *args, **kwargs):
"""allows the EmbedList view to be loaded through unsafe iframe origins"""
embed_list_view = EmbedList.as_view()
return embed_list_view(request, *args, **kwargs)