0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 11:10:43 +01:00

Implement filter by content type on page search

This commit is contained in:
Karl Hobley 2018-05-01 16:03:32 +01:00 committed by Matt Westcott
parent bb8df218e5
commit ce2bdb9644
2 changed files with 60 additions and 7 deletions

View File

@ -2,7 +2,7 @@
<div class="nice-padding">
{% if pages %}
<h2>
{% blocktrans count counter=pages.paginator.count %}
{% blocktrans count counter=all_pages.count %}
There is one matching page
{% plural %}
There are {{ counter }} matching pages
@ -11,6 +11,27 @@
{% search_other %}
{% if pages.object_list.supports_facet %}
<nav class="listing-filter">
<h3 class="filter-title">{% trans "Page types" %}</h3>
<ul class="filter-options">
{% if not selected_content_type %}
<li style="background-color: #E6E6E6">All ({{ all_pages.count }})</li>
{% else %}
<li><a href="{% url 'wagtailadmin_pages:search' %}?q={{ query_string|urlencode }}">All ({{ all_pages.count }})</a></li>
{% endif %}
{% for content_type, count in content_types %}
{% if content_type == selected_content_type %}
<li style="background-color: #E6E6E6">{{ content_type.model_class.get_verbose_name }} ({{ count }})</li>
{% else %}
<li><a href="{% url 'wagtailadmin_pages:search' %}?q={{ query_string|urlencode }}&amp;content_type={{ content_type.app_label }}.{{ content_type.model|lower }}">{{ content_type.model_class.get_verbose_name }} ({{ count }})</a></li>
{% endif %}
{% endfor %}
</ul>
</nav>
{% endif %}
{% include "wagtailadmin/pages/listing/_list_explore.html" with show_parent=1 %}
{% url 'wagtailadmin_pages:search' as pagination_base_url %}
@ -18,7 +39,7 @@
{% else %}
{% if query_string %}
<h2>{% blocktrans %}Sorry, no pages match <em>"{{ query_string }}"</em>{% endblocktrans %}</h2>
{% search_other %}
{% else %}
<p>{% trans 'Enter a search term above' %}</p>

View File

@ -886,31 +886,63 @@ def copy(request, page_id):
@vary_on_headers('X-Requested-With')
@user_passes_test(user_has_any_page_permission)
def search(request):
pages = []
pages = all_pages = Page.objects.all().prefetch_related('content_type').specific()
q = MATCH_ALL
content_types = []
pagination_query_params = {}
if 'content_type' in request.GET:
pagination_query_params['content_type'] = request.GET['content_type']
app_label, model_name = request.GET['content_type'].split('.')
try:
selected_content_type = ContentType.objects.get_by_natural_key(app_label, model_name)
except ContentType.DoesNotExist:
raise Http404
pages = pages.filter(content_type=selected_content_type)
else:
selected_content_type = None
if 'q' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
q = form.cleaned_data['q']
pagination_query_params['q'] = q
all_pages = all_pages.search(q)
pages = pages.search(q)
if pages.supports_facet:
content_types = [
(ContentType.objects.get(id=content_type_id), count)
for content_type_id, count in all_pages.facet('content_type_id').items()
]
pages = Page.objects.all().prefetch_related('content_type').specific().search(q)
paginator, pages = paginate(request, pages)
else:
form = SearchForm()
paginator, pages = paginate(request, pages)
if request.is_ajax():
return render(request, "wagtailadmin/pages/search_results.html", {
'pages': pages,
'all_pages': all_pages,
'query_string': q,
'pagination_query_params': ('q=%s' % q) if q else ''
'content_types': content_types,
'selected_content_type': selected_content_type,
'pagination_query_params': '&'.join('{}={}'.format(*p) for p in pagination_query_params.items()),
})
else:
return render(request, "wagtailadmin/pages/search.html", {
'search_form': form,
'pages': pages,
'all_pages': all_pages,
'query_string': q,
'pagination_query_params': ('q=%s' % q) if q else ''
'content_types': content_types,
'selected_content_type': selected_content_type,
'pagination_query_params': '&'.join('{}={}'.format(*p) for p in pagination_query_params.items()),
})