0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Add a separate URL endpoint for document search results include

Eliminates use of request.is_ajax
This commit is contained in:
Matt Westcott 2021-08-18 18:40:30 +01:00 committed by LB (Ben Johnston)
parent 9fd2ad03d3
commit fe3b9446e9
3 changed files with 35 additions and 38 deletions

View File

@ -6,6 +6,7 @@ from wagtail.documents.views import chooser, documents, multiple
app_name = 'wagtaildocs'
urlpatterns = [
path('', documents.IndexView.as_view(), name='index'),
path('results/', documents.ListingResultsView.as_view(), name='listing_results'),
path('add/', documents.add, name='add'),
path('edit/<int:document_id>/', documents.edit, name='edit'),
path('delete/<int:document_id>/', documents.delete, name='delete'),

View File

@ -5,7 +5,7 @@
{{ block.super }}
<script>
window.headerSearch = {
url: "{% url 'wagtaildocs:index' %}",
url: "{% url 'wagtaildocs:listing_results' %}",
termInput: "#id_q",
targetOutput: "#document-results"
}

View File

@ -7,7 +7,6 @@ from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _
from django.views.decorators.vary import vary_on_headers
from django.views.generic import TemplateView
from wagtail.admin import messages
@ -24,9 +23,8 @@ from wagtail.search import index as search_index
permission_checker = PermissionPolicyChecker(permission_policy)
class IndexView(TemplateView):
class BaseListingView(TemplateView):
@method_decorator(permission_checker.require_any('add', 'change', 'delete'))
@method_decorator(vary_on_headers('X-Requested-With'))
def get(self, request):
return super().get(request)
@ -46,64 +44,62 @@ class IndexView(TemplateView):
documents = documents.order_by(ordering)
# Filter by collection
current_collection = None
self.current_collection = None
collection_id = self.request.GET.get('collection_id')
if collection_id:
try:
current_collection = Collection.objects.get(id=collection_id)
documents = documents.filter(collection=current_collection)
self.current_collection = Collection.objects.get(id=collection_id)
documents = documents.filter(collection=self.current_collection)
except (ValueError, Collection.DoesNotExist):
pass
# Search
query_string = None
if 'q' in self.request.GET:
form = SearchForm(self.request.GET, placeholder=_("Search documents"))
if form.is_valid():
query_string = form.cleaned_data['q']
self.form = SearchForm(self.request.GET, placeholder=_("Search documents"))
if self.form.is_valid():
query_string = self.form.cleaned_data['q']
documents = documents.search(query_string)
else:
form = SearchForm(placeholder=_("Search documents"))
self.form = SearchForm(placeholder=_("Search documents"))
# Pagination
paginator = Paginator(documents, per_page=20)
documents = paginator.get_page(self.request.GET.get('p'))
context.update({
'ordering': ordering,
'documents': documents,
'query_string': query_string,
'is_searching': bool(query_string),
})
return context
class IndexView(BaseListingView):
template_name = 'wagtaildocs/documents/index.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
collections = permission_policy.collections_user_has_any_permission_for(
self.request.user, ['add', 'change']
)
if len(collections) < 2:
collections = None
# Create response
if self.request.is_ajax():
context.update({
'ordering': ordering,
'documents': documents,
'query_string': query_string,
'is_searching': bool(query_string),
})
else:
context.update({
'ordering': ordering,
'documents': documents,
'query_string': query_string,
'is_searching': bool(query_string),
'search_form': form,
'popular_tags': popular_tags_for_model(get_document_model()),
'user_can_add': permission_policy.user_has_permission(self.request.user, 'add'),
'collections': collections,
'current_collection': current_collection,
})
context.update({
'search_form': self.form,
'popular_tags': popular_tags_for_model(get_document_model()),
'user_can_add': permission_policy.user_has_permission(self.request.user, 'add'),
'collections': collections,
'current_collection': self.current_collection,
})
return context
def get_template_names(self):
if self.request.is_ajax():
return ['wagtaildocs/documents/results.html']
else:
return ['wagtaildocs/documents/index.html']
class ListingResultsView(BaseListingView):
template_name = 'wagtaildocs/documents/results.html'
@permission_checker.require('add')