diff --git a/wagtail/wagtailsearch/urls/frontend.py b/wagtail/wagtailsearch/urls/frontend.py deleted file mode 100644 index 54c33c27cf..0000000000 --- a/wagtail/wagtailsearch/urls/frontend.py +++ /dev/null @@ -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"), -) diff --git a/wagtail/wagtailsearch/views/__init__.py b/wagtail/wagtailsearch/views/__init__.py index e69de29bb2..46e2526f62 100644 --- a/wagtail/wagtailsearch/views/__init__.py +++ b/wagtail/wagtailsearch/views/__init__.py @@ -0,0 +1 @@ +from frontend import search \ No newline at end of file diff --git a/wagtail/wagtailsearch/views/frontend.py b/wagtail/wagtailsearch/views/frontend.py index 67f4e7ca73..ab55ec6680 100644 --- a/wagtail/wagtailsearch/views/frontend.py +++ b/wagtail/wagtailsearch/views/frontend.py @@ -9,13 +9,30 @@ from wagtail.wagtailcore import models from wagtail.wagtailsearch.models import Query -def search(request): - query_string = request.GET.get("q", "") - page = request.GET.get("p", 1) +def search( + request, + 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 - if query_string != "": - search_results = models.Page.search(query_string, path=request.site.root_page.path) + if query_string != '': + 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 query = Query.get(query_string) @@ -24,7 +41,7 @@ def search(request): query.add_hit() # Pagination - paginator = Paginator(search_results, 10) + paginator = Paginator(search_results, results_per_page) try: search_results = paginator.page(page) except PageNotAnInteger: @@ -35,34 +52,28 @@ def search(request): query = None search_results = None - # Render - template_name = None - if request.is_ajax(): - 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') + if use_json: # Return a json response + if search_results: + search_results_json = [] + for result in search_results: + result_specific = result.specific - return render(request, template_name, dict(query_string=query_string, search_results=search_results, is_ajax=request.is_ajax(), query=query)) + search_results_json.append({ + attr: getattr(result_specific, attr) + for attr in json_attrs + if hasattr(result_specific, attr) + }) + return HttpResponse(json.dumps(search_results_json)) + else: + return HttpResponse('[]') + else: # Render a template + if request.is_ajax() and template_ajax: + template = template_ajax -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: - search_name = result.specific.search_name - - suggestions.append({ - "label": result.title, - "type": search_name if search_name else '', - "url": result.url, - }) - - return HttpResponse(json.dumps(suggestions)) - else: - return HttpResponse("[]") + return render(request, template, dict( + query_string=query_string, + search_results=search_results, + is_ajax=request.is_ajax(), + query=query + ))