diff --git a/bookwyrm/templates/edit_book.html b/bookwyrm/templates/edit_book.html
index aab9e1035..d0eb50bec 100644
--- a/bookwyrm/templates/edit_book.html
+++ b/bookwyrm/templates/edit_book.html
@@ -26,7 +26,8 @@
{% endif %}
-
+{% endif %}
+
{% endblock %}
diff --git a/bookwyrm/tests/views/test_book.py b/bookwyrm/tests/views/test_book.py
index b3360200d..b7eaac4bc 100644
--- a/bookwyrm/tests/views/test_book.py
+++ b/bookwyrm/tests/views/test_book.py
@@ -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')
diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py
index 1754982e8..174ddaa30 100644
--- a/bookwyrm/views/books.py
+++ b/bookwyrm/views/books.py
@@ -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)