From 0e6895633c11b14cd88b168a77422b0c0a9929ed Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Mon, 27 Sep 2021 13:00:16 +0200 Subject: [PATCH] Allow adding a cover by url when adding a new book (#1443) --- bookwyrm/templates/book/edit_book.html | 4 +--- bookwyrm/views/books.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/bookwyrm/templates/book/edit_book.html b/bookwyrm/templates/book/edit_book.html index 2f6ca324..18cf6671 100644 --- a/bookwyrm/templates/book/edit_book.html +++ b/bookwyrm/templates/book/edit_book.html @@ -236,14 +236,12 @@ {{ form.cover }} - {% if book %}
- +
- {% endif %} {% for error in form.cover.errors %}

{{ error | escape }}

{% endfor %} diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index a31a99e7..6bd97b99 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -185,6 +185,8 @@ class EditBook(View): data["confirm_mode"] = True # this isn't preserved because it isn't part of the form obj data["remove_authors"] = request.POST.getlist("remove_authors") + data["cover_url"] = request.POST.get("cover-url") + # make sure the dates are passed in as datetime, they're currently a string # QueryDicts are immutable, we need to copy formcopy = data["form"].data.copy() @@ -261,12 +263,20 @@ class ConfirmEditBook(View): work = models.Work.objects.create(title=form.cleaned_data["title"]) work.authors.set(book.authors.all()) book.parent_work = work - # we don't tell the world when creating a book - book.save(broadcast=False) for author_id in request.POST.getlist("remove_authors"): book.authors.remove(author_id) + # import cover, if requested + url = request.POST.get("cover-url") + if url: + image = set_cover_from_url(url) + if image: + book.cover.save(*image, save=False) + + # we don't tell the world when creating a book + book.save(broadcast=False) + return redirect(f"/book/{book.id}")