Use serializer to validate search params
This commit is contained in:
parent
639f5885a4
commit
c4109e42f1
2 changed files with 43 additions and 25 deletions
|
@ -1,6 +1,7 @@
|
|||
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
||||
from django.core.paginator import EmptyPage, Paginator
|
||||
from django.http.request import HttpRequest
|
||||
from django.utils.functional import cached_property
|
||||
from rest_framework import serializers
|
||||
from wagtail.models import Page
|
||||
from wagtail.query import PageQuerySet
|
||||
from wagtail.search.models import Query
|
||||
|
@ -19,6 +20,10 @@ class SearchPage(BaseContentMixin, BasePage): # type: ignore[misc]
|
|||
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
|
||||
PAGE_SIZE = 15
|
||||
|
||||
class SearchParamsSerializer(serializers.Serializer):
|
||||
q = serializers.CharField()
|
||||
page = serializers.IntegerField(min_value=1, default=1)
|
||||
|
||||
@cached_property
|
||||
def reading_time(self) -> int:
|
||||
"""
|
||||
|
@ -35,15 +40,19 @@ class SearchPage(BaseContentMixin, BasePage): # type: ignore[misc]
|
|||
|
||||
def get_context(self, request: HttpRequest) -> dict:
|
||||
context = super().get_context(request)
|
||||
if query_string := request.GET.get("q", ""):
|
||||
filters, query = parse_query_string(query_string)
|
||||
Query.get(query_string).add_hit()
|
||||
|
||||
serializer = self.SearchParamsSerializer(data=request.GET)
|
||||
|
||||
if serializer.is_valid():
|
||||
search_query = serializer.validated_data["q"]
|
||||
filters, query = parse_query_string(search_query)
|
||||
Query.get(search_query).add_hit()
|
||||
pages = self.get_search_pages().search(query)
|
||||
else:
|
||||
pages = Page.objects.none()
|
||||
|
||||
paginator = Paginator(pages, self.PAGE_SIZE)
|
||||
page_num = request.GET.get("page", "1")
|
||||
context["paginator"] = paginator
|
||||
page_num = serializer.validated_data["page"]
|
||||
context["page_num"] = page_num
|
||||
try:
|
||||
results = paginator.page(page_num)
|
||||
|
||||
|
@ -51,13 +60,16 @@ class SearchPage(BaseContentMixin, BasePage): # type: ignore[misc]
|
|||
if not isinstance(results.object_list, PageQuerySet):
|
||||
results.object_list = Page.objects.filter(
|
||||
id__in=list(
|
||||
results.object_list.get_queryset().values_list("id", flat=True)
|
||||
results.object_list.get_queryset().values_list(
|
||||
"id", flat=True
|
||||
)
|
||||
)
|
||||
).specific()
|
||||
except (PageNotAnInteger, EmptyPage):
|
||||
results = None
|
||||
context["invalid_page"] = True
|
||||
except EmptyPage:
|
||||
results = []
|
||||
|
||||
context["results"] = results
|
||||
else:
|
||||
context["invalid_search"] = True
|
||||
|
||||
return context
|
||||
|
|
|
@ -11,8 +11,14 @@
|
|||
{% endif %}
|
||||
|
||||
<section class="container">
|
||||
{% if invalid_page %}
|
||||
<p>Invalid page</p>
|
||||
{% if invalid_search %}
|
||||
<p>Invalid search</p>
|
||||
{% elif results|length == 0 %}
|
||||
{% if page_num > paginator.num_pages %}
|
||||
<p>There aren't {{ page_num }} page - only {{ paginator.num_pages }}.</p>
|
||||
{% else %}
|
||||
<p>No results</p>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
{% for page in results %}
|
||||
{% include "common/listing-item.html" %}
|
||||
|
|
Loading…
Reference in a new issue