Functional switch editions button

This commit is contained in:
Mouse Reeve 2020-12-11 16:57:38 -08:00
parent 72eb94315a
commit 2d7f8ada61
5 changed files with 43 additions and 13 deletions

View file

@ -283,7 +283,7 @@ class Boost(Status):
class ReadThrough(BookWyrmModel): class ReadThrough(BookWyrmModel):
''' Store progress through a book in the database. ''' ''' Store progress through a book in the database. '''
user = models.ForeignKey('User', on_delete=models.PROTECT) 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( pages_read = models.IntegerField(
null=True, null=True,
blank=True) blank=True)

View file

@ -103,7 +103,7 @@
{% for shelf in other_edition_shelves %} {% for shelf in other_edition_shelves %}
<p> <p>
A <a href="/book/{{ shelf.book.id }}">different edition</a> of this book is on your <a href="/user/{{ user.localname }}/shelves/{{ shelf.shelf.identifier }}">{{ shelf.shelf.name }}</a> shelf. A <a href="/book/{{ shelf.book.id }}">different edition</a> of this book is on your <a href="/user/{{ user.localname }}/shelves/{{ shelf.shelf.identifier }}">{{ shelf.shelf.name }}</a> shelf.
{% include 'snippets/switch_edition_button.html' with desired_edition=book %} {% include 'snippets/switch_edition_button.html' with edition=book %}
</p> </p>
{% endfor %} {% endfor %}

View file

@ -1,3 +1,5 @@
<div> <form name="switch-edition" action="/switch-edition" method="POST">
<button class="button">Switch to this edition</button> {% csrf_token %}
</div> <input type="hidden" name="edition" value="{{ edition.id }}">
<button class="button">Switch to this edition</button>
</form>

View file

@ -97,15 +97,16 @@ urlpatterns = [
re_path(r'^edit-profile/?$', actions.edit_profile), re_path(r'^edit-profile/?$', actions.edit_profile),
re_path(r'^import-data/?', actions.import_data), re_path(r'^import-data/?$', actions.import_data),
re_path(r'^retry-import/?', actions.retry_import), re_path(r'^retry-import/?$', actions.retry_import),
re_path(r'^resolve-book/?', actions.resolve_book), re_path(r'^resolve-book/?$', actions.resolve_book),
re_path(r'^edit-book/(?P<book_id>\d+)/?', actions.edit_book), re_path(r'^edit-book/(?P<book_id>\d+)/?$', actions.edit_book),
re_path(r'^upload-cover/(?P<book_id>\d+)/?', actions.upload_cover), re_path(r'^upload-cover/(?P<book_id>\d+)/?$', actions.upload_cover),
re_path(r'^add-description/(?P<book_id>\d+)/?', actions.add_description), re_path(r'^add-description/(?P<book_id>\d+)/?$', actions.add_description),
re_path(r'^edit-readthrough/?', actions.edit_readthrough), re_path(r'^switch-edition/?$', actions.switch_edition),
re_path(r'^delete-readthrough/?', actions.delete_readthrough), 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'^rate/?$', actions.rate),
re_path(r'^review/?$', actions.review), re_path(r'^review/?$', actions.review),

View file

@ -10,6 +10,7 @@ from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.db import transaction
from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
@ -244,6 +245,32 @@ def edit_book(request, book_id):
return redirect('/book/%s' % 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 @login_required
@require_POST @require_POST
def upload_cover(request, book_id): def upload_cover(request, book_id):