2021-03-08 16:49:10 +00:00
|
|
|
""" the good people stuff! the authors! """
|
2021-01-13 17:54:35 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2021-10-21 00:36:52 +00:00
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.db.models import OuterRef, Subquery, F, Q
|
2021-01-13 17:54:35 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
2021-10-21 00:36:52 +00:00
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
|
|
|
from bookwyrm.views.helpers import is_api_request
|
2021-01-13 17:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
class Author(View):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""this person wrote a book"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-13 17:54:35 +00:00
|
|
|
def get(self, request, author_id):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""landing page for an author"""
|
2021-01-13 17:54:35 +00:00
|
|
|
author = get_object_or_404(models.Author, id=author_id)
|
|
|
|
|
|
|
|
if is_api_request(request):
|
|
|
|
return ActivitypubResponse(author.to_activity())
|
|
|
|
|
2021-10-21 00:36:52 +00:00
|
|
|
default_editions = models.Edition.objects.filter(
|
|
|
|
parent_work=OuterRef("parent_work")
|
|
|
|
).order_by("-edition_rank")
|
|
|
|
|
|
|
|
books = (
|
2021-10-21 01:11:31 +00:00
|
|
|
models.Edition.viewer_aware_objects(request.user)
|
|
|
|
.filter(Q(authors=author) | Q(parent_work__authors=author))
|
2021-10-21 00:36:52 +00:00
|
|
|
.annotate(default_id=Subquery(default_editions.values("id")[:1]))
|
|
|
|
.filter(default_id=F("id"))
|
2021-10-21 01:27:19 +00:00
|
|
|
.order_by("-first_published_date", "-published_date", "-created_date")
|
|
|
|
.prefetch_related("authors")
|
2021-10-25 18:06:54 +00:00
|
|
|
).distinct()
|
2021-10-21 00:36:52 +00:00
|
|
|
|
|
|
|
paginated = Paginator(books, PAGE_LENGTH)
|
|
|
|
page = paginated.get_page(request.GET.get("page"))
|
2021-01-13 17:54:35 +00:00
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"author": author,
|
2021-10-21 00:36:52 +00:00
|
|
|
"books": page,
|
|
|
|
"page_range": paginated.get_elided_page_range(
|
|
|
|
page.number, on_each_side=2, on_ends=1
|
|
|
|
),
|
2021-01-13 17:54:35 +00:00
|
|
|
}
|
2021-05-10 23:32:24 +00:00
|
|
|
return TemplateResponse(request, "author/author.html", data)
|
2021-01-13 17:54:35 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-01-13 17:54:35 +00:00
|
|
|
@method_decorator(
|
2021-03-08 16:49:10 +00:00
|
|
|
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
|
|
|
|
)
|
2021-01-13 17:54:35 +00:00
|
|
|
class EditAuthor(View):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""edit author info"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-13 17:54:35 +00:00
|
|
|
def get(self, request, author_id):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""info about a book"""
|
2021-01-13 17:54:35 +00:00
|
|
|
author = get_object_or_404(models.Author, id=author_id)
|
2021-03-08 16:49:10 +00:00
|
|
|
data = {"author": author, "form": forms.AuthorForm(instance=author)}
|
2021-05-10 23:32:24 +00:00
|
|
|
return TemplateResponse(request, "author/edit_author.html", data)
|
2021-01-13 17:54:35 +00:00
|
|
|
|
|
|
|
def post(self, request, author_id):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""edit a author cool"""
|
2021-01-13 17:54:35 +00:00
|
|
|
author = get_object_or_404(models.Author, id=author_id)
|
|
|
|
|
|
|
|
form = forms.AuthorForm(request.POST, request.FILES, instance=author)
|
|
|
|
if not form.is_valid():
|
2021-03-08 16:49:10 +00:00
|
|
|
data = {"author": author, "form": form}
|
2021-05-10 23:32:24 +00:00
|
|
|
return TemplateResponse(request, "author/edit_author.html", data)
|
2021-01-13 17:54:35 +00:00
|
|
|
author = form.save()
|
|
|
|
|
2021-09-18 18:32:00 +00:00
|
|
|
return redirect(f"/author/{author.id}")
|