Edit book availability

This commit is contained in:
Mouse Reeve 2022-01-17 09:57:47 -08:00
parent 1595bac9b5
commit cfcacb4797
4 changed files with 55 additions and 10 deletions

View file

@ -34,9 +34,9 @@
<th>{% trans "Filetype" %}</th>
<th>{% trans "Domain" %}</th>
<th>{% trans "Status" %}</th>
<th>{% trans "Actions" %}</th>
<th colspan="2">{% trans "Actions" %}</th>
</tr>
{% for link in book.file_links.all %}
{% for link in links %}
<tr>
<td class="overflow-wrap-anywhere">
<a href="{{ link.url }}" target="_blank" rel="noopener">{{ link.url }}</a>
@ -66,7 +66,26 @@
{% endwith %}
</td>
<td>
<form name="delete-link-{{ link.id }}" class="control" method="post" action="{% url 'file-link' book.id link.id %}">
<form name="edit-link" class="control" method="post" action="{% url 'file-link' book.id link.id %}">
{% csrf_token %}
<input type="hidden" name="url" value="{{ link.form.url.value }}">
<input type="hidden" name="filetype" value="{{ link.form.filetype.value }}">
<input type="hidden" name="added_by" value="{{ link.form.added_by.value }}">
<input type="hidden" name="book" value="{{ link.form.book.value }}">
<div class="field has-addons">
<div class="control">
<div class="select">
{{ link.form.availability }}
</div>
</div>
<div class="control">
<button class="button is-primary" type="submit">{% trans "Save" %}</button>
</div>
</div>
</form>
</td>
<td>
<form name="delete-link-{{ link.id }}" class="control" method="post" action="{% url 'file-link-delete' book.id link.id %}">
{% csrf_token %}
<button class="button is-danger is-light" type="submit">Delete link</button>
</form>

View file

@ -473,10 +473,15 @@ urlpatterns = [
rf"{BOOK_PATH}/filelink/?$", views.BookFileLinks.as_view(), name="file-link"
),
re_path(
rf"{BOOK_PATH}/filelink/(?P<link_id>\d+)/delete/?$",
rf"{BOOK_PATH}/filelink/(?P<link_id>\d+)/?$",
views.BookFileLinks.as_view(),
name="file-link",
),
re_path(
rf"{BOOK_PATH}/filelink/(?P<link_id>\d+)/delete/?$",
views.delete_link,
name="file-link-delete",
),
re_path(
rf"{BOOK_PATH}/filelink/add/?$",
views.AddFileLink.as_view(),

View file

@ -37,7 +37,7 @@ from .books.books import (
from .books.books import update_book_from_remote
from .books.edit_book import EditBook, ConfirmEditBook
from .books.editions import Editions, switch_edition
from .books.links import BookFileLinks, AddFileLink
from .books.links import BookFileLinks, AddFileLink, delete_link
# landing
from .landing.about import about, privacy, conduct

View file

@ -5,28 +5,49 @@ 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 django.views.decorators.http import require_POST
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 BookFileLinks(View):
"""View all links"""
def get(self, request, book_id):
"""view links"""
book = get_object_or_404(models.Edition, id=book_id)
return TemplateResponse(
request, "book/file_links/edit_links.html", {"book": book}
)
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)
def post(self, request, book_id, link_id):
"""delete link"""
"""Edit a link"""
link = get_object_or_404(models.FileLink, id=link_id, book=book_id)
link.delete()
form = forms.FileLinkForm(request.POST, instance=link)
form.save()
return self.get(request, book_id)
@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)
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"