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

Workflow reviewable filter (#6139)

* Pass request into report filtersets

* Add reviewable filter to workflow and tasks reports
This commit is contained in:
jacobtoppm 2020-06-12 11:43:18 +01:00 committed by Matt Westcott
parent 5be2d6df56
commit f684bc7776
5 changed files with 71 additions and 9 deletions

View File

@ -111,10 +111,24 @@ class LockedPagesReportFilterSet(WagtailFilterSet):
class WorkflowReportFilterSet(WagtailFilterSet):
created_at = django_filters.DateFromToRangeFilter(label=_("Started at"), widget=DateRangePickerWidget)
reviewable = django_filters.ChoiceFilter(
label=_("Show"),
method='filter_reviewable',
choices=(
('true', _("Awaiting my review")),
),
empty_label=_("All"),
widget=ButtonSelect
)
def filter_reviewable(self, queryset, name, value):
if value and self.request and self.request.user:
queryset = queryset.filter(current_task_state__in=TaskState.objects.reviewable_by(self.request.user))
return queryset
class Meta:
model = WorkflowState
fields = ['workflow', 'status', 'created_at']
fields = ['reviewable', 'workflow', 'status', 'created_at']
class WorkflowTasksReportFilterSet(WagtailFilterSet):
@ -130,6 +144,21 @@ class WorkflowTasksReportFilterSet(WagtailFilterSet):
queryset=Task.objects.all(), filter_field='id_workflow', filter_accessor='get_workflows'
)
reviewable = django_filters.ChoiceFilter(
label=_("Show"),
method='filter_reviewable',
choices=(
('true', _("Awaiting my review")),
),
empty_label=_("All"),
widget=ButtonSelect
)
def filter_reviewable(self, queryset, name, value):
if value and self.request and self.request.user:
queryset = queryset.filter(id__in=TaskState.objects.reviewable_by(self.request.user).values_list('id', flat=True))
return queryset
class Meta:
model = TaskState
fields = ['workflow', 'task', 'status', 'started_at', 'finished_at']
fields = ['reviewable', 'workflow', 'task', 'status', 'started_at', 'finished_at']

View File

@ -724,6 +724,32 @@ class TestApproveRejectWorkflow(TestCase, WagtailTestUtils):
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Hello world!")
def test_workflow_report_filtered(self):
# the moderator can review the task, so the workflow state should show up even when reports are filtered by reviewable
response = self.client.get(reverse('wagtailadmin_reports:workflow'), {'reviewable': 'true'})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Hello world!")
self.assertContains(response, "test_workflow")
self.assertContains(response, "Sebastian Mitter")
self.assertContains(response, "March 31, 2020")
response = self.client.get(reverse('wagtailadmin_reports:workflow_tasks'), {'reviewable': 'true'})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Hello world!")
# the submitter cannot review the task, so the workflow state shouldn't show up when reports are filtered by reviewable
self.login(self.submitter)
response = self.client.get(reverse('wagtailadmin_reports:workflow'), {'reviewable': 'true'})
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "Hello world!")
self.assertNotContains(response, "Sebastian Mitter")
self.assertNotContains(response, "March 31, 2020")
response = self.client.get(reverse('wagtailadmin_reports:workflow_tasks'), {'reviewable': 'true'})
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "Hello world!")
class TestNotificationPreferences(TestCase, WagtailTestUtils):
def setUp(self):

View File

@ -11,7 +11,7 @@ from wagtail.admin.navigation import get_site_for_user
from wagtail.admin.site_summary import SiteSummaryPanel
from wagtail.core import hooks
from wagtail.core.models import (
Page, PageRevision, Task, TaskState, UserPagePermissionsProxy, WorkflowState)
Page, PageRevision, TaskState, UserPagePermissionsProxy, WorkflowState)
User = get_user_model()
@ -74,11 +74,7 @@ class WorkflowPagesToModeratePanel:
def __init__(self, request):
self.request = request
tasks = Task.objects.filter(active=True)
states = TaskState.objects.none()
for task in tasks:
states = states | task.specific.get_task_states_user_can_moderate(user=request.user).select_related('page_revision', 'task', 'page_revision__page')
states = TaskState.objects.reviewable_by(request.user).select_related('page_revision', 'task', 'page_revision__page')
self.states = [(state, state.task.specific.get_actions(page=state.page_revision.page, user=request.user)) for state in states]
def render(self):

View File

@ -207,7 +207,7 @@ class ReportView(SpreadsheetExportMixin, TemplateResponseMixin, BaseListView):
filters = None
if self.filterset_class:
filters = self.filterset_class(self.request.GET, queryset=queryset)
filters = self.filterset_class(self.request.GET, queryset=queryset, request=self.request)
queryset = filters.qs
return filters, queryset

View File

@ -2993,6 +2993,15 @@ class WorkflowState(models.Model):
]
class TaskStateManager(models.Manager):
def reviewable_by(self, user):
tasks = Task.objects.filter(active=True)
states = TaskState.objects.none()
for task in tasks:
states = states | task.specific.get_task_states_user_can_moderate(user=user)
return states
class TaskState(MultiTableCopyMixin, models.Model):
"""Tracks the status of a given Task for a particular page revision."""
STATUS_IN_PROGRESS = 'in_progress'
@ -3030,6 +3039,8 @@ class TaskState(MultiTableCopyMixin, models.Model):
)
exclude_fields_in_copy = []
objects = TaskStateManager()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not self.id: