Move serializer to different file

This commit is contained in:
Jake Howard 2022-08-02 21:28:51 +01:00
parent 809cc5d454
commit 96e623e3db
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 14 additions and 10 deletions

View file

@ -5,7 +5,6 @@ from django.shortcuts import render
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET
from rest_framework import serializers
from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.models import Page from wagtail.models import Page
from wagtail.query import PageQuerySet from wagtail.query import PageQuerySet
@ -16,6 +15,8 @@ from website.common.models import BaseContentMixin, BasePage
from website.common.utils import TocEntry from website.common.utils import TocEntry
from website.home.models import HomePage from website.home.models import HomePage
from .serializers import MIN_SEARCH_LENGTH, SearchParamsSerializer
class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc]
max_count = 1 max_count = 1
@ -24,11 +25,6 @@ class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore
content_panels = BasePage.content_panels + BaseContentMixin.content_panels content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields search_fields = BasePage.search_fields + BaseContentMixin.search_fields
PAGE_SIZE = 15 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 @cached_property
def reading_time(self) -> int: def reading_time(self) -> int:
@ -45,7 +41,7 @@ class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore
context = super().get_context(request) context = super().get_context(request)
context["search_query"] = request.GET.get("q", "") context["search_query"] = request.GET.get("q", "")
context["search_url"] = self.reverse_subpage("results") context["search_url"] = self.reverse_subpage("results")
context["MIN_SEARCH_TERM"] = self.MIN_SEARCH_TERM context["MIN_SEARCH_LENGTH"] = MIN_SEARCH_LENGTH
return context return context
@route(r"^results/$") @route(r"^results/$")
@ -54,13 +50,13 @@ class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore
if not request.htmx: if not request.htmx:
return HttpResponseBadRequest() return HttpResponseBadRequest()
serializer = self.SearchParamsSerializer(data=request.GET) serializer = SearchParamsSerializer(data=request.GET)
if not serializer.is_valid(): if not serializer.is_valid():
return render( return render(
request, request,
"search/enter-search-term.html", "search/enter-search-term.html",
{"MIN_SEARCH_TERM": self.MIN_SEARCH_TERM}, {"MIN_SEARCH_LENGTH": MIN_SEARCH_LENGTH},
) )
search_query = serializer.validated_data["q"] search_query = serializer.validated_data["q"]

View file

@ -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)

View file

@ -1 +1 @@
<p>Enter a search term (of at least {{ MIN_SEARCH_TERM }} characters) to search</p> <p>Enter a search term (of at least {{ MIN_SEARCH_LENGTH }} characters) to search</p>