UI for adding and removing authors

This commit is contained in:
Mouse Reeve 2021-03-04 13:48:50 -08:00
parent 327f14a150
commit b2d1384bc5
3 changed files with 81 additions and 11 deletions

View file

@ -26,7 +26,8 @@
</div>
{% endif %}
<form class="block" name="edit-book" action="{{ book.local_path }}/edit" method="post" enctype="multipart/form-data">
<form class="block" name="edit-book" action="{{ book.local_path }}/edit#deduplication" method="post" enctype="multipart/form-data">
<fieldset {% if confirm_mode %}disabled{% endif %}>
{% csrf_token %}
<input type="hidden" name="last_edited_by" value="{{ request.user.id }}">
<div class="columns">
@ -68,11 +69,11 @@
{% for author in book.authors.all %}
<p><a href="{{ author.local_path }}">{{ author.name }}</a>
<label class="label">
<input type="checkbox" name="remove-author" value="{{ author.id }}"> {% trans "Remove this author" %}
<input type="checkbox" name="remove_author" value="{{ author.id }}"> {% trans "Remove this author" %}
</label>
{% endfor %}
<label class="label" for="id_add_author">{% trans "Add Author:" %}</label>
<input class="input" type="text" name="author" id="id_add_author" placeholder="John Doe">
<input class="input" type="text" name="add_author" id="id_add_author" placeholder="John Doe" value="{{ add_author }}">
</section>
</div>
@ -138,6 +139,42 @@
<button class="button is-primary" type="submit">{% trans "Save" %}</button>
<a class="button" href="/book/{{ book.id }}">{% trans "Cancel" %}</a>
</div>
</fieldset>
</form>
{% if author_matches or book_matches %}
<hr class="block">
<form class="box content" id="deduplication">
<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 %}Is "{{ add_author }}" an existing author?{% endblocktrans %}</legend>
{% for match in author_matches %}
<label class="label"><input type="radio" name="author_match" value="{{ match.id }}"> {{ 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 class="label"><input type="radio" name="author_match"> this is a new author</label>
</fieldset>
{% else %}
<p class="column is-half">{% blocktrans %}Creating a new author: {{ add_author }}{% 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="book_match" value="{{ match.id }}"> {{ match.title }}</label>
{% endfor %}
<label class="label"><input type="radio" name="book_match"> this is a new work</label>
</fieldset>
{% endif %}
</div>
<button class="button is-primary" type="submit">{% trans "save" %}</button>
</form>
{% endif %}
{% endblock %}

View file

@ -83,6 +83,22 @@ class BookViews(TestCase):
self.assertEqual(self.book.title, 'New Title')
def test_edit_book_add_author(self):
''' lets a user edit a book '''
view = views.EditBook.as_view()
self.local_user.groups.add(self.group)
form = forms.EditionForm(instance=self.book)
form.data['title'] = 'New Title'
form.data['last_edited_by'] = self.local_user.id
form.data['add_author'] = "John Doe"
request = self.factory.post('', form.data)
request.user = self.local_user
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
view(request, self.book.id)
self.book.refresh_from_db()
self.assertEqual(self.book.title, 'New Title')
def test_switch_edition(self):
''' updates user's relationships to a book '''
work = models.Work.objects.create(title='test work')

View file

@ -1,6 +1,7 @@
''' the good stuff! the books! '''
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required, permission_required
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
@ -132,16 +133,32 @@ class EditBook(View):
if not form.is_valid():
return TemplateResponse(request, 'edit_book.html', data)
if not book or form.author:
add_author = request.POST.get('add_author')
if not book or add_author:
# creting a book or adding an author to a book needs another step
return TemplateResponse(request, 'confirm_book.html', data)
data['confirm_mode'] = True
data['add_author'] = add_author
# 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.8).order_by('-rank')[:5]
# check if this is an edition of an existing work
author_text = book.author_text if book else add_author
data['book_matches'] = connector_manager.local_search(
'%s %s' % (form.cleaned_data.get('title'), author_text),
min_confidence=0.5,
raw=True
)[:5]
return TemplateResponse(request, 'edit_book.html', data)
# remove authors
if request.POST.get('remove-author'):
import pdb;pdb.set_trace()
author = get_object_or_404(id=author_id)
book = form.save()
return redirect('/book/%s' % book.id)