Add cover via url

This commit is contained in:
Mouse Reeve 2021-03-18 10:03:53 -07:00
parent ae6d8529af
commit 9470b2831f
3 changed files with 32 additions and 5 deletions

View file

@ -10,17 +10,27 @@
{% endblock %}
{% block modal-body %}
<section class="modal-card-body">
<section class="modal-card-body columns">
{% csrf_token %}
{% csrf_token %}
<label class="label">
<input type="file" name="cover" accept="image/*" enctype="multipart/form-data" id="id_cover">
<div class="column">
<label class="label" for="id_cover">
{% trans "Upload cover:" %}
</label>
<input type="file" name="cover" accept="image/*" enctype="multipart/form-data" id="id_cover">
</div>
<div class="column">
<label class="label" for="id_cover_url">
{% trans "Load cover from url:" %}
</label>
<input class="input" name="cover-url" id="id_cover_url">
</div>
</section>
{% endblock %}
{% block modal-footer %}
<button class="button is-primary" type="submit">{% trans "Add" %}</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

@ -152,7 +152,9 @@ urlpatterns = [
re_path(r"^create-book/?$", views.EditBook.as_view()),
re_path(r"^create-book/confirm?$", views.ConfirmEditBook.as_view()),
re_path(r"%s/editions(.json)?/?$" % book_path, views.Editions.as_view()),
re_path(r"^upload-cover/(?P<book_id>\d+)/?$", views.upload_cover, name="upload-cover"),
re_path(
r"^upload-cover/(?P<book_id>\d+)/?$", views.upload_cover, name="upload-cover"
),
re_path(r"^add-description/(?P<book_id>\d+)/?$", views.add_description),
re_path(r"^resolve-book/?$", views.resolve_book),
re_path(r"^switch-edition/?$", views.switch_edition),

View file

@ -1,6 +1,9 @@
""" the good stuff! the books! """
from uuid import uuid4
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.postgres.search import SearchRank, SearchVector
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
@ -14,6 +17,7 @@ from django.views.decorators.http import require_POST
from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from bookwyrm.connectors.abstract_connector import get_image
from bookwyrm.settings import PAGE_LENGTH
from .helpers import is_api_request, get_activity_feed, get_edition
from .helpers import privacy_filter
@ -257,6 +261,17 @@ def upload_cover(request, book_id):
""" upload a new cover """
book = get_object_or_404(models.Edition, id=book_id)
url = request.POST.get("cover-url")
if url:
# load it from a url
image_file = get_image(url)
if not image_file:
return redirect("/book/%d" % book.id)
image_name = str(uuid4()) + "." + url.split(".")[-1]
image_content = ContentFile(image_file.content)
book.cover.save(*[image_name, image_content])
return redirect("/book/%d" % book.id)
form = forms.CoverForm(request.POST, request.FILES, instance=book)
if not form.is_valid():
return redirect("/book/%d" % book.id)