kopia lustrzana https://github.com/wagtail/wagtail
Split search/pagination actions into their own view
rather than switching behaviour based on the presence of certain URL parameterspull/7867/head
rodzic
889ce5945a
commit
b3c765f14d
|
@ -20,7 +20,7 @@
|
|||
</section>
|
||||
{% endif %}
|
||||
<section id="existing" class="nice-padding{% if not can_create %} active{% endif %}">
|
||||
<form class="task-search search-bar" action="{% url 'wagtailadmin_workflows:task_chooser' %}" method="GET" novalidate>
|
||||
<form class="task-search search-bar" action="{% url 'wagtailadmin_workflows:task_chooser_results' %}" method="GET" novalidate>
|
||||
<ul class="fields">
|
||||
{% for field in searchform %}
|
||||
{% include "wagtailadmin/shared/field_as_li.html" with field=field %}
|
||||
|
|
|
@ -1611,7 +1611,7 @@ class TestTaskChooserView(TestCase, WagtailTestUtils):
|
|||
self.assertEqual([task.name for task in response.context['tasks'].object_list], ['Enabled foo', 'Moderators approval'])
|
||||
|
||||
def test_search(self):
|
||||
response = self.client.get(reverse('wagtailadmin_workflows:task_chooser') + '?q=foo')
|
||||
response = self.client.get(reverse('wagtailadmin_workflows:task_chooser_results') + '?q=foo')
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
@ -1623,7 +1623,7 @@ class TestTaskChooserView(TestCase, WagtailTestUtils):
|
|||
self.assertEqual([task.name for task in response.context['tasks'].object_list], ['Enabled foo'])
|
||||
|
||||
def test_pagination(self):
|
||||
response = self.client.get(reverse('wagtailadmin_workflows:task_chooser') + '?p=2')
|
||||
response = self.client.get(reverse('wagtailadmin_workflows:task_chooser_results') + '?p=2')
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
|
|
@ -20,5 +20,6 @@ urlpatterns = [
|
|||
path('tasks/disable/<int:pk>/', workflows.DisableTask.as_view(), name='disable_task'),
|
||||
path('tasks/enable/<int:pk>/', workflows.enable_task, name='enable_task'),
|
||||
path('task_chooser/', workflows.TaskChooserView.as_view(), name='task_chooser'),
|
||||
path('task_chooser/results/', workflows.TaskChooserResultsView.as_view(), name='task_chooser_results'),
|
||||
path('task_chooser/<int:task_id>/', workflows.task_chosen, name='task_chosen'),
|
||||
]
|
||||
|
|
|
@ -507,7 +507,7 @@ def get_task_result_data(task):
|
|||
}
|
||||
|
||||
|
||||
class TaskChooserView(View):
|
||||
class BaseTaskChooserView(View):
|
||||
def dispatch(self, request):
|
||||
task_models = get_task_types()
|
||||
self.create_model = None
|
||||
|
@ -547,40 +547,15 @@ class TaskChooserView(View):
|
|||
|
||||
return super().dispatch(request)
|
||||
|
||||
|
||||
class TaskChooserView(BaseTaskChooserView):
|
||||
def get(self, request):
|
||||
if 'q' in request.GET or 'p' in request.GET or 'task_type' in request.GET:
|
||||
searchform = TaskChooserSearchForm(request.GET, task_type_choices=self.task_type_choices)
|
||||
tasks = all_tasks = searchform.task_model.objects.filter(active=True).order_by(Lower('name'))
|
||||
q = ''
|
||||
|
||||
if searchform.is_searching():
|
||||
# Note: I decided not to use wagtailsearch here. This is because
|
||||
# wagtailsearch creates a new index for each model you make
|
||||
# searchable and this might affect someone's quota. I doubt there
|
||||
# would ever be enough tasks to require using anything more than
|
||||
# an icontains anyway.
|
||||
q = searchform.cleaned_data['q']
|
||||
tasks = tasks.filter(name__icontains=q)
|
||||
|
||||
# Pagination
|
||||
paginator = Paginator(tasks, per_page=10)
|
||||
tasks = paginator.get_page(request.GET.get('p'))
|
||||
|
||||
return TemplateResponse(request, "wagtailadmin/workflows/task_chooser/includes/results.html", {
|
||||
'task_types': self.task_types,
|
||||
'searchform': searchform,
|
||||
'tasks': tasks,
|
||||
'all_tasks': all_tasks,
|
||||
'query_string': q,
|
||||
'can_create': self.can_create,
|
||||
})
|
||||
if self.createform_class:
|
||||
self.createform = self.createform_class(prefix='create-task')
|
||||
else:
|
||||
if self.createform_class:
|
||||
self.createform = self.createform_class(prefix='create-task')
|
||||
else:
|
||||
self.createform = None
|
||||
self.createform = None
|
||||
|
||||
return self.render_to_response()
|
||||
return self.render_to_response()
|
||||
|
||||
def post(self, request):
|
||||
if not self.createform_class:
|
||||
|
@ -617,6 +592,35 @@ class TaskChooserView(View):
|
|||
}, json_data=get_chooser_context())
|
||||
|
||||
|
||||
class TaskChooserResultsView(BaseTaskChooserView):
|
||||
def get(self, request):
|
||||
searchform = TaskChooserSearchForm(request.GET, task_type_choices=self.task_type_choices)
|
||||
tasks = all_tasks = searchform.task_model.objects.filter(active=True).order_by(Lower('name'))
|
||||
q = ''
|
||||
|
||||
if searchform.is_searching():
|
||||
# Note: I decided not to use wagtailsearch here. This is because
|
||||
# wagtailsearch creates a new index for each model you make
|
||||
# searchable and this might affect someone's quota. I doubt there
|
||||
# would ever be enough tasks to require using anything more than
|
||||
# an icontains anyway.
|
||||
q = searchform.cleaned_data['q']
|
||||
tasks = tasks.filter(name__icontains=q)
|
||||
|
||||
# Pagination
|
||||
paginator = Paginator(tasks, per_page=10)
|
||||
tasks = paginator.get_page(request.GET.get('p'))
|
||||
|
||||
return TemplateResponse(request, "wagtailadmin/workflows/task_chooser/includes/results.html", {
|
||||
'task_types': self.task_types,
|
||||
'searchform': searchform,
|
||||
'tasks': tasks,
|
||||
'all_tasks': all_tasks,
|
||||
'query_string': q,
|
||||
'can_create': self.can_create,
|
||||
})
|
||||
|
||||
|
||||
def task_chosen(request, task_id):
|
||||
task = get_object_or_404(Task, id=task_id)
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue