Refactor the parsing app API
Add additional API and view tests. Add success and multi document titles strings. Make use of external mixin in the document type submit view. Puntuate all view text strings. Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -2,47 +2,104 @@ from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import generics
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from mayan.apps.documents.models import Document
|
||||
from mayan.apps.rest_api.permissions import MayanPermission
|
||||
from mayan.apps.documents.models import Document, DocumentVersion
|
||||
from mayan.apps.rest_api.viewsets import MayanAPIViewSet, MayanAPIGenericViewSet
|
||||
|
||||
from .models import DocumentPageContent
|
||||
from .permissions import permission_content_view
|
||||
from .serializers import DocumentPageContentSerializer
|
||||
from .permissions import permission_content_view, permission_parse_document
|
||||
from .serializers import (
|
||||
DocumentParsingSerializer, DocumentPageParsingSerializer,
|
||||
DocumentVersionParsingSerializer
|
||||
)
|
||||
|
||||
|
||||
class APIDocumentPageContentView(generics.RetrieveAPIView):
|
||||
"""
|
||||
Returns the content of the selected document page.
|
||||
"""
|
||||
lookup_url_kwarg = 'document_page_id'
|
||||
mayan_object_permissions = {
|
||||
'GET': (permission_content_view,),
|
||||
class DocumentParsingAPIViewSet(MayanAPIGenericViewSet):
|
||||
lookup_url_kwarg = 'document_id'
|
||||
object_permission_map = {
|
||||
'parsing_content': permission_content_view,
|
||||
'parsing_submit': permission_parse_document,
|
||||
}
|
||||
permission_classes = (MayanPermission,)
|
||||
serializer_class = DocumentPageContentSerializer
|
||||
queryset = Document.objects.all()
|
||||
serializer_class = DocumentParsingSerializer
|
||||
|
||||
def get_document(self):
|
||||
return get_object_or_404(klass=Document, pk=self.kwargs['document_id'])
|
||||
|
||||
def get_document_version(self):
|
||||
return get_object_or_404(
|
||||
klass=self.get_document().versions.all(),
|
||||
pk=self.kwargs['document_version_id']
|
||||
@action(
|
||||
detail=True, url_name='content', url_path='parsing'
|
||||
)
|
||||
def parsing_content(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
serializer = self.get_serializer(instance)
|
||||
headers = self.get_success_headers(data=serializer.data)
|
||||
return Response(
|
||||
serializer.data, status=status.HTTP_200_OK, headers=headers
|
||||
)
|
||||
|
||||
def get_queryset(self):
|
||||
return self.get_document_version().pages.all()
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
@action(
|
||||
detail=True, methods=('post',), url_name='submit',
|
||||
url_path='parsing/submit'
|
||||
)
|
||||
def parsing_submit(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
instance.submit_for_parsing(_user=request.user)
|
||||
return Response(
|
||||
data=None, status=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
|
||||
try:
|
||||
content = instance.content
|
||||
except DocumentPageContent.DoesNotExist:
|
||||
content = DocumentPageContent.objects.none()
|
||||
|
||||
serializer = self.get_serializer(content)
|
||||
return Response(serializer.data)
|
||||
class DocumentVersionParsingAPIViewSet(MayanAPIViewSet):
|
||||
lookup_url_kwarg = 'document_version_id'
|
||||
object_permission_map = {
|
||||
'parsing_content': permission_content_view,
|
||||
'parsing_submit': permission_parse_document,
|
||||
}
|
||||
queryset = DocumentVersion.objects.all()
|
||||
serializer_class = DocumentVersionParsingSerializer
|
||||
|
||||
@action(
|
||||
detail=True, url_name='content', url_path='parsing'
|
||||
)
|
||||
def parsing_content(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
serializer = self.get_serializer(instance)
|
||||
headers = self.get_success_headers(data=serializer.data)
|
||||
return Response(
|
||||
serializer.data, status=status.HTTP_200_OK, headers=headers
|
||||
)
|
||||
|
||||
@action(
|
||||
detail=True, methods=('post',), url_name='submit',
|
||||
url_path='parsing/submit'
|
||||
)
|
||||
def parsing_submit(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
instance.submit_for_parsing(_user=request.user)
|
||||
return Response(
|
||||
data=None, status=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
|
||||
|
||||
class DocumentPageParsingAPIViewSet(MayanAPIGenericViewSet):
|
||||
lookup_url_kwarg = 'document_page_id'
|
||||
object_permission_map = {
|
||||
'parsing_content': permission_content_view,
|
||||
}
|
||||
serializer_class = DocumentPageParsingSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return get_object_or_404(
|
||||
klass=DocumentVersion, document_id=self.kwargs['document_id'],
|
||||
pk=self.kwargs['document_version_id']
|
||||
).pages.all()
|
||||
|
||||
@action(
|
||||
detail=True, url_name='content', url_path='parsing'
|
||||
)
|
||||
def parsing_content(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
serializer = self.get_serializer(instance)
|
||||
headers = self.get_success_headers(data=serializer.data)
|
||||
return Response(
|
||||
serializer.data, status=status.HTTP_200_OK, headers=headers
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user