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" 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 upload_cover(request, book_id): ''' upload a new cover ''' book = get_object_or_404(models.Edition, id=book_id) form = forms.CoverForm(request.POST, request.FILES, instance=book) if not form.is_valid(): return redirect('/book/%d' % book.id) book.cover = form.files['cover'] book.save() return redirect('/book/%s' % book.id)
def upload_cover(request, book_id): """ upload a new cover """ book = get_object_or_404(models.Edition, id=book_id) form = forms.CoverForm(request.POST, request.FILES, instance=book) if not form.is_valid(): 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 test_upload_cover_url(self): """add a cover via url""" self.assertFalse(self.book.cover) form = forms.CoverForm(instance=self.book) form.data["cover-url"] = _setup_cover_url() request = self.factory.post("", form.data) request.user = self.local_user with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ) 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 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: 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() or not form.files.get("cover"): return redirect("/book/%d" % book.id) book.cover = form.files["cover"] book.save() return redirect("/book/%s" % book.id)
def upload_cover(request, book_id): ''' upload a new cover ''' # TODO: alternate covers? if not request.method == 'POST': return redirect('/book/%s' % request.user.localname) try: book = models.Edition.objects.get(id=book_id) except models.Edition.DoesNotExist: return HttpResponseNotFound() form = forms.CoverForm(request.POST, request.FILES, instance=book) if not form.is_valid(): return redirect(request.headers.get('Referer', '/')) book.cover = form.files['cover'] book.sync_cover = False book.save() outgoing.handle_update_book(request.user, book) return redirect('/book/%s' % book.id)
def test_upload_cover_file(self): """ add a cover via file upload """ self.assertFalse(self.book.cover) image_file = pathlib.Path(__file__).parent.joinpath( "../../static/images/default_avi.jpg") form = forms.CoverForm(instance=self.book) form.data["cover"] = SimpleUploadedFile(image_file, open(image_file, "rb").read(), content_type="image/jpeg") 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 book_page(request, book_id): ''' info about a book ''' book = models.Book.objects.select_subclasses().get(id=book_id) if is_api_request(request): return JsonResponse(book.to_activity(), encoder=ActivityEncoder) if isinstance(book, models.Work): book = book.default_edition if not book: return HttpResponseNotFound() work = book.parent_work if not work: return HttpResponseNotFound() reviews = models.Review.objects.filter(book__in=work.edition_set.all(), ) reviews = get_activity_feed(request.user, 'federated', model=reviews) user_tags = [] readthroughs = [] if request.user.is_authenticated: user_tags = models.Tag.objects.filter( book=book, user=request.user).values_list('identifier', flat=True) readthroughs = models.ReadThrough.objects.filter( user=request.user, book=book, ).order_by('start_date') rating = reviews.aggregate(Avg('rating')) tags = models.Tag.objects.filter(book=book).values( 'book', 'name', 'identifier').distinct().all() data = { 'title': book.title, 'book': book, 'reviews': reviews.filter(content__isnull=False), 'ratings': reviews.filter(content__isnull=True), 'rating': rating['rating__avg'], 'tags': tags, 'user_tags': user_tags, 'review_form': forms.ReviewForm(), 'quotation_form': forms.QuotationForm(), 'comment_form': forms.CommentForm(), 'readthroughs': readthroughs, 'tag_form': forms.TagForm(), 'path': '/book/%s' % book_id, 'cover_form': forms.CoverForm(instance=book), 'info_fields': [ { 'name': 'ISBN', 'value': book.isbn_13 }, { 'name': 'OCLC number', 'value': book.oclc_number }, { 'name': 'OpenLibrary ID', 'value': book.openlibrary_key }, { 'name': 'Goodreads ID', 'value': book.goodreads_key }, { 'name': 'Format', 'value': book.physical_format }, { 'name': 'Pages', 'value': book.pages }, ], } return TemplateResponse(request, 'book.html', data)