diff --git a/main/models.py b/main/models.py index 3460b1250704617cccd8e2eda1e36133ea186e75..66a64279a09a1576396e3cf8fcb326bc15d58d28 100644 --- a/main/models.py +++ b/main/models.py @@ -23,6 +23,7 @@ from wagtail.blocks import CharBlock, RichTextBlock from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.fields import RichTextField, StreamField from wagtail.models import Page +from wagtail.search import index from wagtailmetadata.models import MetadataPageMixin from elections2021.constants import REGION_CHOICES # pozor, import ze sousednĂho modulu @@ -487,6 +488,23 @@ class MainArticlesPage( } return JsonResponse(data=data, safe=False) + @route(r"^search") + def search(self, request): + if request.method == "GET" and "q" in request.GET: + query = request.GET["q"] + + results = MainArticlePage.objects.live().search(query) + + return render( + "main/main_article_search.html", + { + "page": self, + "results": results, + } + ) + else: + return HttpResponseRedirect(self.url) + def serve(self, request, *args, **kwargs): if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest": if "months" in request.GET: @@ -552,6 +570,14 @@ class MainArticlePage( ) tags = ClusterTaggableManager(through=MainArticleTag, blank=True) + search_fields = Page.search_fields + [ + index.SearchField("title"), + index.SearchField("author"), + index.SearchField("author_page"), + index.SearchField("perex"), + index.SearchField("content"), + ] + ### PANELS content_panels = ArticleMixin.content_panels + [