Refactor the model accesors

Refactor the accesors to behave like methods instead of properties.
This means all accesors will be prepended with the string
"get_" and will include a set of parenthesis.

Improve the ModeAttribute class to use the method's
short_description. This commit also adds support for a
new method .help_text attribute has been added.

Move accessors to their own module, named "methods.py".

Remove the PropertyHelper class as the accessors no longer
need it.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2018-12-15 04:49:40 -04:00
parent 8c63ef4c69
commit 0e86f2ad8a
42 changed files with 434 additions and 282 deletions
+15 -34
View File
@@ -1,13 +1,11 @@
from __future__ import unicode_literals
from datetime import timedelta
import logging
from kombu import Exchange, Queue
from django.apps import apps
from django.db.models.signals import post_save
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from mayan.apps.acls import ModelPermission
@@ -16,14 +14,12 @@ from mayan.apps.common import (
menu_tools
)
from mayan.apps.common.classes import ModelAttribute, ModelField
from mayan.apps.common.settings import settings_db_sync_task_delay
from mayan.apps.documents.search import document_search, document_page_search
from mayan.apps.documents.signals import post_version_upload
from mayan.apps.documents.widgets import document_link
from mayan.apps.navigation import SourceColumn
from mayan.celery import app
from .events import event_ocr_document_version_submit
from .handlers import (
handler_index_document, handler_initialize_new_ocr_settings,
handler_ocr_document_version,
@@ -35,37 +31,20 @@ from .links import (
link_document_type_ocr_settings, link_document_type_submit,
link_entry_list
)
from .methods import (
method_document_ocr_submit, method_document_version_ocr_submit,
method_get_document_ocr_content, method_get_document_version_ocr_content
)
from .permissions import (
permission_document_type_ocr_setup, permission_ocr_document,
permission_ocr_content_view
)
from .queues import * # NOQA
from .signals import post_document_version_ocr
from .utils import document_property_ocr_content
logger = logging.getLogger(__name__)
def document_ocr_submit(self):
latest_version = self.latest_version
# Don't error out if document has no version
if latest_version:
latest_version.submit_for_ocr()
def document_version_ocr_submit(self):
from .tasks import task_do_ocr
event_ocr_document_version_submit.commit(
action_object=self.document, target=self
)
task_do_ocr.apply_async(
eta=now() + timedelta(seconds=settings_db_sync_task_delay.value),
kwargs={'document_version_pk': self.pk},
)
class OCRApp(MayanAppConfig):
app_namespace = 'ocr'
app_url = 'ocr'
@@ -96,22 +75,24 @@ class OCRApp(MayanAppConfig):
DocumentVersionOCRError = self.get_model('DocumentVersionOCRError')
Document.add_to_class(
name='submit_for_ocr', value=document_ocr_submit)
name='get_ocr_content',
value=method_get_document_ocr_content
)
Document.add_to_class(
name='ocr_content', value=document_property_ocr_content
name='submit_for_ocr', value=method_document_ocr_submit
)
DocumentVersion.add_to_class(
name='submit_for_ocr', value=document_version_ocr_submit
name='get_ocr_content',
value=method_get_document_version_ocr_content
)
DocumentVersion.add_to_class(
name='submit_for_ocr', value=method_document_version_ocr_submit
)
ModelAttribute(
model=Document, name='ocr_content', description=_(
'The OCR content of the document.'
)
)
ModelAttribute(model=Document, name='get_ocr_content')
ModelField(
Document, name='versions__pages__ocr_content__content'
model=Document, name='versions__pages__ocr_content__content'
)
ModelPermission.register(
+51
View File
@@ -0,0 +1,51 @@
from __future__ import unicode_literals
from datetime import timedelta
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from mayan.apps.common.settings import settings_db_sync_task_delay
from .events import event_ocr_document_version_submit
from .tasks import task_do_ocr
from .utils import get_document_version_content_iterator
def method_document_ocr_submit(self):
latest_version = self.latest_version
# Don't error out if document has no version
if latest_version:
latest_version.submit_for_ocr()
def method_document_version_ocr_submit(self):
event_ocr_document_version_submit.commit(
action_object=self.document, target=self
)
task_do_ocr.apply_async(
eta=now() + timedelta(seconds=settings_db_sync_task_delay.value),
kwargs={'document_version_pk': self.pk},
)
def method_get_document_ocr_content(self):
latest_version = self.latest_version
# Don't error out if document has no version
if latest_version:
latest_version.get_ocr_content()
method_get_document_ocr_content.short_description = _(
'get_ocr_content()'
)
method_get_document_ocr_content.help_text = _(
'Return the OCR content of the document.'
)
def method_get_document_version_ocr_content(self):
return ' '.join(
get_document_version_content_iterator(document_version=self)
)
+1 -1
View File
@@ -1,4 +1,4 @@
from __future__ import unicode_literals
TEST_OCR_INDEX_NODE_TEMPLATE = '{% if "mayan" in document.ocr_content.lower() %}mayan{% endif %}'
TEST_OCR_INDEX_NODE_TEMPLATE = '{% if "mayan" in document.get_ocr_content().lower() %}mayan{% endif %}'
TEST_OCR_INDEX_NODE_TEMPLATE_LEVEL = 'mayan'
+6 -6
View File
@@ -82,7 +82,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase):
)
self._request_document_submit_view()
self.assertTrue(
TEST_DOCUMENT_CONTENT in self.document.ocr_content
TEST_DOCUMENT_CONTENT in self.document.get_ocr_content()
)
def _request_multiple_document_submit_view(self):
@@ -95,7 +95,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase):
def test_multiple_document_submit_view_no_permission(self):
self._request_multiple_document_submit_view()
self.assertEqual(self.document.ocr_content, '')
self.assertEqual(self.document.get_ocr_content(), '')
def test_multiple_document_submit_view_with_access(self):
self.grant_access(
@@ -103,7 +103,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase):
)
self._request_multiple_document_submit_view()
self.assertTrue(
TEST_DOCUMENT_CONTENT in self.document.ocr_content
TEST_DOCUMENT_CONTENT in self.document.get_ocr_content()
)
def _request_document_ocr_download_view(self):
@@ -126,7 +126,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase):
self.assertEqual(response.status_code, 200)
self.assert_download_response(
response=response, content=self.document.ocr_content
response=response, content=self.document.get_ocr_content()
)
@@ -169,7 +169,7 @@ class DocumentTypeViewsTestCase(GenericDocumentViewTestCase):
response = self._request_document_type_submit_view()
self.assertEqual(response.status_code, 200)
self.assertTrue(
TEST_DOCUMENT_CONTENT not in self.document.ocr_content
TEST_DOCUMENT_CONTENT not in self.document.get_ocr_content()
)
def test_document_type_submit_view_with_access(self):
@@ -179,5 +179,5 @@ class DocumentTypeViewsTestCase(GenericDocumentViewTestCase):
response = self._request_document_type_submit_view()
self.assertEqual(response.status_code, 302)
self.assertTrue(
TEST_DOCUMENT_CONTENT in self.document.ocr_content
TEST_DOCUMENT_CONTENT in self.document.get_ocr_content()
)
+11 -7
View File
@@ -4,20 +4,24 @@ from django.apps import apps
from django.utils.encoding import force_text
def get_document_ocr_content(document):
def get_document_content_iterator(document):
latest_version = document.latest_version
if latest_version:
return get_document_version_content_iterator(
document_version=latest_version
)
def get_document_version_content_iterator(document_version):
DocumentPageOCRContent = apps.get_model(
app_label='ocr', model_name='DocumentPageOCRContent'
)
for page in document.pages.all():
for page in document_version.pages.all():
try:
page_content = page.ocr_content.content
except DocumentPageOCRContent.DoesNotExist:
return
else:
yield force_text(page_content)
@property
def document_property_ocr_content(self):
return ' '.join(get_document_ocr_content(self))
+2 -2
View File
@@ -19,7 +19,7 @@ from .permissions import (
permission_ocr_content_view, permission_ocr_document,
permission_document_type_ocr_setup
)
from .utils import get_document_ocr_content
from .utils import get_document_content_iterator
class DocumentOCRContentView(SingleObjectDetailView):
@@ -49,7 +49,7 @@ class DocumentOCRDownloadView(SingleObjectDownloadView):
def get_file(self):
file_object = DocumentOCRDownloadView.TextIteratorIO(
iterator=get_document_ocr_content(document=self.get_object())
iterator=get_document_content_iterator(document=self.get_object())
)
return DocumentOCRDownloadView.VirtualFile(
file=file_object, name='{}-OCR'.format(self.get_object())