Update single and multiple document OCR submit views to use MultipleObjectConfirmActionView instead of the deprecated MultipleInstanceActionMixin.
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -1,14 +1,11 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.test import override_settings
|
|
||||||
|
|
||||||
from documents.tests import GenericDocumentViewTestCase
|
from documents.tests import GenericDocumentViewTestCase
|
||||||
|
|
||||||
from ..permissions import permission_ocr_content_view
|
from ..permissions import permission_ocr_content_view, permission_ocr_document
|
||||||
from ..utils import get_document_ocr_content
|
from ..utils import get_document_ocr_content
|
||||||
|
|
||||||
|
|
||||||
@override_settings(OCR_AUTO_OCR=True)
|
|
||||||
class OCRViewsTestCase(GenericDocumentViewTestCase):
|
class OCRViewsTestCase(GenericDocumentViewTestCase):
|
||||||
# PyOCR's leak descriptor in get_available_languages and image_to_string
|
# PyOCR's leak descriptor in get_available_languages and image_to_string
|
||||||
# Disable descriptor leak test until fixed in upstream
|
# Disable descriptor leak test until fixed in upstream
|
||||||
@@ -18,39 +15,92 @@ class OCRViewsTestCase(GenericDocumentViewTestCase):
|
|||||||
super(OCRViewsTestCase, self).setUp()
|
super(OCRViewsTestCase, self).setUp()
|
||||||
self.login_user()
|
self.login_user()
|
||||||
|
|
||||||
def _document_content_view(self):
|
def _request_document_content_view(self):
|
||||||
return self.get(
|
return self.get(
|
||||||
'ocr:document_content', args=(self.document.pk,)
|
'ocr:document_content', args=(self.document.pk,)
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_document_content_view_no_permissions(self):
|
def test_document_content_view_no_permissions(self):
|
||||||
response = self._document_content_view()
|
self.document.submit_for_ocr()
|
||||||
|
response = self._request_document_content_view()
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 403)
|
self.assertEqual(response.status_code, 403)
|
||||||
|
|
||||||
def test_document_content_view_with_permission(self):
|
def test_document_content_view_with_access(self):
|
||||||
self.grant_permission(permission=permission_ocr_content_view)
|
self.document.submit_for_ocr()
|
||||||
|
self.grant_access(
|
||||||
|
permission=permission_ocr_content_view, obj=self.document
|
||||||
|
)
|
||||||
|
|
||||||
response = self._document_content_view()
|
response = self._request_document_content_view()
|
||||||
|
|
||||||
self.assertContains(
|
self.assertContains(
|
||||||
response, 'Mayan EDMS Documentation', status_code=200
|
response, 'Mayan EDMS Documentation', status_code=200
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_document_ocr_download_view_no_permission(self):
|
def _request_document_submit_view(self):
|
||||||
response = self.get(
|
return self.post(
|
||||||
|
'ocr:document_submit', args=(self.document.pk,)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_document_submit_view_no_permission(self):
|
||||||
|
self._request_document_submit_view()
|
||||||
|
self.assertEqual(
|
||||||
|
''.join(self.document.latest_version.ocr_content()), ''
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_document_submit_view_with_access(self):
|
||||||
|
self.grant_access(
|
||||||
|
permission=permission_ocr_document, obj=self.document
|
||||||
|
)
|
||||||
|
self._request_document_submit_view()
|
||||||
|
self.assertTrue(
|
||||||
|
'Mayan EDMS Documentation' in ''.join(
|
||||||
|
self.document.latest_version.ocr_content()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _request_multiple_document_submit_view(self):
|
||||||
|
return self.post(
|
||||||
|
'ocr:document_submit_multiple',
|
||||||
|
data={
|
||||||
|
'id_list': self.document.pk,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_multiple_document_submit_view_no_permission(self):
|
||||||
|
self._request_multiple_document_submit_view()
|
||||||
|
self.assertEqual(
|
||||||
|
''.join(self.document.latest_version.ocr_content()), ''
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_multiple_document_submit_view_with_access(self):
|
||||||
|
self.grant_access(
|
||||||
|
permission=permission_ocr_document, obj=self.document
|
||||||
|
)
|
||||||
|
self._request_multiple_document_submit_view()
|
||||||
|
self.assertTrue(
|
||||||
|
'Mayan EDMS Documentation' in ''.join(
|
||||||
|
self.document.latest_version.ocr_content()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _request_document_ocr_download_view(self):
|
||||||
|
return self.get(
|
||||||
'ocr:document_ocr_download', args=(self.document.pk,)
|
'ocr:document_ocr_download', args=(self.document.pk,)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_document_ocr_download_view_no_permission(self):
|
||||||
|
self.document.submit_for_ocr()
|
||||||
|
response = self._request_document_ocr_download_view()
|
||||||
self.assertEqual(response.status_code, 403)
|
self.assertEqual(response.status_code, 403)
|
||||||
|
|
||||||
def test_document_download_view_with_permission(self):
|
def test_document_ocr_download_view_with_permission(self):
|
||||||
|
self.document.submit_for_ocr()
|
||||||
self.expected_content_type = 'application/octet-stream; charset=utf-8'
|
self.expected_content_type = 'application/octet-stream; charset=utf-8'
|
||||||
|
|
||||||
self.grant_permission(permission=permission_ocr_content_view)
|
self.grant_permission(permission=permission_ocr_content_view)
|
||||||
response = self.get(
|
response = self._request_document_ocr_download_view()
|
||||||
'ocr:document_ocr_download', args=(self.document.pk,)
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ from .api_views import (
|
|||||||
)
|
)
|
||||||
from .views import (
|
from .views import (
|
||||||
DocumentOCRContent, DocumentOCRDownloadView, DocumentOCRErrorsListView,
|
DocumentOCRContent, DocumentOCRDownloadView, DocumentOCRErrorsListView,
|
||||||
DocumentSubmitView, DocumentSubmitManyView, DocumentTypeSettingsEditView,
|
DocumentSubmitView, DocumentTypeSettingsEditView, DocumentTypeSubmitView,
|
||||||
DocumentTypeSubmitView, EntryListView
|
EntryListView
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@@ -26,7 +26,7 @@ urlpatterns = [
|
|||||||
name='document_type_submit'
|
name='document_type_submit'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
r'^document/multiple/submit/$', DocumentSubmitManyView.as_view(),
|
r'^document/multiple/submit/$', DocumentSubmitView.as_view(),
|
||||||
name='document_submit_multiple'
|
name='document_submit_multiple'
|
||||||
),
|
),
|
||||||
url(
|
url(
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ from django.contrib import messages
|
|||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _, ungettext
|
||||||
|
|
||||||
from acls.models import AccessControlList
|
|
||||||
from common.generics import (
|
from common.generics import (
|
||||||
ConfirmView, FormView, SingleObjectDetailView, SingleObjectDownloadView,
|
FormView, MultipleObjectConfirmActionView, SingleObjectDetailView,
|
||||||
SingleObjectEditView, SingleObjectListView
|
SingleObjectDownloadView, SingleObjectEditView, SingleObjectListView
|
||||||
)
|
)
|
||||||
from common.mixins import MultipleInstanceActionMixin
|
|
||||||
from documents.models import Document, DocumentType
|
from documents.models import Document, DocumentType
|
||||||
|
|
||||||
from .forms import DocumentOCRContentForm, DocumentTypeSelectForm
|
from .forms import DocumentOCRContentForm, DocumentTypeSelectForm
|
||||||
@@ -44,48 +42,28 @@ class DocumentOCRContent(SingleObjectDetailView):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class DocumentSubmitView(ConfirmView):
|
class DocumentSubmitView(MultipleObjectConfirmActionView):
|
||||||
def get_extra_context(self):
|
|
||||||
return {
|
|
||||||
'object': self.get_object(),
|
|
||||||
'title': _('Submit "%s" to the OCR queue?') % self.get_object()
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_object(self):
|
|
||||||
return Document.objects.get(pk=self.kwargs['pk'])
|
|
||||||
|
|
||||||
def object_action(self, instance):
|
|
||||||
AccessControlList.objects.check_access(
|
|
||||||
permissions=permission_ocr_document, user=self.request.user,
|
|
||||||
obj=instance
|
|
||||||
)
|
|
||||||
|
|
||||||
instance.submit_for_ocr()
|
|
||||||
|
|
||||||
def view_action(self):
|
|
||||||
instance = self.get_object()
|
|
||||||
|
|
||||||
self.object_action(instance=instance)
|
|
||||||
|
|
||||||
messages.success(
|
|
||||||
self.request,
|
|
||||||
_('Document: %(document)s was added to the OCR queue.') % {
|
|
||||||
'document': instance
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DocumentSubmitManyView(MultipleInstanceActionMixin, DocumentSubmitView):
|
|
||||||
model = Document
|
model = Document
|
||||||
|
object_permission = permission_ocr_document
|
||||||
success_message = '%(count)d document submitted to the OCR queue.'
|
success_message = '%(count)d document submitted to the OCR queue.'
|
||||||
success_message_plural = '%(count)d documents submitted to the OCR queue.'
|
success_message_plural = '%(count)d documents submitted to the OCR queue.'
|
||||||
|
|
||||||
def get_extra_context(self):
|
def get_extra_context(self):
|
||||||
# Override the base class method
|
queryset = self.get_queryset()
|
||||||
return {
|
|
||||||
'title': _('Submit the selected documents to the OCR queue?')
|
result = {
|
||||||
|
'title': ungettext(
|
||||||
|
'Submit the selected document to the OCR queue?',
|
||||||
|
'Submit the selected documents to the OCR queue?',
|
||||||
|
queryset.count()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def object_action(self, form, instance):
|
||||||
|
instance.submit_for_ocr()
|
||||||
|
|
||||||
|
|
||||||
class DocumentTypeSubmitView(FormView):
|
class DocumentTypeSubmitView(FormView):
|
||||||
form_class = DocumentTypeSelectForm
|
form_class = DocumentTypeSelectForm
|
||||||
|
|||||||
Reference in New Issue
Block a user