diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 9d45379c..07e25119 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -283,7 +283,7 @@ class Boost(Status): class ReadThrough(BookWyrmModel): ''' Store progress through a book in the database. ''' user = models.ForeignKey('User', on_delete=models.PROTECT) - book = models.ForeignKey('Book', on_delete=models.PROTECT) + book = models.ForeignKey('Edition', on_delete=models.PROTECT) pages_read = models.IntegerField( null=True, blank=True) diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index eee22898..c7112016 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -103,7 +103,7 @@ {% for shelf in other_edition_shelves %}

A different edition of this book is on your {{ shelf.shelf.name }} shelf. - {% include 'snippets/switch_edition_button.html' with desired_edition=book %} + {% include 'snippets/switch_edition_button.html' with edition=book %}

{% endfor %} diff --git a/bookwyrm/templates/snippets/switch_edition_button.html b/bookwyrm/templates/snippets/switch_edition_button.html index 3fa09f4f..9771535a 100644 --- a/bookwyrm/templates/snippets/switch_edition_button.html +++ b/bookwyrm/templates/snippets/switch_edition_button.html @@ -1,3 +1,5 @@ -
- -
+
+ {% csrf_token %} + + +
diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index a9792038..01d6c56f 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -97,15 +97,16 @@ urlpatterns = [ re_path(r'^edit-profile/?$', actions.edit_profile), - re_path(r'^import-data/?', actions.import_data), - re_path(r'^retry-import/?', actions.retry_import), - re_path(r'^resolve-book/?', actions.resolve_book), - re_path(r'^edit-book/(?P\d+)/?', actions.edit_book), - re_path(r'^upload-cover/(?P\d+)/?', actions.upload_cover), - re_path(r'^add-description/(?P\d+)/?', actions.add_description), + re_path(r'^import-data/?$', actions.import_data), + re_path(r'^retry-import/?$', actions.retry_import), + re_path(r'^resolve-book/?$', actions.resolve_book), + re_path(r'^edit-book/(?P\d+)/?$', actions.edit_book), + re_path(r'^upload-cover/(?P\d+)/?$', actions.upload_cover), + re_path(r'^add-description/(?P\d+)/?$', actions.add_description), - re_path(r'^edit-readthrough/?', actions.edit_readthrough), - re_path(r'^delete-readthrough/?', actions.delete_readthrough), + re_path(r'^switch-edition/?$', actions.switch_edition), + re_path(r'^edit-readthrough/?$', actions.edit_readthrough), + re_path(r'^delete-readthrough/?$', actions.delete_readthrough), re_path(r'^rate/?$', actions.rate), re_path(r'^review/?$', actions.review), diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index bb862007..bd8baf73 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -10,6 +10,7 @@ from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required, permission_required from django.core.exceptions import PermissionDenied from django.core.files.base import ContentFile +from django.db import transaction from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse @@ -244,6 +245,32 @@ def edit_book(request, book_id): return redirect('/book/%s' % book.id) +@login_required +@require_POST +@transaction.atomic +def switch_edition(request): + ''' switch your copy of a book to a different edition ''' + edition_id = request.POST.get('edition') + new_edition = get_object_or_404(models.Edition, id=edition_id) + shelfbooks = models.ShelfBook.objects.filter( + book__parent_work=new_edition.parent_work, + added_by=request.user + ) + for shelfbook in shelfbooks.all(): + shelfbook.book = new_edition + shelfbook.save() + + readthroughs = models.ReadThrough.objects.filter( + book__parent_work=new_edition.parent_work, + user=request.user + ) + for readthrough in readthroughs.all(): + readthrough.book = new_edition + readthrough.save() + + return redirect('/book/%d' % new_edition.id) + + @login_required @require_POST def upload_cover(request, book_id):