0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Move page search view to wagtail.admin.views.pages.search

This commit is contained in:
Matt Westcott 2020-08-05 16:05:06 +01:00 committed by Matt Westcott
parent 666d581119
commit 73e6b3cb99
2 changed files with 114 additions and 104 deletions

View File

@ -6,7 +6,6 @@ from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator
from django.db import transaction
from django.http import Http404, HttpResponse
from django.http.request import QueryDict
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
@ -17,14 +16,12 @@ from django.utils.http import is_safe_url
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
from django.views.decorators.http import require_GET, require_POST
from django.views.decorators.vary import vary_on_headers
from django.views.generic import View
from wagtail.admin import messages
from wagtail.admin.action_menu import PageActionMenu
from wagtail.admin.auth import user_has_any_page_permission, user_passes_test
from wagtail.admin.filters import PageHistoryReportFilterSet
from wagtail.admin.forms.search import SearchForm
from wagtail.admin.mail import send_notification
from wagtail.admin.modal_workflow import render_modal_workflow
from wagtail.admin.views.pages.utils import get_valid_next_url_from_request
@ -32,8 +29,6 @@ from wagtail.admin.views.reports import ReportView
from wagtail.core import hooks
from wagtail.core.models import (
Page, PageLogEntry, PageRevision, Task, TaskState, UserPagePermissionsProxy, WorkflowState)
from wagtail.search.query import MATCH_ALL
from wagtail.search.utils import parse_query_string
from wagtail.admin.views.pages.copy import * # noqa
from wagtail.admin.views.pages.create import * # noqa
@ -41,6 +36,7 @@ from wagtail.admin.views.pages.edit import * # noqa
from wagtail.admin.views.pages.listing import * # noqa
from wagtail.admin.views.pages.move import * # noqa
from wagtail.admin.views.pages.preview import * # noqa
from wagtail.admin.views.pages.search import * # noqa
def content_type_use(request, content_type_app_name, content_type_model_name):
@ -187,105 +183,6 @@ def set_page_position(request, page_to_move_id):
return HttpResponse('')
@vary_on_headers('X-Requested-With')
@user_passes_test(user_has_any_page_permission)
def search(request):
pages = all_pages = Page.objects.all().prefetch_related('content_type').specific()
q = MATCH_ALL
content_types = []
pagination_query_params = QueryDict({}, mutable=True)
ordering = None
if 'ordering' in request.GET:
if request.GET['ordering'] in ['title', '-title', 'latest_revision_created_at', '-latest_revision_created_at', 'live', '-live']:
ordering = request.GET['ordering']
if ordering == 'title':
pages = pages.order_by('title')
elif ordering == '-title':
pages = pages.order_by('-title')
if ordering == 'latest_revision_created_at':
pages = pages.order_by('latest_revision_created_at')
elif ordering == '-latest_revision_created_at':
pages = pages.order_by('-latest_revision_created_at')
if ordering == 'live':
pages = pages.order_by('live')
elif ordering == '-live':
pages = pages.order_by('-live')
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
# Parse query
filters, query = parse_query_string(q, operator='and', zero_terms=MATCH_ALL)
# Live filter
live_filter = filters.get('live') or filters.get('published')
live_filter = live_filter and live_filter.lower()
if live_filter in ['yes', 'true']:
all_pages = all_pages.filter(live=True)
pages = pages.filter(live=True)
elif live_filter in ['no', 'false']:
all_pages = all_pages.filter(live=False)
pages = pages.filter(live=False)
# Search
all_pages = all_pages.search(query, order_by_relevance=not ordering)
pages = pages.search(query, order_by_relevance=not ordering)
# Facets
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()
]
else:
form = SearchForm()
paginator = Paginator(pages, per_page=20)
pages = paginator.get_page(request.GET.get('p'))
if request.is_ajax():
return TemplateResponse(request, "wagtailadmin/pages/search_results.html", {
'pages': pages,
'all_pages': all_pages,
'query_string': q,
'content_types': content_types,
'selected_content_type': selected_content_type,
'ordering': ordering,
'pagination_query_params': pagination_query_params.urlencode(),
})
else:
return TemplateResponse(request, "wagtailadmin/pages/search.html", {
'search_form': form,
'pages': pages,
'all_pages': all_pages,
'query_string': q,
'content_types': content_types,
'selected_content_type': selected_content_type,
'ordering': ordering,
'pagination_query_params': pagination_query_params.urlencode(),
})
def approve_moderation(request, revision_id):

View File

@ -0,0 +1,113 @@
from django.contrib.contenttypes.models import ContentType
from django.core.paginator import Paginator
from django.http import Http404
from django.http.request import QueryDict
from django.template.response import TemplateResponse
from django.views.decorators.vary import vary_on_headers
from wagtail.admin.auth import user_has_any_page_permission, user_passes_test
from wagtail.admin.forms.search import SearchForm
from wagtail.core.models import Page
from wagtail.search.query import MATCH_ALL
from wagtail.search.utils import parse_query_string
@vary_on_headers('X-Requested-With')
@user_passes_test(user_has_any_page_permission)
def search(request):
pages = all_pages = Page.objects.all().prefetch_related('content_type').specific()
q = MATCH_ALL
content_types = []
pagination_query_params = QueryDict({}, mutable=True)
ordering = None
if 'ordering' in request.GET:
if request.GET['ordering'] in ['title', '-title', 'latest_revision_created_at', '-latest_revision_created_at', 'live', '-live']:
ordering = request.GET['ordering']
if ordering == 'title':
pages = pages.order_by('title')
elif ordering == '-title':
pages = pages.order_by('-title')
if ordering == 'latest_revision_created_at':
pages = pages.order_by('latest_revision_created_at')
elif ordering == '-latest_revision_created_at':
pages = pages.order_by('-latest_revision_created_at')
if ordering == 'live':
pages = pages.order_by('live')
elif ordering == '-live':
pages = pages.order_by('-live')
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
# Parse query
filters, query = parse_query_string(q, operator='and', zero_terms=MATCH_ALL)
# Live filter
live_filter = filters.get('live') or filters.get('published')
live_filter = live_filter and live_filter.lower()
if live_filter in ['yes', 'true']:
all_pages = all_pages.filter(live=True)
pages = pages.filter(live=True)
elif live_filter in ['no', 'false']:
all_pages = all_pages.filter(live=False)
pages = pages.filter(live=False)
# Search
all_pages = all_pages.search(query, order_by_relevance=not ordering)
pages = pages.search(query, order_by_relevance=not ordering)
# Facets
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()
]
else:
form = SearchForm()
paginator = Paginator(pages, per_page=20)
pages = paginator.get_page(request.GET.get('p'))
if request.is_ajax():
return TemplateResponse(request, "wagtailadmin/pages/search_results.html", {
'pages': pages,
'all_pages': all_pages,
'query_string': q,
'content_types': content_types,
'selected_content_type': selected_content_type,
'ordering': ordering,
'pagination_query_params': pagination_query_params.urlencode(),
})
else:
return TemplateResponse(request, "wagtailadmin/pages/search.html", {
'search_form': form,
'pages': pages,
'all_pages': all_pages,
'query_string': q,
'content_types': content_types,
'selected_content_type': selected_content_type,
'ordering': ordering,
'pagination_query_params': pagination_query_params.urlencode(),
})