2021-03-08 16:49:10 +00:00
|
|
|
""" isbn search view """
|
2021-08-21 17:48:26 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-03-01 20:09:21 +00:00
|
|
|
from django.http import JsonResponse
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.views import View
|
|
|
|
|
2021-09-16 17:55:23 +00:00
|
|
|
from bookwyrm import book_search
|
2021-08-21 17:48:26 +00:00
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
2021-03-01 20:09:21 +00:00
|
|
|
from .helpers import is_api_request
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
class Isbn(View):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""search a book by isbn"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-03-01 20:09:21 +00:00
|
|
|
def get(self, request, isbn):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""info about a book"""
|
2021-09-16 17:55:23 +00:00
|
|
|
book_results = book_search.isbn_search(isbn)
|
2021-03-01 20:09:21 +00:00
|
|
|
|
|
|
|
if is_api_request(request):
|
2021-09-16 19:52:10 +00:00
|
|
|
return JsonResponse(
|
|
|
|
[book_search.format_search_result(r) for r in book_results], safe=False
|
|
|
|
)
|
2021-03-01 20:09:21 +00:00
|
|
|
|
2021-08-21 17:48:26 +00:00
|
|
|
paginated = Paginator(book_results, PAGE_LENGTH).get_page(
|
|
|
|
request.GET.get("page")
|
|
|
|
)
|
2021-03-01 20:09:21 +00:00
|
|
|
data = {
|
2021-08-21 17:48:26 +00:00
|
|
|
"results": [{"results": paginated}],
|
2021-03-08 16:49:10 +00:00
|
|
|
"query": isbn,
|
2021-08-21 17:48:26 +00:00
|
|
|
"type": "book",
|
2021-03-01 20:09:21 +00:00
|
|
|
}
|
2021-08-21 17:48:26 +00:00
|
|
|
return TemplateResponse(request, "search/book.html", data)
|