2021-12-15 21:15:51 +00:00
|
|
|
""" 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
|
2022-01-17 17:57:47 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2021-12-15 21:15:51 +00:00
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
2022-01-17 17:57:47 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
|
|
|
|
)
|
2022-01-12 17:52:26 +00:00
|
|
|
class BookFileLinks(View):
|
|
|
|
"""View all links"""
|
|
|
|
|
|
|
|
def get(self, request, book_id):
|
|
|
|
"""view links"""
|
|
|
|
book = get_object_or_404(models.Edition, id=book_id)
|
2022-01-17 17:57:47 +00:00
|
|
|
links = book.file_links.order_by("domain__status", "created_date")
|
|
|
|
annotated_links = []
|
|
|
|
for link in links.all():
|
|
|
|
link.form = forms.FileLinkForm(instance=link)
|
|
|
|
annotated_links.append(link)
|
|
|
|
|
|
|
|
data = {"book": book, "links": annotated_links}
|
|
|
|
return TemplateResponse(request, "book/file_links/edit_links.html", data)
|
2022-01-12 17:52:26 +00:00
|
|
|
|
2022-01-12 18:07:49 +00:00
|
|
|
def post(self, request, book_id, link_id):
|
2022-01-17 17:57:47 +00:00
|
|
|
"""Edit a link"""
|
2022-01-12 18:07:49 +00:00
|
|
|
link = get_object_or_404(models.FileLink, id=link_id, book=book_id)
|
2022-01-17 17:57:47 +00:00
|
|
|
form = forms.FileLinkForm(request.POST, instance=link)
|
|
|
|
form.save()
|
2022-01-12 18:07:49 +00:00
|
|
|
return self.get(request, book_id)
|
|
|
|
|
2022-01-12 17:52:26 +00:00
|
|
|
|
2022-01-17 17:57:47 +00:00
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def delete_link(request, book_id, link_id):
|
|
|
|
"""delete link"""
|
|
|
|
link = get_object_or_404(models.FileLink, id=link_id, book=book_id)
|
|
|
|
link.delete()
|
|
|
|
return redirect("file-link", book_id)
|
|
|
|
|
|
|
|
|
2021-12-15 21:15:51 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
|
|
|
|
)
|
2022-01-12 17:16:24 +00:00
|
|
|
class AddFileLink(View):
|
2021-12-15 21:15:51 +00:00
|
|
|
"""a book! this is the stuff"""
|
|
|
|
|
2022-01-12 17:16:24 +00:00
|
|
|
def get(self, request, book_id):
|
|
|
|
"""Create link form"""
|
2021-12-16 00:23:21 +00:00
|
|
|
book = get_object_or_404(models.Edition, id=book_id)
|
2021-12-15 21:15:51 +00:00
|
|
|
data = {
|
2022-01-12 17:16:24 +00:00
|
|
|
"file_link_form": forms.FileLinkForm(),
|
2021-12-15 21:15:51 +00:00
|
|
|
"book": book,
|
|
|
|
}
|
2022-01-13 00:46:14 +00:00
|
|
|
return TemplateResponse(request, "book/file_links/file_link_page.html", data)
|
2021-12-15 21:15:51 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
2021-12-15 21:20:05 +00:00
|
|
|
def post(self, request, book_id, link_id=None):
|
2021-12-15 21:15:51 +00:00
|
|
|
"""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)
|
2021-12-15 21:20:05 +00:00
|
|
|
link = get_object_or_404(models.FileLink, id=link_id) if link_id else None
|
|
|
|
form = forms.FileLinkForm(request.POST, instance=link)
|
2021-12-15 21:15:51 +00:00
|
|
|
if not form.is_valid():
|
|
|
|
data = {"file_link_form": form, "book": book}
|
2022-01-13 01:11:24 +00:00
|
|
|
return TemplateResponse(
|
|
|
|
request, "book/file_links/file_link_page.html", data
|
|
|
|
)
|
2021-12-15 21:15:51 +00:00
|
|
|
|
|
|
|
link = form.save()
|
|
|
|
book.file_links.add(link)
|
|
|
|
book.last_edited_by = request.user
|
|
|
|
book.save()
|
|
|
|
return redirect("book", book.id)
|