From 23cca85ee39043d4215725b7f592ae913a8e1026 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 13 Mar 2021 17:31:06 -0800 Subject: [PATCH 1/2] Fixes busted author path in confirmation step --- bookwyrm/templates/edit_book.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/edit_book.html b/bookwyrm/templates/edit_book.html index 7c660e9c1..a19e78f4a 100644 --- a/bookwyrm/templates/edit_book.html +++ b/bookwyrm/templates/edit_book.html @@ -48,7 +48,7 @@ {% for match in author.matches %}

- {% blocktrans with book_title=match.book_set.first.title %}Author of {{ book_title }}{% endblocktrans %} + {% blocktrans with book_title=match.book_set.first.title %}Author of {{ book_title }}{% endblocktrans %}

{% endfor %} From 8e71186067e722d7e1ba5fc3db76e2d4f26c8f44 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 13 Mar 2021 18:09:09 -0800 Subject: [PATCH 2/2] Fixes adding mutiple authors --- bookwyrm/templates/edit_book.html | 5 +++-- bookwyrm/tests/views/test_book.py | 6 ++++-- bookwyrm/views/books.py | 31 +++++++++++++++++-------------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/bookwyrm/templates/edit_book.html b/bookwyrm/templates/edit_book.html index a19e78f4a..401dc2941 100644 --- a/bookwyrm/templates/edit_book.html +++ b/bookwyrm/templates/edit_book.html @@ -40,18 +40,19 @@

{% trans "Confirm Book Info" %}

{% if author_matches %} +
{% for author in author_matches %}
{% blocktrans with name=author.name %}Is "{{ name }}" an existing author?{% endblocktrans %} - {% with forloop.counter as counter %} + {% with forloop.counter0 as counter %} {% for match in author.matches %}

{% blocktrans with book_title=match.book_set.first.title %}Author of {{ book_title }}{% endblocktrans %}

{% endfor %} - + {% endwith %}
{% endfor %} diff --git a/bookwyrm/tests/views/test_book.py b/bookwyrm/tests/views/test_book.py index 1549bdc64..850be8db9 100644 --- a/bookwyrm/tests/views/test_book.py +++ b/bookwyrm/tests/views/test_book.py @@ -109,7 +109,8 @@ class BookViews(TestCase): form = forms.EditionForm(instance=self.book) form.data["title"] = "New Title" form.data["last_edited_by"] = self.local_user.id - form.data["add_author"] = "Sappho" + form.data["author-match-count"] = 1 + form.data["author_match-0"] = "Sappho" request = self.factory.post("", form.data) request.user = self.local_user @@ -175,7 +176,8 @@ class BookViews(TestCase): self.local_user.groups.add(self.group) form = forms.EditionForm() form.data["title"] = "New Title" - form.data["add_author"] = "Sappho" + form.data["author-match-count"] = "1" + form.data["author_match-0"] = "Sappho" form.data["last_edited_by"] = self.local_user.id request = self.factory.post("", form.data) request.user = self.local_user diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 0a11b87cf..0b23b0d63 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -4,7 +4,7 @@ from django.contrib.postgres.search import SearchRank, SearchVector from django.core.paginator import Paginator from django.db import transaction from django.db.models import Avg, Q -from django.http import HttpResponseNotFound +from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator @@ -145,12 +145,13 @@ class EditBook(View): "name": author.strip(), "matches": ( models.Author.objects.annotate(search=vector) - .annotate(rank=SearchRank(vector, add_author)) + .annotate(rank=SearchRank(vector, author)) .filter(rank__gt=0.4) .order_by("-rank")[:5] ), } ) + print(data["author_matches"]) # we're creating a new book if not book: @@ -200,18 +201,20 @@ class ConfirmEditBook(View): book = form.save() # get or create author as needed - if request.POST.get("add_author"): - for (i, author) in enumerate(request.POST.get("add_author").split(",")): - if not author: - continue - match = request.POST.get("author_match-%d" % i) - if match and match != "0": - author = get_object_or_404( - models.Author, id=request.POST["author_match-%d" % i] - ) - else: - author = models.Author.objects.create(name=author.strip()) - book.authors.add(author) + for i in range(int(request.POST.get("author-match-count", 0))): + match = request.POST.get("author_match-%d" % i) + if not match: + return HttpResponseBadRequest() + try: + # if it's an int, it's an ID + match = int(match) + author = get_object_or_404( + models.Author, id=request.POST["author_match-%d" % i] + ) + except ValueError: + # otherwise it's a name + author = models.Author.objects.create(name=match) + book.authors.add(author) # create work, if needed if not book_id: