kopia lustrzana https://github.com/wagtail/wagtail
Unified search view. Search view is now configured in urls.py.
rodzic
b33248519c
commit
3c4cd74cf6
|
@ -1,8 +0,0 @@
|
||||||
from django.conf.urls import patterns, url
|
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns(
|
|
||||||
"wagtail.wagtailsearch.views.frontend",
|
|
||||||
url(r"^$", "search", name="wagtailsearch_search"),
|
|
||||||
url(r"^suggest/$", "suggest", name="wagtailsearch_suggest"),
|
|
||||||
)
|
|
|
@ -0,0 +1 @@
|
||||||
|
from frontend import search
|
|
@ -9,13 +9,30 @@ from wagtail.wagtailcore import models
|
||||||
from wagtail.wagtailsearch.models import Query
|
from wagtail.wagtailsearch.models import Query
|
||||||
|
|
||||||
|
|
||||||
def search(request):
|
def search(
|
||||||
query_string = request.GET.get("q", "")
|
request,
|
||||||
page = request.GET.get("p", 1)
|
template='wagtailsearch/search_results.html',
|
||||||
|
template_ajax=None,
|
||||||
|
results_per_page=10,
|
||||||
|
use_json=False,
|
||||||
|
json_attrs=['title', 'url'],
|
||||||
|
show_unpublished=False,
|
||||||
|
search_title_only=False,
|
||||||
|
extra_filters={},
|
||||||
|
path=None,
|
||||||
|
):
|
||||||
|
query_string = request.GET.get('q', '')
|
||||||
|
page = request.GET.get('p', 1)
|
||||||
|
|
||||||
# Search
|
# Search
|
||||||
if query_string != "":
|
if query_string != '':
|
||||||
search_results = models.Page.search(query_string, path=request.site.root_page.path)
|
search_results = models.Page.search(
|
||||||
|
query_string,
|
||||||
|
show_unpublished=show_unpublished,
|
||||||
|
search_title_only=search_title_only,
|
||||||
|
extra_filters=extra_filters,
|
||||||
|
path=path if path else request.site.root_page.path
|
||||||
|
)
|
||||||
|
|
||||||
# Get query object
|
# Get query object
|
||||||
query = Query.get(query_string)
|
query = Query.get(query_string)
|
||||||
|
@ -24,7 +41,7 @@ def search(request):
|
||||||
query.add_hit()
|
query.add_hit()
|
||||||
|
|
||||||
# Pagination
|
# Pagination
|
||||||
paginator = Paginator(search_results, 10)
|
paginator = Paginator(search_results, results_per_page)
|
||||||
try:
|
try:
|
||||||
search_results = paginator.page(page)
|
search_results = paginator.page(page)
|
||||||
except PageNotAnInteger:
|
except PageNotAnInteger:
|
||||||
|
@ -35,34 +52,28 @@ def search(request):
|
||||||
query = None
|
query = None
|
||||||
search_results = None
|
search_results = None
|
||||||
|
|
||||||
# Render
|
if use_json: # Return a json response
|
||||||
template_name = None
|
if search_results:
|
||||||
if request.is_ajax():
|
search_results_json = []
|
||||||
template_name = getattr(settings, 'WAGTAILSEARCH_RESULTS_TEMPLATE_AJAX', None)
|
|
||||||
if template_name is None:
|
|
||||||
template_name = getattr(settings, 'WAGTAILSEARCH_RESULTS_TEMPLATE', 'wagtailsearch/search_results.html')
|
|
||||||
|
|
||||||
return render(request, template_name, dict(query_string=query_string, search_results=search_results, is_ajax=request.is_ajax(), query=query))
|
|
||||||
|
|
||||||
|
|
||||||
def suggest(request):
|
|
||||||
query_string = request.GET.get("q", "")
|
|
||||||
|
|
||||||
# Search
|
|
||||||
if query_string != "":
|
|
||||||
search_results = models.Page.search(query_string, search_title_only=True, path=request.site.root_page.path)[:5]
|
|
||||||
|
|
||||||
# Get list of suggestions
|
|
||||||
suggestions = []
|
|
||||||
for result in search_results:
|
for result in search_results:
|
||||||
search_name = result.specific.search_name
|
result_specific = result.specific
|
||||||
|
|
||||||
suggestions.append({
|
search_results_json.append({
|
||||||
"label": result.title,
|
attr: getattr(result_specific, attr)
|
||||||
"type": search_name if search_name else '',
|
for attr in json_attrs
|
||||||
"url": result.url,
|
if hasattr(result_specific, attr)
|
||||||
})
|
})
|
||||||
|
|
||||||
return HttpResponse(json.dumps(suggestions))
|
return HttpResponse(json.dumps(search_results_json))
|
||||||
else:
|
else:
|
||||||
return HttpResponse("[]")
|
return HttpResponse('[]')
|
||||||
|
else: # Render a template
|
||||||
|
if request.is_ajax() and template_ajax:
|
||||||
|
template = template_ajax
|
||||||
|
|
||||||
|
return render(request, template, dict(
|
||||||
|
query_string=query_string,
|
||||||
|
search_results=search_results,
|
||||||
|
is_ajax=request.is_ajax(),
|
||||||
|
query=query
|
||||||
|
))
|
||||||
|
|
Ładowanie…
Reference in New Issue