2021-10-01 05:22:32 +00:00
|
|
|
""" the good stuff! the books! """
|
2021-11-01 04:48:52 +00:00
|
|
|
from re import sub
|
2021-10-01 05:22:32 +00:00
|
|
|
from dateutil.parser import parse as dateparse
|
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
|
|
|
from django.contrib.postgres.search import SearchRank, SearchVector
|
|
|
|
from django.db import transaction
|
|
|
|
from django.http import HttpResponseBadRequest
|
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.datastructures import MultiValueDictKeyError
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
|
2021-10-03 18:55:16 +00:00
|
|
|
from bookwyrm import book_search, forms, models
|
2021-11-22 01:52:59 +00:00
|
|
|
|
2021-11-21 08:55:55 +00:00
|
|
|
# from bookwyrm.activitypub.base_activity import ActivityObject
|
2021-11-01 08:50:49 +00:00
|
|
|
from bookwyrm.utils.isni import (
|
|
|
|
find_authors_by_name,
|
2021-11-21 08:55:55 +00:00
|
|
|
build_author_from_isni,
|
2021-11-01 08:50:49 +00:00
|
|
|
augment_author_metadata,
|
|
|
|
)
|
2021-10-01 05:22:32 +00:00
|
|
|
from bookwyrm.views.helpers import get_edition
|
|
|
|
from .books import set_cover_from_url
|
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
|
|
|
|
)
|
|
|
|
class EditBook(View):
|
|
|
|
"""edit a book"""
|
|
|
|
|
|
|
|
def get(self, request, book_id=None):
|
|
|
|
"""info about a book"""
|
|
|
|
book = None
|
|
|
|
if book_id:
|
|
|
|
book = get_edition(book_id)
|
|
|
|
if not book.description:
|
|
|
|
book.description = book.parent_work.description
|
|
|
|
data = {"book": book, "form": forms.EditionForm(instance=book)}
|
|
|
|
return TemplateResponse(request, "book/edit/edit_book.html", data)
|
|
|
|
|
2021-11-01 01:01:57 +00:00
|
|
|
# pylint: disable=too-many-locals
|
2021-10-01 05:22:32 +00:00
|
|
|
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, "book/edit/edit_book.html", data)
|
|
|
|
|
2021-11-30 22:21:28 +00:00
|
|
|
# filter out empty author fields
|
|
|
|
add_author = [author for author in request.POST.getlist("add_author") if author]
|
2021-10-01 05:22:32 +00:00
|
|
|
if add_author:
|
|
|
|
data["add_author"] = add_author
|
|
|
|
data["author_matches"] = []
|
2021-10-29 05:13:05 +00:00
|
|
|
data["isni_matches"] = []
|
2021-11-30 22:42:31 +00:00
|
|
|
|
2021-11-21 22:18:18 +00:00
|
|
|
for author in add_author:
|
2021-10-01 05:22:32 +00:00
|
|
|
if not author:
|
|
|
|
continue
|
|
|
|
# check for existing authors
|
|
|
|
vector = SearchVector("name", weight="A") + SearchVector(
|
|
|
|
"aliases", weight="B"
|
|
|
|
)
|
|
|
|
|
2021-10-31 09:48:47 +00:00
|
|
|
author_matches = (
|
|
|
|
models.Author.objects.annotate(search=vector)
|
|
|
|
.annotate(rank=SearchRank(vector, author))
|
|
|
|
.filter(rank__gt=0.4)
|
|
|
|
.order_by("-rank")[:5]
|
|
|
|
)
|
|
|
|
|
|
|
|
isni_authors = find_authors_by_name(
|
2021-11-22 01:52:59 +00:00
|
|
|
author, description=True
|
2021-10-31 09:48:47 +00:00
|
|
|
) # find matches from ISNI API
|
|
|
|
|
2021-11-21 21:49:22 +00:00
|
|
|
# dedupe isni authors we already have in the DB
|
2021-10-31 09:48:47 +00:00
|
|
|
exists = [
|
|
|
|
i
|
|
|
|
for i in isni_authors
|
|
|
|
for a in author_matches
|
2021-11-21 21:49:22 +00:00
|
|
|
if sub(r"\D", "", str(i.isni)) == sub(r"\D", "", str(a.isni))
|
2021-10-31 09:48:47 +00:00
|
|
|
]
|
2021-10-31 23:26:17 +00:00
|
|
|
|
2021-11-01 00:58:08 +00:00
|
|
|
# pylint: disable=cell-var-from-loop
|
2021-11-21 21:49:22 +00:00
|
|
|
matches = list(filter(lambda x: x not in exists, isni_authors))
|
|
|
|
# combine existing and isni authors
|
|
|
|
matches.extend(author_matches)
|
2021-10-31 23:26:17 +00:00
|
|
|
|
2021-10-01 05:22:32 +00:00
|
|
|
data["author_matches"].append(
|
|
|
|
{
|
|
|
|
"name": author.strip(),
|
2021-11-21 21:49:22 +00:00
|
|
|
"matches": matches,
|
2021-10-31 23:26:17 +00:00
|
|
|
"existing_isnis": exists,
|
2021-10-01 05:22:32 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2021-10-03 18:55:16 +00:00
|
|
|
data["book_matches"] = book_search.search(
|
2021-10-01 05:22:32 +00:00
|
|
|
f'{form.cleaned_data.get("title")} {author_text}',
|
|
|
|
min_confidence=0.5,
|
|
|
|
)[: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
|
|
|
|
# this isn't preserved because it isn't part of the form obj
|
|
|
|
data["remove_authors"] = request.POST.getlist("remove_authors")
|
|
|
|
data["cover_url"] = request.POST.get("cover-url")
|
|
|
|
|
|
|
|
# make sure the dates are passed in as datetime, they're currently a string
|
|
|
|
# QueryDicts are immutable, we need to copy
|
|
|
|
formcopy = data["form"].data.copy()
|
|
|
|
try:
|
|
|
|
formcopy["first_published_date"] = dateparse(
|
|
|
|
formcopy["first_published_date"]
|
|
|
|
)
|
|
|
|
except (MultiValueDictKeyError, ValueError):
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
formcopy["published_date"] = dateparse(formcopy["published_date"])
|
|
|
|
except (MultiValueDictKeyError, ValueError):
|
|
|
|
pass
|
|
|
|
data["form"].data = formcopy
|
|
|
|
return TemplateResponse(request, "book/edit/edit_book.html", data)
|
|
|
|
|
|
|
|
remove_authors = request.POST.getlist("remove_authors")
|
|
|
|
for author_id in remove_authors:
|
|
|
|
book.authors.remove(author_id)
|
|
|
|
|
|
|
|
book = form.save(commit=False)
|
|
|
|
url = request.POST.get("cover-url")
|
|
|
|
if url:
|
|
|
|
image = set_cover_from_url(url)
|
|
|
|
if image:
|
|
|
|
book.cover.save(*image, save=False)
|
|
|
|
book.save()
|
|
|
|
return redirect(f"/book/{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"""
|
|
|
|
|
2021-11-01 09:04:25 +00:00
|
|
|
# pylint: disable=too-many-locals
|
2021-11-22 02:01:58 +00:00
|
|
|
# pylint: disable=too-many-branches
|
2021-10-01 05:22:32 +00:00
|
|
|
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, "book/edit/edit_book.html", data)
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
# save book
|
|
|
|
book = form.save()
|
|
|
|
|
|
|
|
# get or create author as needed
|
|
|
|
for i in range(int(request.POST.get("author-match-count", 0))):
|
|
|
|
match = request.POST.get(f"author_match-{i}")
|
|
|
|
if not match:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
try:
|
|
|
|
# if it's an int, it's an ID
|
|
|
|
match = int(match)
|
|
|
|
author = get_object_or_404(
|
|
|
|
models.Author, id=request.POST[f"author_match-{i}"]
|
|
|
|
)
|
2021-11-01 08:50:49 +00:00
|
|
|
# update author metadata if the ISNI record is more complete
|
|
|
|
isni = request.POST.get(f"isni-for-{match}", None)
|
|
|
|
if isni is not None:
|
|
|
|
augment_author_metadata(author, isni)
|
2021-10-01 05:22:32 +00:00
|
|
|
except ValueError:
|
2021-11-21 08:55:55 +00:00
|
|
|
# otherwise it's a new author
|
|
|
|
isni_match = request.POST.get(f"author_match-{i}")
|
|
|
|
author_object = build_author_from_isni(isni_match)
|
2021-11-21 21:49:22 +00:00
|
|
|
# with author data class from isni id
|
2021-11-21 08:55:55 +00:00
|
|
|
if "author" in author_object:
|
2021-11-22 01:52:59 +00:00
|
|
|
skeleton = models.Author.objects.create(
|
|
|
|
name=author_object["author"].name
|
|
|
|
)
|
2021-11-21 08:55:55 +00:00
|
|
|
author = author_object["author"].to_model(
|
2021-11-22 01:52:59 +00:00
|
|
|
model=models.Author, overwrite=True, instance=skeleton
|
2021-11-21 08:55:55 +00:00
|
|
|
)
|
|
|
|
else:
|
2021-11-21 21:49:22 +00:00
|
|
|
# or it's just a name
|
2021-11-21 08:55:55 +00:00
|
|
|
author = models.Author.objects.create(name=match)
|
2021-10-01 05:22:32 +00:00
|
|
|
book.authors.add(author)
|
|
|
|
|
|
|
|
# create work, if needed
|
|
|
|
if not book_id:
|
|
|
|
work_match = request.POST.get("parent_work")
|
|
|
|
if work_match and work_match != "0":
|
|
|
|
work = get_object_or_404(models.Work, id=work_match)
|
|
|
|
else:
|
|
|
|
work = models.Work.objects.create(title=form.cleaned_data["title"])
|
|
|
|
work.authors.set(book.authors.all())
|
|
|
|
book.parent_work = work
|
|
|
|
|
|
|
|
for author_id in request.POST.getlist("remove_authors"):
|
|
|
|
book.authors.remove(author_id)
|
|
|
|
|
|
|
|
# import cover, if requested
|
|
|
|
url = request.POST.get("cover-url")
|
|
|
|
if url:
|
|
|
|
image = set_cover_from_url(url)
|
|
|
|
if image:
|
|
|
|
book.cover.save(*image, save=False)
|
|
|
|
|
|
|
|
# we don't tell the world when creating a book
|
|
|
|
book.save(broadcast=False)
|
|
|
|
|
|
|
|
return redirect(f"/book/{book.id}")
|