Move editions into their own view file

This commit is contained in:
Mouse Reeve 2021-08-17 10:26:56 -07:00
parent 0f0dde6c6d
commit 4b788f5df1
3 changed files with 79 additions and 64 deletions

View file

@ -4,11 +4,12 @@ from .authentication import Login, Register, Logout
from .authentication import ConfirmEmail, ConfirmEmailCode, resend_link
from .author import Author, EditAuthor
from .block import Block, unblock
from .books import Book, EditBook, ConfirmEditBook, Editions
from .books import upload_cover, add_description, switch_edition, resolve_book
from .books import Book, EditBook, ConfirmEditBook
from .books import upload_cover, add_description, resolve_book
from .directory import Directory
from .discover import Discover
from .edit_user import EditUser, DeleteUser
from .editions import Editions, switch_edition
from .federation import Federation, FederatedServer
from .federation import AddFederatedServer, ImportServerBlocklist
from .federation import block_server, unblock_server

View file

@ -24,7 +24,7 @@ from bookwyrm.settings import PAGE_LENGTH
from .helpers import is_api_request, get_edition, privacy_filter
# pylint: disable= no-self-use
# pylint: disable=no-self-use
class Book(View):
"""a book! this is the stuff"""
@ -270,37 +270,6 @@ class ConfirmEditBook(View):
return redirect("/book/%s" % book.id)
class Editions(View):
"""list of editions"""
def get(self, request, book_id):
"""list of editions of a book"""
work = get_object_or_404(models.Work, id=book_id)
if is_api_request(request):
return ActivitypubResponse(work.to_edition_list(**request.GET))
filters = {}
if request.GET.get("language"):
filters["languages__contains"] = [request.GET.get("language")]
if request.GET.get("format"):
filters["physical_format__iexact"] = request.GET.get("format")
editions = work.editions.order_by("-edition_rank")
languages = set(sum([e.languages for e in editions], []))
paginated = Paginator(editions.filter(**filters), PAGE_LENGTH)
data = {
"editions": paginated.get_page(request.GET.get("page")),
"work": work,
"languages": languages,
"formats": set(
e.physical_format.lower() for e in editions if e.physical_format
),
}
return TemplateResponse(request, "book/editions.html", data)
@login_required
@require_POST
def upload_cover(request, book_id):
@ -363,33 +332,3 @@ def resolve_book(request):
book = connector.get_or_create_book(remote_id)
return redirect("book", 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, shelf__user=request.user
)
for shelfbook in shelfbooks.all():
with transaction.atomic():
models.ShelfBook.objects.create(
created_date=shelfbook.created_date,
user=shelfbook.user,
shelf=shelfbook.shelf,
book=new_edition,
)
shelfbook.delete()
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)

View file

@ -0,0 +1,75 @@
""" the good stuff! the books! """
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.views import View
from django.views.decorators.http import require_POST
from bookwyrm import models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
from .helpers import is_api_request
# pylint: disable=no-self-use
class Editions(View):
"""list of editions"""
def get(self, request, book_id):
"""list of editions of a book"""
work = get_object_or_404(models.Work, id=book_id)
if is_api_request(request):
return ActivitypubResponse(work.to_edition_list(**request.GET))
filters = {}
if request.GET.get("language"):
filters["languages__contains"] = [request.GET.get("language")]
if request.GET.get("format"):
filters["physical_format__iexact"] = request.GET.get("format")
editions = work.editions.order_by("-edition_rank")
languages = set(sum([e.languages for e in editions], []))
paginated = Paginator(editions.filter(**filters), PAGE_LENGTH)
data = {
"editions": paginated.get_page(request.GET.get("page")),
"work": work,
"languages": languages,
"formats": set(
e.physical_format.lower() for e in editions if e.physical_format
),
}
return TemplateResponse(request, "book/editions.html", data)
@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, shelf__user=request.user
)
for shelfbook in shelfbooks.all():
with transaction.atomic():
models.ShelfBook.objects.create(
created_date=shelfbook.created_date,
user=shelfbook.user,
shelf=shelfbook.shelf,
book=new_edition,
)
shelfbook.delete()
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)