Update OCR app
Normalize API base names. Update ViewSet base class, model classes are not needed the OCR API views. Split API tests into content and submit tests. Puntuate view test strings. Make use of success and title strings. Make use of external object mixin in document type settings view. Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -7,7 +7,7 @@ from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from mayan.apps.documents.models import Document, DocumentVersion
|
||||
from mayan.apps.rest_api.viewsets import MayanAPIViewSet
|
||||
from mayan.apps.rest_api.viewsets import MayanAPIViewSet, MayanAPIGenericViewSet
|
||||
|
||||
from .permissions import permission_ocr_content_view, permission_ocr_document
|
||||
from .serializers import (
|
||||
@@ -26,7 +26,7 @@ class DocumentOCRAPIViewSet(MayanAPIViewSet):
|
||||
serializer_class = DocumentOCRSerializer
|
||||
|
||||
@action(
|
||||
detail=True, url_name='ocr-content', url_path='ocr'
|
||||
detail=True, url_name='content', url_path='ocr'
|
||||
)
|
||||
def ocr_content(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
@@ -37,7 +37,7 @@ class DocumentOCRAPIViewSet(MayanAPIViewSet):
|
||||
)
|
||||
|
||||
@action(
|
||||
detail=True, methods=('post',), url_name='ocr-submit',
|
||||
detail=True, methods=('post',), url_name='submit',
|
||||
url_path='ocr/submit'
|
||||
)
|
||||
def ocr_submit(self, request, *args, **kwargs):
|
||||
@@ -48,39 +48,7 @@ class DocumentOCRAPIViewSet(MayanAPIViewSet):
|
||||
)
|
||||
|
||||
|
||||
class DocumentVersionOCRAPIViewSet(MayanAPIViewSet):
|
||||
lookup_url_kwarg = 'document_version_id'
|
||||
object_permission_map = {
|
||||
'ocr_content': permission_ocr_content_view,
|
||||
'ocr_submit': permission_ocr_document,
|
||||
}
|
||||
queryset = DocumentVersion.objects.all()
|
||||
serializer_class = DocumentVersionOCRSerializer
|
||||
|
||||
@action(
|
||||
detail=True, url_name='ocr-content', url_path='ocr'
|
||||
)
|
||||
def ocr_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='ocr-submit',
|
||||
url_path='ocr/submit'
|
||||
)
|
||||
def ocr_submit(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
instance.submit_for_ocr(_user=request.user)
|
||||
return Response(
|
||||
data=None, status=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
|
||||
|
||||
class DocumentPageOCRAPIViewSet(MayanAPIViewSet):
|
||||
class DocumentPageOCRAPIViewSet(MayanAPIGenericViewSet):
|
||||
lookup_url_kwarg = 'document_page_id'
|
||||
object_permission_map = {
|
||||
'ocr_content': permission_ocr_content_view,
|
||||
@@ -103,3 +71,35 @@ class DocumentPageOCRAPIViewSet(MayanAPIViewSet):
|
||||
return Response(
|
||||
serializer.data, status=status.HTTP_200_OK, headers=headers
|
||||
)
|
||||
|
||||
|
||||
class DocumentVersionOCRAPIViewSet(MayanAPIViewSet):
|
||||
lookup_url_kwarg = 'document_version_id'
|
||||
object_permission_map = {
|
||||
'ocr_content': permission_ocr_content_view,
|
||||
'ocr_submit': permission_ocr_document,
|
||||
}
|
||||
queryset = DocumentVersion.objects.all()
|
||||
serializer_class = DocumentVersionOCRSerializer
|
||||
|
||||
@action(
|
||||
detail=True, url_name='content', url_path='ocr'
|
||||
)
|
||||
def ocr_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='ocr/submit'
|
||||
)
|
||||
def ocr_submit(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
instance.submit_for_ocr(_user=request.user)
|
||||
return Response(
|
||||
data=None, status=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
|
||||
@@ -43,7 +43,8 @@ def method_document_page_get_ocr_content(self):
|
||||
page_content = self.ocr_content.content
|
||||
except DocumentPageOCRContent.DoesNotExist:
|
||||
return ''
|
||||
return page_content
|
||||
else:
|
||||
return page_content
|
||||
|
||||
|
||||
def method_document_version_get_ocr_content(self):
|
||||
|
||||
@@ -12,7 +12,7 @@ from ..permissions import (
|
||||
from .literals import TEST_DOCUMENT_CONTENT
|
||||
|
||||
|
||||
class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
class OCRSubmitAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
def _request_document_ocr_submit_view(self):
|
||||
return self.post(
|
||||
viewname='rest_api:document-ocr-submit',
|
||||
@@ -28,7 +28,7 @@ class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
#TODO: mock OCR here
|
||||
def test_submit_document_with_access(self):
|
||||
self.grant_access(
|
||||
permission=permission_ocr_document, obj=self.document
|
||||
obj=self.document, permission=permission_ocr_document
|
||||
)
|
||||
response = self._request_document_ocr_submit_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
||||
@@ -52,13 +52,15 @@ class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
|
||||
def test_submit_document_version_with_access(self):
|
||||
self.grant_access(
|
||||
permission=permission_ocr_document, obj=self.document
|
||||
obj=self.document, permission=permission_ocr_document
|
||||
)
|
||||
response = self._request_document_version_ocr_submit_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
||||
|
||||
self.assertTrue(hasattr(self.document.pages.first(), 'ocr_content'))
|
||||
|
||||
|
||||
class OCRContentAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
def _request_document_content_view(self):
|
||||
return self.get(
|
||||
viewname='rest_api:document-ocr-content',
|
||||
@@ -68,13 +70,14 @@ class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
)
|
||||
|
||||
def test_get_document_content_no_permission(self):
|
||||
self.document.submit_for_ocr()
|
||||
response = self._request_document_content_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
def test_get_document_content_with_access(self):
|
||||
self.document.submit_for_ocr()
|
||||
self.grant_access(
|
||||
permission=permission_ocr_content_view, obj=self.document
|
||||
obj=self.document, permission=permission_ocr_content_view
|
||||
)
|
||||
|
||||
response = self._request_document_content_view()
|
||||
@@ -95,14 +98,15 @@ class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
}
|
||||
)
|
||||
|
||||
def test_get_document_version_page_content_no_permission(self):
|
||||
def test_get_document_page_content_no_permission(self):
|
||||
self.document.submit_for_ocr()
|
||||
response = self._request_document_page_content_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
def test_get_document_version_page_content_with_access(self):
|
||||
def test_get_document_page_content_with_access(self):
|
||||
self.document.submit_for_ocr()
|
||||
self.grant_access(
|
||||
permission=permission_ocr_content_view, obj=self.document
|
||||
obj=self.document, permission=permission_ocr_content_view
|
||||
)
|
||||
|
||||
response = self._request_document_page_content_view()
|
||||
@@ -122,14 +126,15 @@ class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
}
|
||||
)
|
||||
|
||||
def test_get_document_version_version_content_no_permission(self):
|
||||
def test_get_document_version_content_no_permission(self):
|
||||
self.document.submit_for_ocr()
|
||||
response = self._request_document_version_content_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
def test_get_document_version_version_content_with_access(self):
|
||||
def test_get_document_version_content_with_access(self):
|
||||
self.document.submit_for_ocr()
|
||||
self.grant_access(
|
||||
permission=permission_ocr_content_view, obj=self.document
|
||||
obj=self.document, permission=permission_ocr_content_view
|
||||
)
|
||||
|
||||
response = self._request_document_version_content_view()
|
||||
|
||||
@@ -79,7 +79,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase):
|
||||
|
||||
def test_document_submit_view_with_access(self):
|
||||
self.grant_access(
|
||||
permission=permission_ocr_document, obj=self.document
|
||||
obj=self.document, permission=permission_ocr_document
|
||||
)
|
||||
self._request_document_submit_view()
|
||||
self.assertTrue(
|
||||
|
||||
@@ -58,11 +58,11 @@ urlpatterns = [
|
||||
api_router_entries = (
|
||||
{
|
||||
'prefix': r'documents',
|
||||
'viewset': DocumentOCRAPIViewSet, 'basename': 'document'
|
||||
'viewset': DocumentOCRAPIViewSet, 'basename': 'document-ocr'
|
||||
},
|
||||
{
|
||||
'prefix': r'documents/(?P<document_id>\d+)/document_versions',
|
||||
'viewset': DocumentVersionOCRAPIViewSet, 'basename': 'document_version'
|
||||
'viewset': DocumentVersionOCRAPIViewSet, 'basename': 'document_version-ocr'
|
||||
},
|
||||
{
|
||||
'prefix': r'documents/(?P<document_id>\d+)/document_versions/(?P<document_version_id>\d+)/document_pages',
|
||||
|
||||
@@ -4,13 +4,13 @@ from django.contrib import messages
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import ugettext_lazy as _, ungettext
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mayan.apps.acls.models import AccessControlList
|
||||
from mayan.apps.common.generics import (
|
||||
FormView, MultipleObjectConfirmActionView, SingleObjectDetailView,
|
||||
SingleObjectDownloadView, SingleObjectEditView, SingleObjectListView
|
||||
)
|
||||
from mayan.apps.common.mixins import ExternalObjectMixin
|
||||
from mayan.apps.documents.forms import DocumentTypeFilteredSelectForm
|
||||
from mayan.apps.documents.mixins import RecentDocumentMixin
|
||||
from mayan.apps.documents.models import Document, DocumentPage, DocumentType
|
||||
@@ -35,7 +35,7 @@ class DocumentOCRContentView(RecentDocumentMixin, SingleObjectDetailView):
|
||||
'document': self.get_object(),
|
||||
'hide_labels': True,
|
||||
'object': self.get_object(),
|
||||
'title': _('OCR result for document: %s') % self.get_object(),
|
||||
'title': _('OCR result for document: %s.') % self.get_object(),
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ class DocumentOCRErrorsListView(SingleObjectListView):
|
||||
return {
|
||||
'hide_object': True,
|
||||
'object': self.get_document(),
|
||||
'title': _('OCR errors for document: %s') % self.get_document(),
|
||||
'title': _('OCR errors for document: %s.') % self.get_document(),
|
||||
}
|
||||
|
||||
def get_object_list(self):
|
||||
@@ -82,7 +82,7 @@ class DocumentPageOCRContentView(RecentDocumentMixin, SingleObjectDetailView):
|
||||
return {
|
||||
'hide_labels': True,
|
||||
'object': self.object,
|
||||
'title': _('OCR result for document page: %s') % self.object,
|
||||
'title': _('OCR result for document page: %s.') % self.object,
|
||||
}
|
||||
|
||||
def get_recent_document(self):
|
||||
@@ -93,49 +93,34 @@ class DocumentSubmitView(MultipleObjectConfirmActionView):
|
||||
model = Document
|
||||
object_permission = permission_ocr_document
|
||||
pk_url_kwarg = 'document_id'
|
||||
success_message_singular = '%(count)d document submitted to the OCR queue.'
|
||||
success_message_plural = '%(count)d documents submitted to the OCR queue.'
|
||||
|
||||
def get_extra_context(self):
|
||||
queryset = self.get_queryset()
|
||||
|
||||
result = {
|
||||
'title': ungettext(
|
||||
singular='Submit the selected document to the OCR queue?',
|
||||
plural='Submit the selected documents to the OCR queue?',
|
||||
number=queryset.count()
|
||||
)
|
||||
}
|
||||
|
||||
return result
|
||||
success_message_single = _('Document "%(object)s" added to the OCR queue.')
|
||||
success_message_singular = _('%(count)d document submitted to the OCR queue.')
|
||||
success_message_plural = _('%(count)d documents submitted to the OCR queue.')
|
||||
title_single = _('Submit the document "%(object)s" to the OCR queue.')
|
||||
title_singular = _('Submit the selected document to the OCR queue.')
|
||||
title_plural = _('Submit the selected documents to the OCR queue.')
|
||||
|
||||
def object_action(self, form, instance):
|
||||
instance.submit_for_ocr()
|
||||
|
||||
|
||||
class DocumentTypeSettingsEditView(SingleObjectEditView):
|
||||
class DocumentTypeSettingsEditView(ExternalObjectMixin, SingleObjectEditView):
|
||||
external_object_class = DocumentType
|
||||
external_object_permission = permission_document_type_ocr_setup
|
||||
external_object_pk_url_kwarg = 'document_type_id'
|
||||
fields = ('auto_ocr',)
|
||||
object_permission = permission_document_type_ocr_setup
|
||||
post_action_redirect = reverse_lazy(
|
||||
viewname='documents:document_type_list'
|
||||
)
|
||||
|
||||
def get_document_type(self):
|
||||
queryset = AccessControlList.objects.restrict_queryset(
|
||||
permission=permission_document_type_ocr_setup,
|
||||
queryset=DocumentType.objects.all(),
|
||||
user=self.request.user
|
||||
)
|
||||
|
||||
return get_object_or_404(
|
||||
klass=queryset, pk=self.kwargs['document_type_id']
|
||||
)
|
||||
return self.external_object
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'object': self.get_document_type(),
|
||||
'title': _(
|
||||
'Edit OCR settings for document type: %s'
|
||||
'Edit OCR settings for document type: %s.'
|
||||
) % self.get_document_type()
|
||||
}
|
||||
|
||||
@@ -145,7 +130,7 @@ class DocumentTypeSettingsEditView(SingleObjectEditView):
|
||||
|
||||
class DocumentTypeSubmitView(FormView):
|
||||
extra_context = {
|
||||
'title': _('Submit all documents of a type for OCR')
|
||||
'title': _('Submit all documents of a type for OCR.')
|
||||
}
|
||||
form_class = DocumentTypeFilteredSelectForm
|
||||
post_action_redirect = reverse_lazy(viewname='common:tools_list')
|
||||
@@ -178,7 +163,7 @@ class DocumentTypeSubmitView(FormView):
|
||||
class EntryListView(SingleObjectListView):
|
||||
extra_context = {
|
||||
'hide_object': True,
|
||||
'title': _('OCR errors'),
|
||||
'title': _('OCR errors.'),
|
||||
}
|
||||
view_permission = permission_document_type_ocr_setup
|
||||
|
||||
|
||||
Reference in New Issue
Block a user