Add multiple authors

This commit is contained in:
Mouse Reeve 2021-03-11 16:33:49 -08:00
parent 965d84f86f
commit c1976dbd62
2 changed files with 61 additions and 41 deletions

View file

@ -39,29 +39,37 @@
<div class="box">
<h2 class="title is-4">{% trans "Confirm Book Info" %}</h2>
<div class="columns">
{% if author_matches.exists %}
<fieldset class="column is-half">
<legend class="label">{% blocktrans with name=add_author %}Is "{{ name }}" an existing author?{% endblocktrans %}</legend>
{% for match in author_matches %}
<label><input type="radio" name="author_match" 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>
</p>
{% if author_matches %}
<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 %}
{% 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>
</p>
{% endfor %}
<label><input type="radio" name="author_match-{{ counter }}" value="0" required> {% trans "This is a new author" %}</label>
{% endwith %}
</fieldset>
{% endfor %}
<label><input type="radio" name="author_match"> {% trans "This is a new author" %}</label>
</fieldset>
</div>
{% else %}
<p class="column is-half">{% blocktrans with name=add_author %}Creating a new author: {{ name }}{% endblocktrans %}</p>
{% endif %}
{% if not book %}
<fieldset class="column is-half">
<legend class="title is-5">{% trans "Is this an editions of an existing work?" %}</legend>
{% for match in book_matches %}
<label class="label"><input type="radio" name="parent_work" value="{{ match.parent_work.id }}"> {{ match.parent_work.title }}</label>
{% endfor %}
<label><input type="radio" name="parent_work" value="0"> this is a new work</label>
</fieldset>
<div class="column is-half">
<fieldset>
<legend class="title is-5 mb-1">{% trans "Is this an edition of an existing work?" %}</legend>
{% for match in book_matches %}
<label class="label"><input type="radio" name="parent_work" value="{{ match.parent_work.id }}"> {{ match.parent_work.title }}</label>
{% endfor %}
<label><input type="radio" name="parent_work" value="0" required> {% trans "This is a new work" %}</label>
</fieldset>
</div>
{% endif %}
</div>
@ -109,16 +117,19 @@
<section class="block">
<h2 class="title is-4">{% trans "Authors" %}</h2>
{% if book.authors.exists %}
<fieldset>
{% for author in book.authors.all %}
<p><a href="{{ author.local_path }}">{{ author.name }}</a>
<label class="label">
<input type="checkbox" name="remove_authors" value="{{ author.id }}"> {% trans "Remove this author" %}
<label class="label mb-2">
<input type="checkbox" name="remove_authors" value="{{ author.id }}" {% if author.id|stringformat:"i" in remove_authors %}checked{% endif %}>
{% blocktrans with name=author.name path=author.local_path %}Remove <a href="{{ path }}">{{ name }}</a>{% endblocktrans %}
</label>
{% endfor %}
</fieldset>
<label class="label" for="id_add_author">{% trans "Add Author:" %}</label>
<input class="input" type="text" name="add_author" id="id_add_author" placeholder="{% trans 'John Doe' %}" value="{{ add_author }}">
{% endif %}
<label class="label" for="id_add_author">{% trans "Add Authors:" %}</label>
<p class="help">Separate multiple author names with commas.</p>
<input class="input" type="text" name="add_author" id="id_add_author" placeholder="{% trans 'John Doe, Jane Smith' %}" value="{{ add_author }}">
</section>
</div>

View file

@ -131,17 +131,22 @@ class EditBook(View):
# we're adding an author through a free text field
if add_author:
data["add_author"] = add_author
# check for existing authors
vector = SearchVector("name", weight="A") + SearchVector(
"aliases", weight="B"
)
data['author_matches'] = []
for author in add_author.split(','):
# check for existing authors
vector = SearchVector("name", weight="A") + SearchVector(
"aliases", weight="B"
)
data["author_matches"] = (
models.Author.objects.annotate(search=vector)
.annotate(rank=SearchRank(vector, add_author))
.filter(rank__gt=0.4)
.order_by("-rank")[:5]
)
data["author_matches"].append({
'name': author.strip(),
'matches': (
models.Author.objects.annotate(search=vector)
.annotate(rank=SearchRank(vector, add_author))
.filter(rank__gt=0.4)
.order_by("-rank")[:5]
)
})
# we're creating a new book
if not book:
@ -157,6 +162,8 @@ class EditBook(View):
if add_author or not book:
# creting a book or adding an author to a book needs another step
data["confirm_mode"] = True
# this isn't preserved because it isn't part of the form obj
data["remove_authors"] = request.POST.getlist("remove_authors")
return TemplateResponse(request, "edit_book.html", data)
remove_authors = request.POST.getlist("remove_authors")
@ -190,15 +197,17 @@ class ConfirmEditBook(View):
# get or create author as needed
if request.POST.get("add_author"):
if request.POST.get("author_match"):
author = get_object_or_404(
models.Author, id=request.POST["author_match"]
)
else:
author = models.Author.objects.create(
name=request.POST.get("add_author")
)
book.authors.add(author)
for (i, author) in enumerate(request.POST.get("add_author").split(',')):
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)
# create work, if needed
if not book_id: