Merge pull request #735 from mouse-reeve/multi-author-fixes

Multi author fixes
This commit is contained in:
Mouse Reeve 2021-03-13 18:25:00 -08:00 committed by GitHub
commit 7213e79fa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 19 deletions

View file

@ -40,18 +40,19 @@
<h2 class="title is-4">{% trans "Confirm Book Info" %}</h2>
<div class="columns">
{% if author_matches %}
<input type="hidden" name="author-match-count" value="{{ author_matches|length }}">
<div class="column is-half">
{% for author in author_matches %}
<fieldset class="mb-4">
<legend class="title is-5 mb-1">{% blocktrans with name=author.name %}Is "{{ name }}" an existing author?{% endblocktrans %}</legend>
{% with forloop.counter as counter %}
{% with forloop.counter0 as counter %}
{% for match in author.matches %}
<label><input type="radio" name="author_match-{{ counter }}" value="{{ match.id }}" required> {{ match.name }}</label>
<p class="help">
<a href="{{ author.local_path }}" target="_blank">{% blocktrans with book_title=match.book_set.first.title %}Author of <em>{{ book_title }}</em>{% endblocktrans %}</a>
<a href="{{ match.local_path }}" target="_blank">{% blocktrans with book_title=match.book_set.first.title %}Author of <em>{{ book_title }}</em>{% endblocktrans %}</a>
</p>
{% endfor %}
<label><input type="radio" name="author_match-{{ counter }}" value="0" required> {% trans "This is a new author" %}</label>
<label><input type="radio" name="author_match-{{ counter }}" value="{{ author.name }}" required> {% trans "This is a new author" %}</label>
{% endwith %}
</fieldset>
{% endfor %}

View file

@ -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

View file

@ -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: