moviewyrm/bookwyrm/views/author.py

59 lines
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
from django.db.models import Q
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
from .helpers import is_api_request
# pylint: disable= no-self-use
class Author(View):
2021-03-08 16:49:10 +00:00
""" this person wrote a book """
2021-01-13 17:54:35 +00:00
def get(self, request, author_id):
2021-03-08 16:49:10 +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())
books = models.Work.objects.filter(
2021-03-08 16:49:10 +00:00
Q(authors=author) | Q(editions__authors=author)
).distinct()
2021-01-13 17:54:35 +00:00
data = {
2021-03-08 16:49:10 +00:00
"author": author,
"books": [b.get_default_edition() for b in books],
2021-01-13 17:54:35 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "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-03-08 16:49:10 +00:00
""" edit author info """
2021-01-13 17:54:35 +00:00
def get(self, request, author_id):
2021-03-08 16:49:10 +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)}
return TemplateResponse(request, "edit_author.html", data)
2021-01-13 17:54:35 +00:00
def post(self, request, author_id):
2021-03-08 16:49:10 +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}
return TemplateResponse(request, "edit_author.html", data)
2021-01-13 17:54:35 +00:00
author = form.save()
2021-03-08 16:49:10 +00:00
return redirect("/author/%s" % author.id)