Search editions in view

This commit is contained in:
Mouse Reeve 2021-08-17 10:44:24 -07:00
parent 3b54852536
commit 6db3391f8b

View file

@ -1,7 +1,11 @@
""" the good stuff! the books! """ """ the good stuff! the books! """
from functools import reduce
import operator
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db import transaction from django.db import transaction
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.views import View from django.views import View
@ -30,10 +34,31 @@ class Editions(View):
if request.GET.get("format"): if request.GET.get("format"):
filters["physical_format__iexact"] = request.GET.get("format") filters["physical_format__iexact"] = request.GET.get("format")
query = request.GET.get("q")
search_filters = {}
if query:
searchable_array_fields = ["languages", "publishers"]
searchable_fields = [
"title",
"physical_format",
"isbn_10",
"isbn_13",
"oclc_number",
"asin",
]
search_filter_entries = [
{f"{f}__icontains": query} for f in searchable_fields
] + [{f"{f}__iexact": query} for f in searchable_array_fields]
search_filters = reduce(
operator.or_, (Q(**f) for f in search_filter_entries)
)
editions = work.editions.order_by("-edition_rank") editions = work.editions.order_by("-edition_rank")
languages = set(sum([e.languages for e in editions], [])) languages = set(sum([e.languages for e in editions], []))
paginated = Paginator(editions.filter(**filters), PAGE_LENGTH) paginated = Paginator(
editions.filter(**filters).filter(search_filters), PAGE_LENGTH
)
data = { data = {
"editions": paginated.get_page(request.GET.get("page")), "editions": paginated.get_page(request.GET.get("page")),
"work": work, "work": work,