mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Merge pull request #1456 from Tak/add-cover-from-url-while-adding-book
Add cover from url while adding book
This commit is contained in:
commit
002a9c4267
3 changed files with 54 additions and 19 deletions
|
@ -236,14 +236,12 @@
|
||||||
<label class="label" for="id_cover">{% trans "Upload cover:" %}</label>
|
<label class="label" for="id_cover">{% trans "Upload cover:" %}</label>
|
||||||
{{ form.cover }}
|
{{ form.cover }}
|
||||||
</div>
|
</div>
|
||||||
{% if book %}
|
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label" for="id_cover_url">
|
<label class="label" for="id_cover_url">
|
||||||
{% trans "Load cover from url:" %}
|
{% trans "Load cover from url:" %}
|
||||||
</label>
|
</label>
|
||||||
<input class="input" name="cover-url" id="id_cover_url">
|
<input class="input" name="cover-url" id="id_cover_url" type="url" value="{{ cover_url|default:'' }}">
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
|
||||||
{% for error in form.cover.errors %}
|
{% for error in form.cover.errors %}
|
||||||
<p class="help is-danger">{{ error | escape }}</p>
|
<p class="help is-danger">{{ error | escape }}</p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -283,6 +283,46 @@ class BookViews(TestCase):
|
||||||
self.assertEqual(book.authors.first().name, "Sappho")
|
self.assertEqual(book.authors.first().name, "Sappho")
|
||||||
self.assertEqual(book.authors.first(), book.parent_work.authors.first())
|
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):
|
def test_upload_cover_file(self):
|
||||||
"""add a cover via file upload"""
|
"""add a cover via file upload"""
|
||||||
self.assertFalse(self.book.cover)
|
self.assertFalse(self.book.cover)
|
||||||
|
@ -311,21 +351,8 @@ class BookViews(TestCase):
|
||||||
def test_upload_cover_url(self):
|
def test_upload_cover_url(self):
|
||||||
"""add a cover via url"""
|
"""add a cover via url"""
|
||||||
self.assertFalse(self.book.cover)
|
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 = 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 = self.factory.post("", form.data)
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
|
|
|
@ -191,6 +191,8 @@ class EditBook(View):
|
||||||
data["confirm_mode"] = True
|
data["confirm_mode"] = True
|
||||||
# this isn't preserved because it isn't part of the form obj
|
# this isn't preserved because it isn't part of the form obj
|
||||||
data["remove_authors"] = request.POST.getlist("remove_authors")
|
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
|
# make sure the dates are passed in as datetime, they're currently a string
|
||||||
# QueryDicts are immutable, we need to copy
|
# QueryDicts are immutable, we need to copy
|
||||||
formcopy = data["form"].data.copy()
|
formcopy = data["form"].data.copy()
|
||||||
|
@ -267,12 +269,20 @@ class ConfirmEditBook(View):
|
||||||
work = models.Work.objects.create(title=form.cleaned_data["title"])
|
work = models.Work.objects.create(title=form.cleaned_data["title"])
|
||||||
work.authors.set(book.authors.all())
|
work.authors.set(book.authors.all())
|
||||||
book.parent_work = work
|
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"):
|
for author_id in request.POST.getlist("remove_authors"):
|
||||||
book.authors.remove(author_id)
|
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}")
|
return redirect(f"/book/{book.id}")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue