From 6db3391f8bec09c3ee78b61e87d0e49ea3892597 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 17 Aug 2021 10:44:24 -0700 Subject: [PATCH] Search editions in view --- bookwyrm/views/editions.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/bookwyrm/views/editions.py b/bookwyrm/views/editions.py index d2c2a4d2e..b15e29b7b 100644 --- a/bookwyrm/views/editions.py +++ b/bookwyrm/views/editions.py @@ -1,7 +1,11 @@ """ the good stuff! the books! """ +from functools import reduce +import operator + from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.db import transaction +from django.db.models import Q from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.views import View @@ -30,10 +34,31 @@ class Editions(View): if 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") 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 = { "editions": paginated.get_page(request.GET.get("page")), "work": work,