2016-12-09 05:11:31 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
|
|
|
|
2019-05-13 15:11:16 +00:00
|
|
|
from wagtail.core.models import Page
|
|
|
|
from wagtail.search.models import Query
|
2016-12-09 05:11:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def search(request):
|
|
|
|
search_query = request.GET.get('query', None)
|
|
|
|
page = request.GET.get('page', 1)
|
|
|
|
|
|
|
|
# Search
|
|
|
|
if search_query:
|
|
|
|
search_results = Page.objects.live().search(search_query)
|
|
|
|
query = Query.get(search_query)
|
|
|
|
|
|
|
|
# Record hit
|
|
|
|
query.add_hit()
|
|
|
|
else:
|
|
|
|
search_results = Page.objects.none()
|
|
|
|
|
|
|
|
# Pagination
|
|
|
|
paginator = Paginator(search_results, 10)
|
|
|
|
try:
|
|
|
|
search_results = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
search_results = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
search_results = paginator.page(paginator.num_pages)
|
|
|
|
|
|
|
|
return render(request, 'search/search.html', {
|
|
|
|
'search_query': search_query,
|
|
|
|
'search_results': search_results,
|
|
|
|
})
|