From 0e6895633c11b14cd88b168a77422b0c0a9929ed Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Mon, 27 Sep 2021 13:00:16 +0200 Subject: [PATCH 01/55] 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 2f6ca3242..18cf66710 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 a31a99e71..6bd97b991 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}") From 9db75cc5b7c70516aa8aea364173e56db1a0a704 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Mon, 27 Sep 2021 13:04:30 +0200 Subject: [PATCH 02/55] Add test for creating a book with a cover url --- bookwyrm/tests/views/test_book.py | 55 +++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/bookwyrm/tests/views/test_book.py b/bookwyrm/tests/views/test_book.py index 99022ec5e..d3bec6f01 100644 --- a/bookwyrm/tests/views/test_book.py +++ b/bookwyrm/tests/views/test_book.py @@ -282,6 +282,46 @@ class BookViews(TestCase): self.assertEqual(book.authors.first().name, "Sappho") self.assertEqual(book.authors.first(), book.parent_work.authors.first()) + def _setup_cover_url(self): + cover_url = "http://example.com" + image_file = pathlib.Path(__file__).parent.joinpath( + "../../static/images/default_avi.jpg" + ) + image = Image.open(image_file) + output = BytesIO() + image.save(output, format=image.format) + responses.add( + responses.GET, + cover_url, + body=output.getvalue(), + status=200, + ) + return cover_url + + @responses.activate + def test_create_book_upload_cover_url(self): + """create an entirely new book and work with cover url""" + self.assertFalse(self.book.cover) + view = views.ConfirmEditBook.as_view() + self.local_user.groups.add(self.group) + cover_url = self._setup_cover_url() + + form = forms.EditionForm() + form.data["title"] = "New Title" + form.data["last_edited_by"] = self.local_user.id + form.data["cover-url"] = cover_url + request = self.factory.post("", form.data) + request.user = self.local_user + + 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) + def test_upload_cover_file(self): """add a cover via file upload""" self.assertFalse(self.book.cover) @@ -310,21 +350,8 @@ class BookViews(TestCase): def test_upload_cover_url(self): """add a cover via url""" self.assertFalse(self.book.cover) - image_file = pathlib.Path(__file__).parent.joinpath( - "../../static/images/default_avi.jpg" - ) - image = Image.open(image_file) - output = BytesIO() - image.save(output, format=image.format) - responses.add( - responses.GET, - "http://example.com", - body=output.getvalue(), - status=200, - ) - form = forms.CoverForm(instance=self.book) - form.data["cover-url"] = "http://example.com" + form.data["cover-url"] = self._setup_cover_url() request = self.factory.post("", form.data) request.user = self.local_user From 9a06b7d4936b20f05c4dd0d9fd89daad741aba38 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 28 Sep 2021 09:24:09 -0700 Subject: [PATCH 03/55] Fixes incorrect label in edit user panel --- bookwyrm/templates/preferences/edit_user.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/preferences/edit_user.html b/bookwyrm/templates/preferences/edit_user.html index 4dc792f9b..a0c7a79ab 100644 --- a/bookwyrm/templates/preferences/edit_user.html +++ b/bookwyrm/templates/preferences/edit_user.html @@ -46,7 +46,7 @@ {% trans "Show reading goal prompt in feed:" %} {{ form.show_goal }} -