2021-08-17 17:26:56 +00:00
|
|
|
""" the good stuff! the books! """
|
2021-08-17 17:44:24 +00:00
|
|
|
from functools import reduce
|
|
|
|
import operator
|
|
|
|
|
2021-08-17 17:26:56 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.db import transaction
|
2021-08-17 17:44:24 +00:00
|
|
|
from django.db.models import Q
|
2021-08-17 17:26:56 +00:00
|
|
|
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
|
2021-09-29 20:39:12 +00:00
|
|
|
from bookwyrm.views.helpers import is_api_request
|
2021-08-17 17:26:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
2021-08-17 18:47:10 +00:00
|
|
|
editions = work.editions.order_by("-edition_rank")
|
2021-08-17 19:04:14 +00:00
|
|
|
languages = set(sum(editions.values_list("languages", flat=True), []))
|
2021-08-17 18:47:10 +00:00
|
|
|
|
|
|
|
editions = editions.filter(**filters)
|
|
|
|
|
2021-08-17 17:44:24 +00:00
|
|
|
query = request.GET.get("q")
|
|
|
|
if query:
|
|
|
|
searchable_array_fields = ["languages", "publishers"]
|
|
|
|
searchable_fields = [
|
|
|
|
"title",
|
|
|
|
"physical_format",
|
|
|
|
"isbn_10",
|
|
|
|
"isbn_13",
|
|
|
|
"oclc_number",
|
|
|
|
"asin",
|
|
|
|
]
|
|
|
|
search_filter_entries = [
|
|
|
|
{f"{f}__icontains": query} for f in searchable_fields
|
|
|
|
] + [{f"{f}__iexact": query} for f in searchable_array_fields]
|
2021-08-17 18:47:10 +00:00
|
|
|
editions = editions.filter(
|
|
|
|
reduce(operator.or_, (Q(**f) for f in search_filter_entries))
|
2021-08-17 17:44:24 +00:00
|
|
|
)
|
|
|
|
|
2021-08-17 18:47:10 +00:00
|
|
|
paginated = Paginator(editions, PAGE_LENGTH)
|
2022-03-10 17:49:17 +00:00
|
|
|
page = paginated.get_page(request.GET.get("page"))
|
2021-08-17 17:26:56 +00:00
|
|
|
data = {
|
2022-03-10 17:49:17 +00:00
|
|
|
"editions": page,
|
|
|
|
"page_range": paginated.get_elided_page_range(
|
|
|
|
page.number, on_each_side=2, on_ends=1
|
|
|
|
),
|
2021-08-17 17:26:56 +00:00
|
|
|
"work": work,
|
|
|
|
"languages": languages,
|
|
|
|
"formats": set(
|
|
|
|
e.physical_format.lower() for e in editions if e.physical_format
|
|
|
|
),
|
|
|
|
}
|
2021-09-29 20:23:52 +00:00
|
|
|
return TemplateResponse(request, "book/editions/editions.html", data)
|
2021-08-17 17:26:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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()
|
|
|
|
|
2021-09-18 18:32:00 +00:00
|
|
|
return redirect(f"/book/{new_edition.id}")
|