moviewyrm/bookwyrm/views/author.py

91 lines
3.2 KiB
Python
Raw Normal View History

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
2022-02-25 20:04:21 +00:00
from django.db.models import Avg, 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
2021-12-05 20:47:43 +00:00
from django.views.decorators.http import require_POST
2021-01-13 17:54:35 +00:00
from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
2021-12-05 20:47:43 +00:00
from bookwyrm.connectors import connector_manager
2021-10-21 00:36:52 +00:00
from bookwyrm.settings import PAGE_LENGTH
2022-03-02 09:12:32 +00:00
from bookwyrm.views.helpers import is_api_request, maybe_redirect_local_path
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
def get(self, request, author_id, slug=None):
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())
2022-03-02 09:12:32 +00:00
if r := maybe_redirect_local_path(request, author):
return r
2022-02-05 03:44:03 +00:00
books = (
models.Work.objects.filter(Q(authors=author) | Q(editions__authors=author))
2022-02-25 20:04:21 +00:00
.annotate(Avg("editions__review__rating"))
.order_by("editions__review__rating__avg")
2022-02-05 03:44:03 +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}")
2021-12-05 20:47:43 +00:00
@login_required
@require_POST
@permission_required("bookwyrm.edit_book", raise_exception=True)
# pylint: disable=unused-argument
2021-12-05 21:02:42 +00:00
def update_author_from_remote(request, author_id, connector_identifier):
2021-12-05 20:47:43 +00:00
"""load the remote data for this author"""
connector = connector_manager.load_connector(
get_object_or_404(models.Connector, identifier=connector_identifier)
)
2021-12-05 21:24:40 +00:00
author = get_object_or_404(models.Author, id=author_id)
2021-12-05 20:47:43 +00:00
connector.update_author_from_remote(author)
return redirect("author", author.id)