diff --git a/bookwyrm/templates/edit_book.html b/bookwyrm/templates/edit_book.html index 7c660e9c1..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 %} + {% 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: