Modal to add link

This commit is contained in:
Mouse Reeve 2021-12-15 12:40:01 -08:00
parent 40d1beee20
commit 1d6b200172
4 changed files with 83 additions and 3 deletions

View file

@ -147,7 +147,7 @@
{% include 'snippets/toggle/open_button.html' with text=button_text controls_text="add_description" controls_uid=book.id focus="id_description" hide_active=True id="hide_description" %}
<div class="box is-hidden" id="add_description_{{ book.id }}">
<form name="add-description" method="POST" action="/add-description/{{ book.id }}">
<form name="add-description" method="POST" action="{% url "add-description" book.id %}">
{% csrf_token %}
<p class="fields is-grouped">
<label class="label" for="id_description_{{ book.id }}">{% trans "Description:" %}</label>
@ -345,6 +345,23 @@
{% endif %}
</section>
{% endif %}
<section class="content-block">
<h2 class="title is-5">{% trans "Get a copy" %}</h2>
{% if book.file_links %}
<ul>
{% for link in book.file_links.all %}
<li><a href="{{ link.url }}">{{ link.name }}</a> ({{ link.filetype }})</li>
{% endfor %}
</ul>
{% endif %}
{% if can_edit_book %}
{% trans "Add link to copy" as button_text %}
{% include 'snippets/toggle/toggle_button.html' with text=button_text controls_text="edit_file_links" controls_uid=book.id focus="modal_title_edit_file_links" class="is-small" icon_with_text="plus" %}
{% include 'book/file_links_modal.html' with book=book controls_text="edit_file_links" controls_uid=book.id %}
{% endif %}
</section>
</div>
</div>

View file

@ -0,0 +1,37 @@
{% extends 'components/modal.html' %}
{% load i18n %}
{% block modal-title %}
{% trans "Add file link" %}
{% endblock %}
{% block modal-form-open %}
<form name="add-link" method="POST" action="{% url 'add-file-link' book.id %}">
{% endblock %}
{% block modal-body %}
{% csrf_token %}
<div class="field">
<label class="label" for="id_url">{% trans "Description:" %}</label>
{{ file_link_form.name }}
</div>
<div class="columns">
<div class="column is-four-fifths">
<label class="label" for="id_url">{% trans "URL:" %}</label>
<input type="text" name="url" maxlength="255" class="input" required="" id="id_url" value="{% firstof file_link_form.url.value "" %}" placeholder="https://...">
</div>
<div class="column is-one-fifth">
<label class="label" for="id_filetype">{% trans "File type:" %}</label>
<input type="text" name="filetype" maxlength="5" class="input" required="" id="id_filetype" value="{% firstof file_link_form.filetype.value "" %}" placeholder="PDF">
</div>
</div>
{% endblock %}
{% block modal-footer %}
<button class="button is-primary" type="submit">{% trans "Save" %}</button>
{% trans "Cancel" as button_text %}
{% include 'snippets/toggle/toggle_button.html' with text=button_text %}
{% endblock %}
{% block modal-form-close %}</form>{% endblock %}

View file

@ -28,7 +28,13 @@ from .preferences.delete_user import DeleteUser
from .preferences.block import Block, unblock
# books
from .books.books import Book, upload_cover, add_description, resolve_book
from .books.books import (
Book,
upload_cover,
add_description,
resolve_book,
add_file_link,
)
from .books.books import update_book_from_remote
from .books.edit_book import EditBook, ConfirmEditBook
from .books.editions import Editions, switch_edition

View file

@ -4,6 +4,7 @@ from uuid import uuid4
from django.contrib.auth.decorators import login_required, permission_required
from django.core.files.base import ContentFile
from django.core.paginator import Paginator
from django.db import transaction
from django.db.models import Avg, Q
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
@ -40,7 +41,7 @@ class Book(View):
.filter(Q(id=book_id) | Q(parent_work__id=book_id))
.order_by("-edition_rank")
.select_related("parent_work")
.prefetch_related("authors")
.prefetch_related("authors", "file_links")
.first()
)
@ -84,6 +85,7 @@ class Book(View):
}
if request.user.is_authenticated:
data["file_link_form"] = forms.FileLinkForm()
readthroughs = models.ReadThrough.objects.filter(
user=request.user,
book=book,
@ -194,3 +196,21 @@ def update_book_from_remote(request, book_id, connector_identifier):
connector.update_book_from_remote(book)
return redirect("book", book.id)
@login_required
@require_POST
@permission_required("bookwyrm.edit_book", raise_exception=True)
@transaction.atomic
def add_file_link(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():
return redirect("book", book.id)
link = form.save()
book.file_links.add(link)
book.last_edited_by = request.user
book.save()
return redirect("book", book.id)