@@ -72,8 +109,12 @@
{% trans "Remove this author" %}
{% endfor %}
+ {% if confirm_mode %}
+
+ {% else %}
-
+
+ {% endif %}
@@ -142,39 +183,4 @@
-{% if author_matches or book_matches %}
-
-
-{% endif %}
-
{% endblock %}
diff --git a/bookwyrm/tests/views/test_book.py b/bookwyrm/tests/views/test_book.py
index b7eaac4b..8a6c66cb 100644
--- a/bookwyrm/tests/views/test_book.py
+++ b/bookwyrm/tests/views/test_book.py
@@ -85,18 +85,7 @@ class BookViews(TestCase):
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')
+ # TODO
def test_switch_edition(self):
diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py
index dfb64c23..9e0d4630 100644
--- a/bookwyrm/urls.py
+++ b/bookwyrm/urls.py
@@ -129,6 +129,7 @@ urlpatterns = [
# books
re_path(r'%s(.json)?/?$' % book_path, views.Book.as_view()),
re_path(r'%s/edit/?$' % book_path, views.EditBook.as_view()),
+ re_path(r'%s/confirm/?$' % book_path, views.ConfirmEditBook.as_view()),
re_path(r'%s/editions(.json)?/?$' % book_path, views.Editions.as_view()),
re_path(r'^upload-cover/(?P
\d+)/?$', views.upload_cover),
re_path(r'^add-description/(?P\d+)/?$', views.add_description),
diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py
index 2c7cdc46..c3d17da7 100644
--- a/bookwyrm/views/__init__.py
+++ b/bookwyrm/views/__init__.py
@@ -2,7 +2,7 @@
from .authentication import Login, Register, Logout
from .author import Author, EditAuthor
from .block import Block, unblock
-from .books import Book, EditBook, Editions
+from .books import Book, EditBook, ConfirmEditBook, Editions
from .books import upload_cover, add_description, switch_edition, resolve_book
from .error import not_found_page, server_error_page
from .federation import Federation
diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py
index 174ddaa3..0097eb9f 100644
--- a/bookwyrm/views/books.py
+++ b/bookwyrm/views/books.py
@@ -122,8 +122,8 @@ class EditBook(View):
def post(self, request, book_id=None):
''' edit a book cool '''
- book = get_object_or_404(models.Edition, id=book_id) if book_id \
- else None
+ # returns None if no match is found
+ book = models.Edition.objects.filter(id=book_id).first()
form = forms.EditionForm(request.POST, request.FILES, instance=book)
data = {
@@ -134,9 +134,8 @@ class EditBook(View):
return TemplateResponse(request, 'edit_book.html', data)
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
- data['confirm_mode'] = True
+ # 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') +\
@@ -148,6 +147,8 @@ class EditBook(View):
rank=SearchRank(vector, add_author)
).filter(rank__gt=0.8).order_by('-rank')[:5]
+ # we're creating a new book
+ if not book:
# 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(
@@ -156,12 +157,54 @@ class EditBook(View):
raw=True
)[:5]
+ # either of the above cases requires additional confirmation
+ if add_author or not book:
+ # creting a book or adding an author to a book needs another step
+ data['confirm_mode'] = True
return TemplateResponse(request, 'edit_book.html', data)
book = form.save()
return redirect('/book/%s' % book.id)
+@method_decorator(login_required, name='dispatch')
+@method_decorator(
+ permission_required('bookwyrm.edit_book', raise_exception=True),
+ name='dispatch')
+class ConfirmEditBook(View):
+ ''' confirm edits to a book '''
+ def post(self, request, book_id=None):
+ ''' edit a book cool '''
+ # returns None if no match is found
+ book = models.Edition.objects.filter(id=book_id).first()
+ form = forms.EditionForm(request.POST, request.FILES, instance=book)
+
+ data = {
+ 'book': book,
+ 'form': form
+ }
+ if not form.is_valid():
+ return TemplateResponse(request, 'edit_book.html', data)
+
+ # create work, if needed
+ # TODO
+
+ # save book
+ book = form.save()
+
+ # 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)
+
+ return redirect('/book/%s' % book.id)
+
+
class Editions(View):
''' list of editions '''
def get(self, request, book_id):