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

add another helper method get_task_type_filter_choices

This commit is contained in:
Matt Westcott 2021-09-15 21:45:45 +01:00 committed by LB (Ben Johnston)
parent 60ba39ffb5
commit 284380672d

View File

@ -503,19 +503,10 @@ def get_task_chosen_response(request, task):
class BaseTaskChooserView(View): class BaseTaskChooserView(View):
def dispatch(self, request): def dispatch(self, request):
self.task_models = get_task_types() self.task_models = get_task_types()
self.can_create = ( self.can_create = (
task_permission_policy.user_has_permission(request.user, 'add') task_permission_policy.user_has_permission(request.user, 'add')
and len(self.task_models) != 0 and len(self.task_models) != 0
) )
# Build task type choices for filter on "existing task" tab
self.task_type_choices = [
(model, model.get_verbose_name())
for model in self.task_models
]
self.task_type_choices.sort(key=lambda task_type: task_type[1].lower())
return super().dispatch(request) return super().dispatch(request)
def get_create_model(self): def get_create_model(self):
@ -558,6 +549,17 @@ class BaseTaskChooserView(View):
return task_types return task_types
def get_task_type_filter_choices(self):
"""
To be called after dispatch(); returns the list of task type choices for filter on "existing task" tab
"""
task_type_choices = [
(model, model.get_verbose_name())
for model in self.task_models
]
task_type_choices.sort(key=lambda task_type: task_type[1].lower())
return task_type_choices
class TaskChooserView(BaseTaskChooserView): class TaskChooserView(BaseTaskChooserView):
def get(self, request): def get(self, request):
@ -583,7 +585,7 @@ class TaskChooserView(BaseTaskChooserView):
return self.render_to_response() return self.render_to_response()
def render_to_response(self): def render_to_response(self):
searchform = TaskChooserSearchForm(task_type_choices=self.task_type_choices) searchform = TaskChooserSearchForm(task_type_choices=self.get_task_type_filter_choices())
tasks = searchform.task_model.objects.filter(active=True).order_by(Lower('name')) tasks = searchform.task_model.objects.filter(active=True).order_by(Lower('name'))
paginator = Paginator(tasks, per_page=10) paginator = Paginator(tasks, per_page=10)
@ -614,7 +616,7 @@ class TaskChooserView(BaseTaskChooserView):
class TaskChooserResultsView(BaseTaskChooserView): class TaskChooserResultsView(BaseTaskChooserView):
def get(self, request): def get(self, request):
searchform = TaskChooserSearchForm(request.GET, task_type_choices=self.task_type_choices) searchform = TaskChooserSearchForm(request.GET, task_type_choices=self.get_task_type_filter_choices())
tasks = all_tasks = searchform.task_model.objects.filter(active=True).order_by(Lower('name')) tasks = all_tasks = searchform.task_model.objects.filter(active=True).order_by(Lower('name'))
q = '' q = ''