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> <h2 class="title is-4">{% trans "Confirm Book Info" %}</h2>
<div class="columns"> <div class="columns">
{% if author_matches %} {% if author_matches %}
<input type="hidden" name="author-match-count" value="{{ author_matches|length }}">
<div class="column is-half"> <div class="column is-half">
{% for author in author_matches %} {% for author in author_matches %}
<fieldset class="mb-4"> <fieldset class="mb-4">
<legend class="title is-5 mb-1">{% blocktrans with name=author.name %}Is "{{ name }}" an existing author?{% endblocktrans %}</legend> <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 %} {% for match in author.matches %}
<label><input type="radio" name="author_match-{{ counter }}" value="{{ match.id }}" required> {{ match.name }}</label> <label><input type="radio" name="author_match-{{ counter }}" value="{{ match.id }}" required> {{ match.name }}</label>
<p class="help"> <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> </p>
{% endfor %} {% 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 %} {% endwith %}
</fieldset> </fieldset>
{% endfor %} {% endfor %}

View file

@ -109,7 +109,8 @@ class BookViews(TestCase):
form = forms.EditionForm(instance=self.book) form = forms.EditionForm(instance=self.book)
form.data["title"] = "New Title" form.data["title"] = "New Title"
form.data["last_edited_by"] = self.local_user.id 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 = self.factory.post("", form.data)
request.user = self.local_user request.user = self.local_user
@ -175,7 +176,8 @@ class BookViews(TestCase):
self.local_user.groups.add(self.group) self.local_user.groups.add(self.group)
form = forms.EditionForm() form = forms.EditionForm()
form.data["title"] = "New Title" 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 form.data["last_edited_by"] = self.local_user.id
request = self.factory.post("", form.data) request = self.factory.post("", form.data)
request.user = self.local_user 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.core.paginator import Paginator
from django.db import transaction from django.db import transaction
from django.db.models import Avg, Q 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.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -145,12 +145,13 @@ class EditBook(View):
"name": author.strip(), "name": author.strip(),
"matches": ( "matches": (
models.Author.objects.annotate(search=vector) models.Author.objects.annotate(search=vector)
.annotate(rank=SearchRank(vector, add_author)) .annotate(rank=SearchRank(vector, author))
.filter(rank__gt=0.4) .filter(rank__gt=0.4)
.order_by("-rank")[:5] .order_by("-rank")[:5]
), ),
} }
) )
print(data["author_matches"])
# we're creating a new book # we're creating a new book
if not book: if not book:
@ -200,18 +201,20 @@ class ConfirmEditBook(View):
book = form.save() book = form.save()
# get or create author as needed # get or create author as needed
if request.POST.get("add_author"): for i in range(int(request.POST.get("author-match-count", 0))):
for (i, author) in enumerate(request.POST.get("add_author").split(",")): match = request.POST.get("author_match-%d" % i)
if not author: if not match:
continue return HttpResponseBadRequest()
match = request.POST.get("author_match-%d" % i) try:
if match and match != "0": # if it's an int, it's an ID
author = get_object_or_404( match = int(match)
models.Author, id=request.POST["author_match-%d" % i] author = get_object_or_404(
) models.Author, id=request.POST["author_match-%d" % i]
else: )
author = models.Author.objects.create(name=author.strip()) except ValueError:
book.authors.add(author) # otherwise it's a name
author = models.Author.objects.create(name=match)
book.authors.add(author)
# create work, if needed # create work, if needed
if not book_id: if not book_id: