Upload cover via url form edit book view

This commit is contained in:
Mouse Reeve 2021-03-19 10:29:34 -07:00
parent a8545e2701
commit e92a506e61
3 changed files with 45 additions and 16 deletions

View file

@ -85,7 +85,7 @@
<input type="hidden" name="last_edited_by" value="{{ request.user.id }}">
<div class="columns">
<div class="column">
<div class="column is-half">
<section class="block">
<h2 class="title is-4">{% trans "Metadata" %}</h2>
<p class="mb-2"><label class="label" for="id_title">{% trans "Title:" %}</label> {{ form.title }} </p>
@ -151,15 +151,26 @@
</section>
</div>
<div class="column">
<div class="column is-half">
<h2 class="title is-4">{% trans "Cover" %}</h2>
<div class="columns">
<div class="column is-narrow">
{% include 'snippets/book_cover.html' with book=book size="small" %}
</div>
<div class="column is-narrow">
<div class="block">
<h2 class="title is-4">{% trans "Cover" %}</h2>
<p>{{ form.cover }}</p>
<p>
<label class="label" for="id_cover">{% trans "Upload cover:" %}</label>
{{ form.cover }}
</p>
{% if book %}
<p>
<label class="label" for="id_cover_url">
{% trans "Load cover from url:" %}
</label>
<input class="input" name="cover-url" id="id_cover_url">
</p>
{% endif %}
{% for error in form.cover.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}

View file

@ -252,8 +252,11 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
views.upload_cover(request, self.book.id)
self.assertEqual(delay_mock.call_count, 1)
self.book.refresh_from_db()
self.assertTrue(self.book.cover)
@ -281,8 +284,11 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
views.upload_cover(request, self.book.id)
self.assertEqual(delay_mock.call_count, 1)
self.book.refresh_from_db()
self.assertTrue(self.book.cover)

View file

@ -179,7 +179,13 @@ class EditBook(View):
for author_id in remove_authors:
book.authors.remove(author_id)
book = form.save()
book = form.save(commit=False)
url = request.POST.get("cover-url")
if url:
image = set_cover_from_url(url)
if image:
book.cover.save(*image, save=False)
book.save()
return redirect("/book/%s" % book.id)
@ -260,29 +266,35 @@ class Editions(View):
def upload_cover(request, book_id):
""" upload a new cover """
book = get_object_or_404(models.Edition, id=book_id)
book.last_edited_by = request.user
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])
image = set_cover_from_url(url)
book.cover.save(*image)
return redirect("/book/%d" % book.id)
form = forms.CoverForm(request.POST, request.FILES, instance=book)
if not form.is_valid():
if not form.is_valid() or not form.files.get("cover"):
return redirect("/book/%d" % book.id)
book.last_edited_by = request.user
book.cover = form.files["cover"]
book.save()
return redirect("/book/%s" % book.id)
def set_cover_from_url(url):
""" load it from a url """
image_file = get_image(url)
if not image_file:
return None
image_name = str(uuid4()) + "." + url.split(".")[-1]
image_content = ContentFile(image_file.content)
return [image_name, image_content]
@login_required
@require_POST
@permission_required("bookwyrm.edit_book", raise_exception=True)