From 96e623e3dbb79e586fad851a17e007cad0c0cb16 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 2 Aug 2022 21:28:51 +0100 Subject: [PATCH] Move serializer to different file --- website/search/models.py | 14 +++++--------- website/search/serializers.py | 8 ++++++++ .../search/templates/search/enter-search-term.html | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 website/search/serializers.py diff --git a/website/search/models.py b/website/search/models.py index 18617ef..4a2bc43 100644 --- a/website/search/models.py +++ b/website/search/models.py @@ -5,7 +5,6 @@ from django.shortcuts import render from django.utils.decorators import method_decorator from django.utils.functional import cached_property from django.views.decorators.http import require_GET -from rest_framework import serializers from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.models import Page from wagtail.query import PageQuerySet @@ -16,6 +15,8 @@ from website.common.models import BaseContentMixin, BasePage from website.common.utils import TocEntry from website.home.models import HomePage +from .serializers import MIN_SEARCH_LENGTH, SearchParamsSerializer + class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] max_count = 1 @@ -24,11 +25,6 @@ class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore content_panels = BasePage.content_panels + BaseContentMixin.content_panels search_fields = BasePage.search_fields + BaseContentMixin.search_fields PAGE_SIZE = 15 - MIN_SEARCH_TERM = 3 - - class SearchParamsSerializer(serializers.Serializer): - q = serializers.CharField(min_length=3) - page = serializers.IntegerField(min_value=1, default=1) @cached_property def reading_time(self) -> int: @@ -45,7 +41,7 @@ class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore context = super().get_context(request) context["search_query"] = request.GET.get("q", "") context["search_url"] = self.reverse_subpage("results") - context["MIN_SEARCH_TERM"] = self.MIN_SEARCH_TERM + context["MIN_SEARCH_LENGTH"] = MIN_SEARCH_LENGTH return context @route(r"^results/$") @@ -54,13 +50,13 @@ class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore if not request.htmx: return HttpResponseBadRequest() - serializer = self.SearchParamsSerializer(data=request.GET) + serializer = SearchParamsSerializer(data=request.GET) if not serializer.is_valid(): return render( request, "search/enter-search-term.html", - {"MIN_SEARCH_TERM": self.MIN_SEARCH_TERM}, + {"MIN_SEARCH_LENGTH": MIN_SEARCH_LENGTH}, ) search_query = serializer.validated_data["q"] diff --git a/website/search/serializers.py b/website/search/serializers.py new file mode 100644 index 0000000..95664a4 --- /dev/null +++ b/website/search/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers + +MIN_SEARCH_LENGTH = 3 + + +class SearchParamsSerializer(serializers.Serializer): + q = serializers.CharField(min_length=MIN_SEARCH_LENGTH) + page = serializers.IntegerField(min_value=1, default=1) diff --git a/website/search/templates/search/enter-search-term.html b/website/search/templates/search/enter-search-term.html index ecb4e9f..1331462 100644 --- a/website/search/templates/search/enter-search-term.html +++ b/website/search/templates/search/enter-search-term.html @@ -1 +1 @@ -

Enter a search term (of at least {{ MIN_SEARCH_TERM }} characters) to search

+

Enter a search term (of at least {{ MIN_SEARCH_LENGTH }} characters) to search