From 8b20015f645f291437209a8efdff9846dd5f7e86 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 15 Feb 2017 20:20:14 -0400 Subject: [PATCH] Add per document type, workflow list API view. GitLab issue #357, GitLab merge request #!9. cc @jeverling --- mayan/apps/document_states/api_views.py | 29 +++++++++++++++++++- mayan/apps/document_states/tests/test_api.py | 13 +++++++++ mayan/apps/document_states/urls.py | 13 ++++++--- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/mayan/apps/document_states/api_views.py b/mayan/apps/document_states/api_views.py index 2c3735e159..08a1e55d43 100644 --- a/mayan/apps/document_states/api_views.py +++ b/mayan/apps/document_states/api_views.py @@ -6,7 +6,7 @@ from django.shortcuts import get_object_or_404 from rest_framework import generics from acls.models import AccessControlList -from documents.models import Document +from documents.models import Document, DocumentType from documents.permissions import permission_document_type_view from permissions import Permission from rest_api.filters import MayanObjectPermissionsFilter @@ -27,6 +27,33 @@ from .serializers import ( ) +class APIDocumentTypeWorkflowListView(generics.ListAPIView): + serializer_class = WorkflowSerializer + + def get(self, *args, **kwargs): + """ + Returns a list of all the document type workflows. + """ + return super(APIDocumentTypeWorkflowListView, self).get(*args, **kwargs) + + def get_document_type(self): + document_type = get_object_or_404(DocumentType, pk=self.kwargs['pk']) + + try: + Permission.check_permissions( + self.request.user, (permission_workflow_view,) + ) + except PermissionDenied: + AccessControlList.objects.check_access( + permission_workflow_view, self.request.user, document_type + ) + + return document_type + + def get_queryset(self): + return self.get_document_type().workflows.all() + + class APIWorkflowDocumentTypeList(generics.ListCreateAPIView): filter_backends = (MayanObjectPermissionsFilter,) mayan_object_permissions = { diff --git a/mayan/apps/document_states/tests/test_api.py b/mayan/apps/document_states/tests/test_api.py index 6545450ee7..9ee947abe2 100644 --- a/mayan/apps/document_states/tests/test_api.py +++ b/mayan/apps/document_states/tests/test_api.py @@ -186,6 +186,19 @@ class WorkflowAPITestCase(APITestCase): workflow.refresh_from_db() self.assertEqual(workflow.label, TEST_WORKFLOW_LABEL_EDITED) + def test_document_type_workflow_list(self): + workflow = self._create_workflow() + workflow.document_types.add(self.document_type) + + response = self.client.get( + reverse( + 'rest_api:documenttype-workflow-list', + args=(self.document_type.pk,) + ), + ) + + self.assertEqual(response.data['results'][0]['label'], workflow.label) + @override_settings(OCR_AUTO_OCR=False) class WorkflowStatesAPITestCase(APITestCase): diff --git a/mayan/apps/document_states/urls.py b/mayan/apps/document_states/urls.py index d612b28324..2194d26285 100644 --- a/mayan/apps/document_states/urls.py +++ b/mayan/apps/document_states/urls.py @@ -3,10 +3,10 @@ from __future__ import unicode_literals from django.conf.urls import patterns, url from .api_views import ( - APIWorkflowDocumentTypeList, APIWorkflowDocumentTypeView, - APIWorkflowInstanceListView, APIWorkflowInstanceView, - APIWorkflowInstanceLogEntryListView, APIWorkflowListView, - APIWorkflowStateListView, APIWorkflowStateView, + APIDocumentTypeWorkflowListView, APIWorkflowDocumentTypeList, + APIWorkflowDocumentTypeView, APIWorkflowInstanceListView, + APIWorkflowInstanceView, APIWorkflowInstanceLogEntryListView, + APIWorkflowListView, APIWorkflowStateListView, APIWorkflowStateView, APIWorkflowTransitionListView, APIWorkflowTransitionView, APIWorkflowView ) from .views import ( @@ -150,4 +150,9 @@ api_urls = [ APIWorkflowInstanceLogEntryListView.as_view(), name='workflowinstancelogentry-list' ), + url( + r'^document_type/(?P[0-9]+)/workflows/$', + APIDocumentTypeWorkflowListView.as_view(), + name='documenttype-workflow-list' + ), ]