2021-01-31 05:33:41 +00:00
|
|
|
''' book list views'''
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-02-01 01:34:06 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-01-31 05:33:41 +00:00
|
|
|
from django.db.models import Q
|
2021-01-31 18:34:25 +00:00
|
|
|
from django.http import HttpResponseNotFound, HttpResponseBadRequest
|
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.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
|
2021-01-31 05:33:41 +00:00
|
|
|
|
2021-01-31 16:08:52 +00:00
|
|
|
from bookwyrm import forms, models
|
2021-01-31 05:33:41 +00:00
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
2021-02-02 17:37:46 +00:00
|
|
|
from bookwyrm.broadcast import broadcast
|
2021-01-31 19:11:26 +00:00
|
|
|
from bookwyrm.connectors import connector_manager
|
2021-01-31 16:41:11 +00:00
|
|
|
from .helpers import is_api_request, object_visible_to_user, privacy_filter
|
2021-02-01 19:34:08 +00:00
|
|
|
from .helpers import get_user_from_username
|
2021-01-31 05:33:41 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
class Lists(View):
|
|
|
|
''' book list page '''
|
|
|
|
def get(self, request):
|
|
|
|
''' display a book list '''
|
2021-02-01 01:34:06 +00:00
|
|
|
try:
|
|
|
|
page = int(request.GET.get('page', 1))
|
|
|
|
except ValueError:
|
|
|
|
page = 1
|
|
|
|
|
2021-01-31 05:33:41 +00:00
|
|
|
user = request.user if request.user.is_authenticated else None
|
2021-01-31 16:41:11 +00:00
|
|
|
lists = models.List.objects.filter(
|
|
|
|
~Q(user=user),
|
2021-02-04 00:01:49 +00:00
|
|
|
books__isnull=False,
|
2021-01-31 16:41:11 +00:00
|
|
|
).all()
|
|
|
|
lists = privacy_filter(request.user, lists, ['public', 'followers'])
|
2021-02-01 01:34:06 +00:00
|
|
|
|
|
|
|
paginated = Paginator(lists, 12)
|
2021-01-31 16:08:52 +00:00
|
|
|
data = {
|
|
|
|
'title': 'Lists',
|
2021-02-01 01:34:06 +00:00
|
|
|
'lists': paginated.page(page),
|
|
|
|
'list_form': forms.ListForm(),
|
|
|
|
'path': '/list',
|
2021-01-31 16:08:52 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'lists/lists.html', data)
|
2021-01-31 05:33:41 +00:00
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def post(self, request):
|
|
|
|
''' create a book_list '''
|
2021-01-31 16:41:11 +00:00
|
|
|
form = forms.ListForm(request.POST)
|
|
|
|
if not form.is_valid():
|
|
|
|
return redirect('lists')
|
|
|
|
book_list = form.save()
|
2021-02-02 17:37:46 +00:00
|
|
|
|
|
|
|
# let the world know
|
|
|
|
broadcast(
|
|
|
|
request.user,
|
|
|
|
book_list.to_create_activity(request.user),
|
|
|
|
privacy=book_list.privacy,
|
|
|
|
software='bookwyrm'
|
|
|
|
)
|
2021-01-31 05:33:41 +00:00
|
|
|
return redirect(book_list.local_path)
|
|
|
|
|
2021-02-01 19:34:08 +00:00
|
|
|
class UserLists(View):
|
|
|
|
''' a user's book list page '''
|
|
|
|
def get(self, request, username):
|
|
|
|
''' display a book list '''
|
|
|
|
try:
|
|
|
|
page = int(request.GET.get('page', 1))
|
|
|
|
except ValueError:
|
|
|
|
page = 1
|
|
|
|
user = get_user_from_username(username)
|
|
|
|
lists = models.List.objects.filter(user=user).all()
|
|
|
|
lists = privacy_filter(
|
|
|
|
request.user, lists, ['public', 'followers', 'unlisted'])
|
|
|
|
paginated = Paginator(lists, 12)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'title': '%s: Lists' % user.name,
|
|
|
|
'user': user,
|
|
|
|
'is_self': request.user.id == user.id,
|
|
|
|
'lists': paginated.page(page),
|
|
|
|
'list_form': forms.ListForm(),
|
|
|
|
'path': user.local_path + '/lists',
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'user/lists.html', data)
|
|
|
|
|
2021-01-31 05:33:41 +00:00
|
|
|
|
|
|
|
class List(View):
|
|
|
|
''' book list page '''
|
|
|
|
def get(self, request, list_id):
|
|
|
|
''' display a book list '''
|
|
|
|
book_list = get_object_or_404(models.List, id=list_id)
|
|
|
|
if not object_visible_to_user(request.user, book_list):
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
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-01-31 19:11:26 +00:00
|
|
|
query = request.GET.get('q')
|
2021-01-31 21:38:26 +00:00
|
|
|
suggestions = None
|
|
|
|
if query and request.user.is_authenticated:
|
2021-01-31 19:11:26 +00:00
|
|
|
# search for books
|
|
|
|
suggestions = connector_manager.local_search(query, raw=True)
|
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 += [
|
|
|
|
s.default_edition for s in \
|
|
|
|
models.Work.objects.filter(
|
|
|
|
~Q(editions__in=book_list.books.all()),
|
|
|
|
).order_by('-updated_date')
|
2021-01-31 19:11:26 +00:00
|
|
|
][:5 - len(suggestions)]
|
2021-01-31 18:56:40 +00:00
|
|
|
|
2021-01-31 17:08:06 +00:00
|
|
|
|
2021-01-31 05:33:41 +00:00
|
|
|
data = {
|
2021-01-31 17:08:06 +00:00
|
|
|
'title': '%s | Lists' % book_list.name,
|
|
|
|
'list': book_list,
|
2021-01-31 19:21:50 +00:00
|
|
|
'items': book_list.listitem_set.filter(approved=True),
|
2021-01-31 20:07:54 +00:00
|
|
|
'pending_count': book_list.listitem_set.filter(
|
|
|
|
approved=False).count(),
|
2021-01-31 18:56:40 +00:00
|
|
|
'suggested_books': suggestions,
|
2021-01-31 18:34:25 +00:00
|
|
|
'list_form': forms.ListForm(instance=book_list),
|
2021-01-31 19:11:26 +00:00
|
|
|
'query': query or ''
|
2021-01-31 05:33:41 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'lists/list.html', data)
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def post(self, request, list_id):
|
2021-01-31 17:08:06 +00:00
|
|
|
''' edit a book_list '''
|
2021-01-31 05:33:41 +00:00
|
|
|
book_list = get_object_or_404(models.List, id=list_id)
|
2021-01-31 18:34:25 +00:00
|
|
|
form = forms.ListForm(request.POST, instance=book_list)
|
|
|
|
if not form.is_valid():
|
|
|
|
return redirect('list', book_list.id)
|
|
|
|
book_list = form.save()
|
2021-02-02 17:37:46 +00:00
|
|
|
# let the world know
|
|
|
|
broadcast(
|
|
|
|
request.user,
|
|
|
|
book_list.to_update_activity(request.user),
|
|
|
|
privacy=book_list.privacy,
|
|
|
|
software='bookwyrm'
|
|
|
|
)
|
2021-01-31 05:33:41 +00:00
|
|
|
return redirect(book_list.local_path)
|
2021-01-31 18:34:25 +00:00
|
|
|
|
|
|
|
|
2021-01-31 20:07:54 +00:00
|
|
|
class Curate(View):
|
|
|
|
''' approve or discard list suggestsions '''
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
|
|
def get(self, request, list_id):
|
|
|
|
''' display a pending list '''
|
|
|
|
book_list = get_object_or_404(models.List, id=list_id)
|
|
|
|
if not book_list.user == request.user:
|
|
|
|
# only the creater can curate the list
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'title': 'Curate "%s" | Lists' % book_list.name,
|
|
|
|
'list': book_list,
|
|
|
|
'pending': book_list.listitem_set.filter(approved=False),
|
|
|
|
'list_form': forms.ListForm(instance=book_list),
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'lists/curate.html', data)
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def post(self, request, list_id):
|
|
|
|
''' edit a book_list '''
|
|
|
|
book_list = get_object_or_404(models.List, id=list_id)
|
|
|
|
suggestion = get_object_or_404(
|
|
|
|
models.ListItem, id=request.POST.get('item'))
|
|
|
|
approved = request.POST.get('approved') == 'true'
|
|
|
|
if approved:
|
|
|
|
suggestion.approved = True
|
|
|
|
suggestion.save()
|
2021-02-02 17:37:46 +00:00
|
|
|
# let the world know
|
|
|
|
broadcast(
|
|
|
|
request.user,
|
|
|
|
suggestion.to_add_activity(request.user),
|
|
|
|
privacy=book_list.privacy,
|
|
|
|
software='bookwyrm'
|
|
|
|
)
|
2021-01-31 20:07:54 +00:00
|
|
|
else:
|
|
|
|
suggestion.delete()
|
|
|
|
return redirect('list-curate', book_list.id)
|
|
|
|
|
|
|
|
|
2021-01-31 18:34:25 +00:00
|
|
|
@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
|
2021-02-02 17:37:46 +00:00
|
|
|
item = models.ListItem.objects.create(
|
2021-01-31 18:34:25 +00:00
|
|
|
book=book,
|
|
|
|
book_list=book_list,
|
|
|
|
added_by=request.user,
|
|
|
|
)
|
2021-02-02 17:37:46 +00:00
|
|
|
# let the world know
|
|
|
|
broadcast(
|
|
|
|
request.user,
|
|
|
|
item.to_add_activity(request.user),
|
|
|
|
privacy=book_list.privacy,
|
|
|
|
software='bookwyrm'
|
|
|
|
)
|
2021-01-31 18:34:25 +00:00
|
|
|
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)
|
2021-01-31 20:15:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@require_POST
|
|
|
|
def remove_book(request, list_id):
|
|
|
|
''' put a book on a list '''
|
|
|
|
book_list = get_object_or_404(models.List, id=list_id)
|
|
|
|
item = get_object_or_404(models.ListItem, id=request.POST.get('item'))
|
|
|
|
|
2021-01-31 21:14:52 +00:00
|
|
|
if not book_list.user == request.user and not item.added_by == request.user:
|
2021-01-31 20:15:38 +00:00
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2021-02-02 17:37:46 +00:00
|
|
|
activity = item.to_remove_activity(request.user)
|
2021-01-31 20:15:38 +00:00
|
|
|
item.delete()
|
2021-02-02 17:37:46 +00:00
|
|
|
# let the world know
|
|
|
|
broadcast(
|
|
|
|
request.user,
|
|
|
|
activity,
|
|
|
|
privacy=book_list.privacy,
|
|
|
|
software='bookwyrm'
|
|
|
|
)
|
2021-01-31 20:15:38 +00:00
|
|
|
return redirect('list', list_id)
|