Use class view

This commit is contained in:
Mouse Reeve 2021-12-15 13:15:51 -08:00
parent cc3db31db9
commit dcf5694b66

View file

@ -0,0 +1,43 @@
""" the good stuff! the books! """
from django.contrib.auth.decorators import login_required, permission_required
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.views import View
from django.utils.decorators import method_decorator
from bookwyrm import forms, models
# pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
)
class FileLink(View):
"""a book! this is the stuff"""
def get(self, request, book_id, link_id=None):
"""info about a book"""
book = get_object_or_404(models.Edition, id=book_id)
link = get_object_or_404(models.FileLink, id=link_id) if link_id else None
data = {
"file_link_form": forms.FileLinkForm(instance=link),
"book": book,
}
return TemplateResponse(request, "book/file_link_page.html", data)
@transaction.atomic
def post(self, request, book_id):
"""Add a link to a copy of the book you can read"""
book = get_object_or_404(models.Book.objects.select_subclasses(), id=book_id)
form = forms.FileLinkForm(request.POST)
if not form.is_valid():
data = {"file_link_form": form, "book": book}
return TemplateResponse(request, "book/file_link_page.html", data)
link = form.save()
book.file_links.add(link)
book.last_edited_by = request.user
book.save()
return redirect("book", book.id)