Allow users to remove books from suggestion lists

This commit is contained in:
Mouse Reeve 2024-08-26 18:16:31 -07:00
parent 2e15c227f3
commit c901d76705
5 changed files with 25 additions and 5 deletions

View file

@ -113,11 +113,11 @@
</form>
{% endif %}
{% if list.user == request.user or list.curation == 'open' and item.user == request.user or list.group|is_member:request.user %}
{% if item.user == request.user or list.curation == 'open' and item.user == request.user or list.group|is_member:request.user %}
<form
name="remove-book-{{ item.id }}"
method="post"
action="{% url 'list-remove-book' list.id %}"
action="{{ remove_book_url }}"
class="card-footer-item"
>
{% csrf_token %}

View file

@ -770,6 +770,11 @@ urlpatterns = [
views.book_add_suggestion,
name="book-add-suggestion",
),
re_path(
rf"{BOOK_PATH}/suggestions/remove/?$",
views.book_remove_suggestion,
name="book-remove-suggestion",
),
re_path(
r"^author/(?P<author_id>\d+)/update/(?P<connector_identifier>[\w\.]+)/?$",
views.update_author_from_remote,

View file

@ -107,7 +107,7 @@ from .list.list import (
# suggestion lists
from .suggestion_list import SuggestionList
from .suggestion_list import book_add_suggestion
from .suggestion_list import book_add_suggestion, book_remove_suggestion
# misc views
from .author import Author, EditAuthor, update_author_from_remote

View file

@ -74,6 +74,7 @@ class List(View):
"add_failed": add_failed,
"add_succeeded": add_succeeded,
"add_book_url": reverse("list-add-book"),
"remove_book_url": reverse("list-remove-book", args=[list_id]),
}
if request.user.is_authenticated:

View file

@ -13,7 +13,7 @@ from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views import Book
from bookwyrm.views.helpers import is_api_request
from bookwyrm.views.helpers import is_api_request, redirect_to_referer
from bookwyrm.views.list.list import get_list_suggestions
# pylint: disable=no-self-use
@ -57,6 +57,7 @@ class SuggestionList(View):
"add_failed": add_failed,
"add_succeeded": add_succeeded,
"add_book_url": reverse("book-add-suggestion", args=[book_id]),
"remove_book_url": reverse("book-remove-suggestion", args=[book_id]),
}
if request.user.is_authenticated:
@ -97,4 +98,17 @@ def book_add_suggestion(request, book_id):
item = form.save(request, commit=False)
item.save()
return Book().get(request, book_id, add_succeeded=True)
return redirect_to_referer(request)
@require_POST
@login_required
def book_remove_suggestion(request, _):
"""remove a book from a suggestion list"""
item = get_object_or_404(models.SuggestionListItem, id=request.POST.get("item"))
item.raise_not_deletable(request.user)
with transaction.atomic():
item.delete()
return redirect_to_referer(request)